-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.py
37 lines (29 loc) · 1.1 KB
/
hello.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
#encoding:UTF-8
import os.path
import urllib2
import json
import tornado.ioloop
import tornado.web
headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
url = 'http://news.at.zhihu.com/api/1.2/news/latest'
req = urllib2.Request(url,headers = headers)
content = urllib2.urlopen(req).read()
json_cont = content.decode('u8')
loaded = json.loads(json_cont)
#print loaded['date']
#for each in loaded['news']:
# print 'title:', each['title']
# print 'url:', each['url']
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render('index.html', articles = loaded['news'])
#for each in loaded['news']:
# self.write('<p> <a href = {0} >{1}</a></p>\n'.format(each['share_url'], each['title'].encode('utf-8')))
application = tornado.web.Application(
handlers = [(r"/", MainHandler)],
template_path = os.path.join(os.path.dirname(__file__),"templates"),
debug = True
)
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()