【问题标题】:Bypass cookie agreement page while web scraping using Python使用 Python 抓取网页时绕过 cookie 协议页面
【发布时间】:2021-04-21 15:16:54
【问题描述】:

在抓取重定向 google url 后,我遇到了 google 协议页面 cookie 的问题。

我正在尝试从 Google 新闻 uri 上的不同页面抓取,但是当我运行此代码时:

req = requests.get(url,headers=headers)

with "headers" = {'User-Agent': 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US) AppleWebKit/534.1 (KHTML, like Gecko) Chrome/6.0.422.0 Safari/534.1', 'Upgrade-Insecure-Requests': '1', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'DNT': '1', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'it-IT'}
and for example URL = https://news.google.com/./articles/CAIiEMb3PYSjFFVbudiidQPL79QqGQgEKhAIACoHCAow-ImTCzDRqagDMKiIvgY?hl=it&gl=IT&ceid=IT%3Ait 

the "request.content" is the HTMLs code of agreement cookies page by Google.

我也尝试将重定向链接转换为普通链接,但响应为我提供了指向this的重定向链接

我有与此问题相关的相同问题 (How can I bypass a cookie agreement page while web scraping using Python?)。

无论如何,其中提出的解决方案仅适用于特定站点。

注意:整个代码直到几周前才有效。

【问题讨论】:

    标签: python web-scraping cookies


    【解决方案1】:

    我通过添加行解决了问题

    'Cookie':'CONSENT=YES+cb.20210418-17-p0.it+FX+917; '

    到请求头。

    虽然请求返回的页面仍然是 Google 页面,但该页面包含指向发起请求的站点的链接。

    所以,一旦我获得了页面,我就进行了更多的抓取,以便获得链接并启动我想要的请求。

    【讨论】:

      猜你喜欢
      • 2019-12-19
      • 2021-04-20
      • 2013-09-24
      • 1970-01-01
      • 2011-12-24
      相关资源
      最近更新 更多