Python要求库超时,但是从浏览器获取响应

时间:2017-10-21 11:21:20

标签: python web-scraping python-requests user-agent

我正在尝试为nba数据创建一个网络剪贴簿。当我运行以下代码时:

import requests

response = requests.get('https://stats.nba.com/stats/leaguedashplayerstats?College=&Conference=&Country=&DateFrom=10%2F20%2F2017&DateTo=10%2F20%2F2017&Division=&DraftPick=&DraftYear=&GameScope=&GameSegment=&Height=&LastNGames=0&LeagueID=00&Location=&MeasureType=Base&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PaceAdjust=N&PerMode=Totals&Period=0&PlayerExperience=&PlayerPosition=&PlusMinus=N&Rank=N&Season=2017-18&SeasonSegment=&SeasonType=Regular+Season&ShotClockRange=&StarterBench=&TeamID=0&VsConference=&VsDivision=&Weight=')

请求超时并出现错误:

  

文件" C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ requests \ api.py",   第70行,在得到       退货请求(' get',url,params = params,** kwargs)

     

文件" C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ requests \ api.py",   第56行,请求中       return session.request(method = method,url = url,** kwargs)

     

文件   " C:\ ProgramData \ Anaconda3 \ lib中\站点包\请求\ sessions.py&#34 ;,   第488行,请求中       resp = self.send(prep,** send_kwargs)

     

文件   " C:\ ProgramData \ Anaconda3 \ lib中\站点包\请求\ sessions.py&#34 ;,   第609行,发送       r = adapter.send(request,** kwargs)

     

文件   " C:\ ProgramData \ Anaconda3 \ lib中\站点包\请求\ adapters.py&#34 ;,   第473行,发送       引发ConnectionError(错误,请求=请求)

     

ConnectionError :('连接中止。',OSError("(10060,   ' WSAETIMEDOUT')",))

然而,当我在浏览器中点击相同的URL时,我收到了回复。

3 个答案:

答案 0 :(得分:5)

看起来您提到的网站正在检查请求标头中的"User-Agent"。您可以伪造请求中的"User-Agent",使其看起来像来自实际的浏览器,您将收到回复。

例如:

>>> import requests
>>> url = "https://stats.nba.com/stats/leaguedashplayerstats?College=&Conference=&Country=&DateFrom=10%2F20%2F2017&DateTo=10%2F20%2F2017&Division=&DraftPick=&DraftYear=&GameScope=&GameSegment=&Height=&LastNGames=0&LeagueID=00&Location=&MeasureType=Base&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PaceAdjust=N&PerMode=Totals&Period=0&PlayerExperience=&PlayerPosition=&PlusMinus=N&Rank=N&Season=2017-18&SeasonSegment=&SeasonType=Regular+Season&ShotClockRange=&StarterBench=&TeamID=0&VsConference=&VsDivision=&Weight="
>>> headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'}

>>> response = requests.get(url, headers=headers)
>>> response.status_code
200

>>> response.text  # will return the website content

答案 1 :(得分:3)

如果仍然无法使用,请使用此标头

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36','Accept-Encoding': 'gzip, deflate, br','Accept-Language': 'en-US,en;q=0.9,hi;q=0.8'}

答案 2 :(得分:0)

如果其他标题不起作用,请尝试使用此 HEADER ,对我来说效果很好。

headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 Safari/605.1.15","Accept-Language": "en-gb","Accept-Encoding":"br, gzip, deflate","Accept":"test/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Referer":"http://www.google.com/"}

this link

收集了这些标头