-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwrapper.py
173 lines (138 loc) · 5.42 KB
/
wrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'''
Created on Sep 17, 2017
@author: Clark
'''
import os, sys, time, json, re
from bs4 import BeautifulSoup
def check_create_path(path):
'''
Create path for output
'''
if not os.path.isdir(path):
os.makedirs (path)
def get_content(inputfile):
'''
Get file name, load the JSON LINES data
'''
data_list = []
with open(inputfile) as f:
for line in f:
data_list.append(json.loads(line))
return data_list
def get_detail (html_doc):
'''
Extract article category, subject, content, publish date, total views and author.
'''
soup = BeautifulSoup(html_doc, 'html.parser')
print(soup.h1.text)
if 'I Asked Jeff Bezos The Tough Questions — No Profits, The Book Controversies' in soup.h1.text:
print ('Stop')
# Get article category
if soup.find('h2', class_ ='vert-name') is not None:
category = ' '.join(soup.find('h2', class_ ='vert-name').text.split())
# elif soup.find('h2', class_ ='vert-name ellipsis') is not None:
# category = ' '.join(soup.find('h2', class_ ='vert-name ellipsis').text.split())
else:
category = None
# Get subject of the article
if soup.h1 is not None:
subject = soup.h1.text
else:
subject = None
# Get content
if soup.find('div', class_=re.compile('KonaBody post-content*')) is not None:
content = ' '.join(soup.find('div', class_=re.compile('KonaBody post-content*')).text.split())
elif soup.find('div', class_='intro-content') is not None:
content = ' '.join(soup.find('div', class_='intro-content').text.split())
else:
content = None
# Get publish date
publishdate = soup.find('span', class_='svg sprites date-icon').next_sibling.next_sibling.text
# Get page views
if soup.find('span', title='Engagement') is not None:
views = int(soup.find('span', title='Engagement').text.replace(',',''))
else:
views = None
# # Get author from meta tag.
# if soup.find('meta', property="author") is not None:
# author = (soup.find('meta', property="author")['content'])
# Get author(s) name.
if soup.find('li', class_='ks-author-byline') is not None:
author = soup.find('li', class_='ks-author-byline').text.replace('and',',')
elif soup.find('li', class_='single-author') is not None:
author = soup.find('li', class_='single-author').text
else:
author = None
if ',' in author:
author = author.split(',')
elif ' and ' in author:
author = author.split(' and ')
else:
author = [author]
data = {'category':category, 'subject': subject, 'content': content,
'publish_date': publishdate, 'views': views, 'author': author}
return data
def set_output(outfile_name, data_list):
'''
Generate JSON file format output
JSON file specification:
{
"url": {
"category": " Category Text ",
"subject": "Subject Text",
"content": "Article content",
"publish_date": "Article publish date",
"views": How many views on this article
"author": ["author1", "author 2"...]
},
...
}
'''
i = 0
output_obj = {}
for json_data in data_list:
raw_html = json_data['raw_content'] #html page content
data = get_detail(raw_html)
output_obj[str(json_data['url'])] = data
i += 1
with open(outfile_name, 'w') as outfile:
json.dump(output_obj, outfile, indent=4)
def main(input_file, output_path):
start = time.time()
data_list = get_content(input_file)
if output_path is not None:
# Generate JSON output
check_create_path(output_path)
outfile_name = output_path + '/wrapper.json'
set_output(outfile_name, data_list)
print('JSON file output has been generated.')
else:
print('Invalid output path.')
end = time.time()
print('runtime:', end - start)
if __name__ == '__main__':
# Get input and output parameters
if len(sys.argv) < 3:
print('Usage: python ' + sys.argv[0] + ' <JSON Lines input file name> </output/path>')
print(' The program requires Python 3.6 to execute.')
print(' - JSON Lines input file path = input file path of JSON Lines file')
print(' - output_path = Output file path of JSON file')
print(' Output JSON format:')
print(' {')
print(' "url": {')
print(' "category": " Category Text ",')
print(' "subject": "Subject Text",')
print(' "content": "Article content",')
print(' "publish_date": "Article publish date",')
print(' "views": How many views on this article,')
print(' "author": ["author1", "author 2"...]')
print(' },')
print(' ...')
print(' }')
exit ()
if len(sys.argv) == 3:
input_file = sys.argv[1]
output_path = sys.argv[2]
main(input_file, output_path)
else:
pass