关于python:基于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("新闻标题:%sn新闻工夫:%sn新闻链接:%snn" % (i['title'], i['date'], i['url']))
        else:
            print("申请失败:%s %s" % (result['error_code'], result['reason']))
    except Exception as e:
        print("解析后果异样:%s" % e)
else:
    # 可能网络异样等问题,无奈获取返回内容,申请异样
    print("申请异样")

三、返回后果

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理