python通过API查阅热点新闻
来源:互联网 发布:java cas干什么用的 编辑:程序博客网 时间:2024/05/22 00:20
最近在看一些API接口的文档信息,随手写了比较简单的接口调用程序。
新闻的API用的是聚合数据的,免费的,需要自己申请一个账号,然后才有key。
先放新闻接口数据格式以及返回信息
接口地址:http://v.juhe.cn/toutiao/index支持格式:json请求方式:get/post请求示例:http://v.juhe.cn/toutiao/index?type=top&key=APPKEY接口备注:返回头条,社会,国内,娱乐,体育,军事,科技,财经,时尚等新闻信息
请求参数说明:名称类型必填说明 keystring是应用APPKEY typestring否类型,,top(头条,默认),shehui(社会),guonei(国内),guoji(国际),yule(娱乐),tiyu(体育)junshi(军事),keji(科技),caijing(财经),shishang(时尚)
JSON返回的示例:
{ "reason": "成功的返回", "result": { "stat": "1", "data": [ { "title": "巫山云雨枉断肠:女摄影师Erika Lust记录的性爱",/*标题*/ "date": "2016-06-13 10:31",/*时间*/ "author_name": "POCO摄影",/*作者*/ "thumbnail_pic_s": "http://09.imgmini.eastday.com/mobile/20160613/20160613103108_7b015493398e7fd13dda3a5ce315b1c8_1_mwpm_03200403.jpeg",/*图片1*/ "thumbnail_pic_s02": "http://09.imgmini.eastday.com/mobile/20160613/20160613103108_7b015493398e7fd13dda3a5ce315b1c8_1_mwpl_05500201.jpeg",/*图片2*/ "thumbnail_pic_s03": "http://09.imgmini.eastday.com/mobile/20160613/20160613103108_7b015493398e7fd13dda3a5ce315b1c8_1_mwpl_05500201.jpeg",/*图片3*/ "url": "http://mini.eastday.com/mobile/160613103108379.html?qid=juheshuju",/*新闻链接*/ "uniquekey": "160613103108379",/*唯一标识*/ "type": "头条",/*类型一*/ "realtype": "娱乐"/*类型二*/ },...]}}
coding=utf-8import urllibimport urllib2import reimport jsonuser_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'headers = { 'User-Agent' : user_agent }def news(ntype): url = 'http://v.juhe.cn/toutiao/index?type='+str(type)+'&key=' try: request = urllib2.Request(url,headers = headers) response = urllib2.urlopen(request) content = response.read() data=json.loads(content) mynews=data['result']['data'] for i in range(len(mynews)): print mynews[i]['title'] print u"点击新闻链接%s"%mynews[i]['url'] except urllib2.URLError, e: if hasattr(e,"code"): print e.code if hasattr(e,"reason"): print e.reasonwhile 1: print "-"*70 ntype=raw_input(u"请输入要查看热点新闻的类型,top(头条,默认),shehui(社会),guonei(国内),guoji(国际),yule(娱乐),tiyu(体育)junshi(军事),keji(科技),caijing(财经),shishang(时尚)") news(ntype) print "-"*70
程序运行结果:
0 0
- python通过API查阅热点新闻
- 热点新闻
- python脚本爬取今日百度热点新闻
- 查阅linux kernel api
- 查阅API文档
- 学会查阅API文档
- java-API文档如何查阅
- 热点新闻算法
- 热点新闻算法
- iOS API离线文档下载和查阅
- DSOframer 微软官方API的查阅方法
- Android API配置查阅(本地+在线)
- python 通过urllib 访问api接口
- 通过Python调用OpenStack 的RESTful API
- 用python 通过12306api抓取列车信息
- Python 通过api操控鼠标键盘
- 为什么开发者热衷在Stack Overflow上查阅API文档?
- 黑马程序员_java基础——如何查阅API
- 面向对象_匿名内部类的格式和理解
- Python学习-用户交互raw_input()
- HDOJ 5724 Chess (SG)
- [心得]利用python并发提速上线测试效率
- Codeforces 730G Car Repair Shop
- python通过API查阅热点新闻
- 2014~2016百度之星hdu题目
- 和《创时代》
- Linux学习--第四天--find、locate、帮助命令、grep、who、w、压缩命令、网络命令、mount
- Java——第二章(变量)
- 并查集的find和join函数实现
- 泛型是个巨坑
- LeetCode-Third Maximum Number
- 思科 IOS简介