-
Notifications
You must be signed in to change notification settings - Fork 2
/
api_nytimes.py
56 lines (45 loc) · 1.42 KB
/
api_nytimes.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
from nytimesarticle import articleAPI
import requests
import lxml.html as lh
import re
import sys
def searchTopic(topic):
f = open('secret', 'r')
for line in f:
if line.find('NYTIMES_ARTICLE') != -1:
a,key = line.split('=')
key = key.replace('\n','')
api = articleAPI(key)
topic = topic
headline = topic
sources = ['Reuters', 'AP', 'The New York Times']
beg_date = 20140101
pages = 4
articles_content = []
output = []
for page in range(pages):
articles = api.search(q = topic, fq = {'headline':topic, 'source':sources},
fl = ['web_url'], begin_date = beg_date, page = str(page + 1))
for url in articles['response']['docs']:
if url['web_url'].find('video') == -1:
page = requests.get(url['web_url'])
doc = lh.fromstring(page.content)
text = doc.xpath('//p[@itemprop="articleBody"]')
article = str()
for par in text:
paragraph = par.text_content()
article += paragraph
articles_content.append(article)
for count, art in enumerate(articles_content):
regex = re.compile('[^a-zA-Z]')
articles_content[count] = regex.sub(' ', art)
# with open('%s data.csv' % topic, 'wb') as output_file:
# wr = csv.writer(output_file, quoting=csv.QUOTE_ALL)
# wr.writerow(articles_content)
print articles_content
if __name__ == '__main__':
str_input = ''
for x in range(len(sys.argv) - 1):
str_input = str_input + sys.argv[x + 1] + ' '
print str_input
searchTopic(sys.argv[1:])