Python 3: Beautifulsoup and cache



我在Python3中使用beautifulsoup在网站上查找字符串。到目前为止一切正常。但是,如果网站发生变化,则需要一些时间,我的脚本也需要一些时间才能找到此更改。我想有任何类型的缓存,这就是问题所在。

    URL = 'https://www.lotto.de/lotto-6aus49/lottozahlen'
    soup = BeautifulSoup(requests.get(URL).text, "lxml")
    if  not 'Samstag' in soup.find('span', class_='WinningNumbers__date').text:
      print(soup.find('span', class_='WinningNumbers__date').text)
      print('Die Samstagszahlen sind noch nicht online.')
      sys.exit()

我的猜测是否正确,我该如何改变这种行为?

这与BeautifulSoup无关,而只与requests的使用有关。据我所知,默认情况下该库并不真正使用缓存,可以启用安装外部包requests-cache

但无论如何,您始终可以使用 Cache-Control 标头:

requests.get(URL, headers={'Cache-Control': 'no-cache'})

最新更新