Python 请求模块未从 Web 服务器获取最新数据

Python Requests Module Not Getting Latest Data from Web Server

在下面的代码片段中,您可以看到我正在尝试从 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 HTTP 请求请求提取我需要的所有数据,然后使用 BeautifulSoup 从 HTML 中提取数据。 The full scraper is here if you'd like to take a look.

问题:当 Requests 从 NCAA 网站获得响应时,数据比 NCAA 网站上的数据要旧得多(有时至少达 30 或 40 分钟)实际网站。

我已经在谷歌上搜索了几个小时。阅读 Python Requests docs 后,我相信我发现 NCAA 网络服务器正在发送过时的数据。但是我不明白为什么它在发送 Google Chrome (或任何网络浏览器)正确的数据时会向我的程序发送过时的数据。

我认为服务器发送过时数据的原因是当我打印响应 headers 时,其中一项是 'Last-Modified': 'Sat, 26 Jan 2019 17:49:13 GMT' 而另一个是 'Date': 'Sat, 26 Jan 2019 18:20:29 GMT',所以看起来服务器在正确的时间收到请求,但提供的数据还没有有一段时间修改了。

我的问题:你知道为什么会发生这种情况吗?我是否需要在我的 HTTP 请求中添加一些内容,以使服务器向我发送与发送 Web 浏览器的内容一致的数据?

P.S。很抱歉问了这么长的问题。我试图保持简洁,但仍然解释清楚。

尝试更改请求 header 中的 user-agent,使其与您的 Google Chrome user-agent 相同,方法是将其添加到您的 headers:

headers = {
    'User-Agent': 'Add your google chrome user-agent here'
}

在您的 requests.get() 之前,尝试添加一个 header:

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 object return 确实 scores/data。您需要做的就是更改 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'])

如上述答案所述,您需要做的是设置一个合法的user-agent。因此添加 headers 来模拟浏览器:

# 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 假装(更多)像一个合法的浏览器。像这样添加更多 headers:

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' }