[英]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.