聚合数据新闻头条接口查询调用示例

一、开通接口
新闻头条接口服务使用的聚合数据提供的免费接口,每天可以50次免费调用。可以通过https://www.juhe.cn/docs/api/id/235注册及开通。

二、Python发起接口请求

#!/usr/bin/python
# -*- coding: utf-8 -*-

import urllib, urllib2, sys, json

reload(sys)
sys.setdefaultencoding('utf-8')

url = 'http://v.juhe.cn/toutiao/index'

params = {
    
    
    "type": "top",  # 头条类型,top(头条,默认),shehui(社会),guonei(国内),guoji(国际),yule(娱乐),tiyu(体育)junshi(军事),keji(科技),caijing(财经),shishang(时尚)
    "key": "xxxxxxx",  # 您申请的接口API接口请求Key
}
querys = urllib.urlencode(params)

request = urllib2.Request(url, data=querys)
response = urllib2.urlopen(request)
content = response.read()
if (content):
    try:
        result = json.loads(content)
        error_code = result['error_code']
        if (error_code == 0):
            data = result['result']['data']
            for i in data:
                # 更多字段可参考接口文档
                print("新闻标题:%s\n新闻时间:%s\n新闻链接:%s\n\n" % (i['title'], i['date'], i['url']))
        else:
            print("请求失败:%s %s" % (result['error_code'], result['reason']))
    except Exception as e:
        print("解析结果异常:%s" % e)
else:
    # 可能网络异常等问题,无法获取返回内容,请求异常
    print("请求异常")

在这里插入图片描述

三、返回结果

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/juhedata/article/details/130872213
今日推荐