簡體   English   中英

GET中的Python錯誤請求,但wget和curl有效

[英]Python bad request in GET but wget and curl work

我正在嘗試使用請求在python中執行get請求,但出現400錯誤的請求錯誤。 但是,當我在同一URL上執行wget或curl時,它可以工作。 這是我的代碼:

daily_scoreboard_url = 'http://stats.nba.com/stats/scoreboardV2?DayOffset=0&LeagueID=00&gameDate=01/12/2016'
scoreboard_response = requests.get(daily_scoreboard_url)
scoreboard_response.raise_for_status()

但是,當我嘗試類似

curl 'http://stats.nba.com/stats/scoreboardV2?DayOffset=0&LeagueID=00&gameDate=01/12/2016'
wget 'http://stats.nba.com/stats/scoreboardV2?DayOffset=0&LeagueID=00&gameDate=01/12/2016'

有用。 我也嘗試過使用stats.nba.com的get請求獲取其他內容,並且該方法有效。

teamslist_url = 'http://stats.nba.com/stats/leaguedashteamstats?Conference=&DateFrom=&DateTo=&Division=&GameScope=&GameSegment=&LastNGames=0&LeagueID=00&Location=&MeasureType=Base&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PaceAdjust=N&PerMode=PerGame&Period=0&PlayerExperience=&PlayerPosition=&PlusMinus=N&Rank=N&Season=2015-16&SeasonSegment=&SeasonType=Regular+Season&ShotClockRange=&StarterBench=&TeamID=0&VsConference=&VsDivision='
teamslist_response = requests.get(teamslist_url)
teamslist_response.raise_for_status()

您需要requests一個用戶代理

>>> headers = {'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36'}
>>> scoreboard_response = requests.get(daily_scoreboard_url, headers=headers)
>>> scoreboard_response
<Response [200]>

那是網站的錯,不是reuqests的錯。 一些網站需要用戶代理來檢查您是人類還是機器人/腳本。

但是,我們可以手動設置用戶代理。 在示例中,這就是我的Chrome瀏覽器的用戶代理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM