首页 > 编程语言 > 详细

python练习

时间:2016-01-27 19:40:50      阅读:224      评论:0      收藏:0      [点我收藏+]
 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__

 

python练习

原文:http://www.cnblogs.com/Alex-LiN/p/5164232.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!