繁体   English   中英

卷毛有效,但python请求不起作用

[英]Curl works but python requests doesn't

当我卷曲时,我得到一个响应:

root@3d7044bac92f:/home/app/tmp# curl -H "Content-type: application/json" -X GET https://github.com/timeline.json -k 

{"message":"Hello there, wayfaring stranger. If you\u2019re reading this then you probably didn\u2019t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.","documentation_url":"https://developer.github.com/v3/activity/events/#list-public-events"}

但是,当我向相同的网址发出python请求时,状态为410。

import requests

headers = {
    'Content-type': 'application/json',
}

r = requests.get('https://github.com/timeline.json')
print r.json

root@3d7044bac92f:/home/app/tmp# python rest.py 
<bound method Response.json of <Response [410]>>

是什么赋予了?

主机是标准的Ubuntu docker映像,仅安装了Curl和一些python模块。 Python -V为2.7

注意:我看了这个问题,但是我无法远程登录到上述服务器,因此该解决方案不适用于我: Curl有效但Python请求无效

您在程序中至少犯了两个错误。

1)您尚未为requests.get()调用指定data=headers参数。 尝试这个:

 r = requests.get('https://github.com/timeline.json', data=data, headers=headers)

2) .json是方法,而不是响应对象的数据属性。 作为一种方法,必须先调用它才能有效。 尝试这个:

print r.json()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM