1 # encoding=utf-8
2 import json
3
4 import requests
5
6
7 class ZhihuLastedNews(object):
8 """
9 input url you will get the data from web
10 then get what you want
11 """
12 def __init__(self, url):
13 r = requests.get(url)
14 self.data = json.loads(r.text)
15
16 @property
17 def image_urls(self):
18 stories = self.data.get('stories')
19 return [d.get('images') for d in stories] # 列表推导式
20
21 @property
22 def date(self):
23 return self.data.get('date')
24
25 def __call__(self, url):
26 return requests.get(url).status_code
27
28 def __str__(self):
29 return 'ZhihuLastedNews version 1.0'
30
31 if __name__ == "__main__":
32 zhihu = ZhihuLastedNews("http://news-at.zhihu.com/api/4/news/latest")
33
34 print zhihu.image_urls
35 print "{0}年{1}月{2}号".format(zhihu.date[:4], zhihu.date[4:6], zhihu.date[6:])
36
37 print dir(ZhihuLastedNews)
38
39 print zhihu("http://news-at.zhihu.com/api/4/news/latest")
40
41 print zhihu.__doc__