Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #5 from danieleewww/patch-2

Update nytimes_pull.py
  • Loading branch information...
commit 33af094dffeb48ae6c4ce18cebb5162c69a2e35f 2 parents dbc59f1 + b195d45
@hmason authored
Showing with 14 additions and 15 deletions.
  1. +14 −15 intro_web_data/nytimes_pull.py
View
29 intro_web_data/nytimes_pull.py
@@ -1,12 +1,3 @@
-#!/usr/bin/env python
-# encoding: utf-8
-"""
-nytimes_pull.py
-
-Created by Hilary Mason on 2011-02-17.
-Copyright (c) 2011 Hilary Mason. All rights reserved.
-"""
-
import urllib
import json
@@ -16,16 +7,24 @@ def main(api_key, category, label):
for i in range(0,5):
# print "http://api.nytimes.com/svc/search/v2/articlesearch.json?fq=news_desk:('%s')&api-key=%s&page=%s" % (category, api_key, i)
h = urllib.urlopen("http://api.nytimes.com/svc/search/v2/articlesearch.json?fq=news_desk:(\"%s\")&api-key=%s&page=%s" % (category, api_key, i))
- data = json.loads(h.read())
- for result in data['results']:
- content.append(result['body'])
-
+ print h
+ try:
+ result = json.loads(h.read())
+ content.append(result)
+ except ValueError:
+ print "Malformed JSON: " + data
+ continue #In the rare cases that JSON refuses to parse
+
f = open(label, 'w')
for line in content:
- f.write('%s\n' % line.encode('utf-8'))
+ try:
+ f.write('%s\n' % line)
+ except UnicodeEncodeError:
+ pass
f.close()
if __name__ == '__main__':
main("f7b4a1749764aec0364b215c354e3a0f:18:25759498", "Arts","arts")
- main("f7b4a1749764aec0364b215c354e3a0f:18:25759498", "Sports","sports")
+ main("f7b4a1749764aec0364b215c354e3a0f:18:25759498", "Sports","sports")
+
Please sign in to comment.
Something went wrong with that request. Please try again.