在下面的代码段中,您可以看到我正在尝试从NCAA男子篮球网站上抓取一些数据。
import requests
url = "https://www.ncaa.com/scoreboard/basketball-men/d1/"
response = requests.get(url)
html = response.text
print(html)
print(response.headers)
print("\n\n")
print(response.request.headers)
该网站上列出了游戏及其得分。我想出了如何使用Python Requests来获取我需要的所有数据的HTTP请求,然后使用BeautifulSoup从HTML提取数据的方法。 The full scraper is here if you'd like to take a look.
问题:当“请求”从NCAA网站获得响应时,数据比实际网站上的数据要早得多(有时至少长达30或40分钟)。>
我已经搜索了几个小时。阅读Python Requests docs之后,我相信我发现NCAA Web服务器正在发送过时的数据。但是我不明白为什么当它向Google Chrome(或任何网络浏览器)发送正确的数据时,为什么会发送我的程序过时的数据。
我认为服务器正在发送过时的数据的原因是,当我打印响应标头时,其中一项是'Last-Modified':'Sat,26 Jan 2019 17:49:13 GMT'< / em>,而另一个是'Date':'Sat,26 Jan 2019 18:20:29 GMT',因此服务器似乎在正确的时间获取了请求,但提供的数据有一阵子没有被修改。
我的问题:您知道发生这种情况的任何原因吗?我需要在HTTP请求中添加一些内容,以使服务器向我发送与发送Web浏览器一致的数据吗?
P.S。我很抱歉这个长问题。我试图保持简洁,但仍然清楚地解释了事情。
答案 0 :(得分:5)
在您的requests.get()
之前,尝试添加标题:
import requests
url = "https://www.ncaa.com/scoreboard/basketball-men/d1/"
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
response = requests.get(url, headers = headers)
html = response.text
我的其他建议是使用:
url = 'https://data.ncaa.com/casablanca/scoreboard/basketball-men/d1/2019/01/26/scoreboard.json'
并使用json包读取它。所有内容都以一种不错的JSON格式实时存在,并为您提供了
代码
import json
import requests
url = 'https://data.ncaa.com/casablanca/scoreboard/basketball-men/d1/2019/01/26/scoreboard.json'
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
response = requests.get(url, headers = headers)
jsonStr = response.text
jsonObj = json.loads(jsonStr)
我检查了一下,JSON对象确实返回了实时得分/数据。您所需要做的就是更改URL 2019/01/26
中的日期,以获取游戏的先前日期完成数据。
编辑-附加
这可以帮助您提取数据。注意如何将其更改为今天的日期以获取当前数据。它为您提供了一个不错的数据框:
from pandas.io.json import json_normalize
import json
import requests
url = 'https://data.ncaa.com/casablanca/scoreboard/basketball-men/d1/2019/01/27/scoreboard.json'
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
# Thanks to InfectedDrake wisdom, the following 3 lines that I previously had can be replaced by a single line. See below
#response = requests.get(url, headers = headers)
#jsonStr = response.text
#jsonObj = json.loads(jsonStr)
jsonObj = requests.get(url, headers = headers).json()
result = json_normalize(jsonObj['games'])
答案 1 :(得分:3)
尝试通过将其添加到标头中来更改请求标头中的用户代理,使其与Google Chrome用户代理相同:
headers = {
'User-Agent': 'Add your google chrome user-agent here'
}
答案 2 :(得分:0)
如上述答案所述,您需要做的是设置一个合法的用户代理。因此,添加标题以模拟浏览器:
# This is a standard user-agent of Chrome browser running on Windows 10
headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36' }
此外,您可以像合法浏览器一样添加另一组标题以假装(更多)。添加一些其他标题,如下所示:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language' : 'en-US,en;q=0.5',
'Accept-Encoding' : 'gzip',
'DNT' : '1', # Do Not Track Request Header
'Connection' : 'close' }