登录
原创

基于Python的免费新闻头条接口查询

专栏聚合数据JUHE.CN
发布于 2020-11-25 阅读 18469
  • Python
  • 聚合
原创

一、开通接口

新闻头条接口服务使用的聚合数据提供的免费接口,每天可以100次免费调用。可以通过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("请求异常")

三、返回结果

image.png

评论区

励志做一条安静的咸鱼,从此走上人生巅峰。

0

0

0

举报