【问题标题】:Cookies and cookie consent boxes when crawling webpage with BeautifulSoup and Requests in Python在 Python 中使用 BeautifulSoup 和 Requests 抓取网页时的 Cookie 和 cookie 同意框
【发布时间】:2023-03-09 14:07:01
【问题描述】:

我正在编写一个脚本,它应该抓取价格比较页面 like this one,它应该每 x 秒运行一次,并在某个商品的价格低于 X 时通过电子邮件提醒自己。可悲的是,我'我无法提取我想要的数据(在名称和价格列中,html 参考如下)并认为问题是由 cookie 同意框引起的。

当我通过浏览器手动查看页面的html时,我看到列出的项目的名称和价格是:

<a href="inno3d-geforce-rtx-3070-ichill-x3-c30703-08d6x-1710va38-a2408388.html?hloc=at&amp;hloc=de" class="productlist__link">
<span class="notrans">
INNO3D GeForce RTX 3070 iCHILL X3, 8GB GDDR6, HDMI, 3x DP (C30703-08D6X-1710VA38)
</span>
</a>
<div class="productlist__item productlist__description">
<p class="x"><span class="notrans">Anschlüsse: 1x HDMI 2.1, 3x DisplayPort 1.4a • Grafik: NVIDIA GeForce RTX 3070 (Desktop), 8GB GDDR6 • Chip: GA104-300-A1 "Ampere", 46SM, 392mm² • Fertigung: 8nm (Samsung) • Chiptakt: 1500MHz, Boost: 1785MHz • Speicher: 8GB GDDR6, 1750MHz, 256bit, ...</span></p>
</div>
</div>

<span class="gh_price"> <span class="notrans">€ 609,00</span>
</span>
</div>

分别为名称和价格。使用BeautifulSoup.find_all("a", class_"productlist__link")BeautifulSoup.find_all("span", class_="gh_price") 可以轻松地将它们从给定的示例中提取出来。编辑:为澄清起见,我使用x=BeautifulSoup(response.text, "lxml") 并在之后调用x.find_all(...),就像下面的代码一样。

我正在使用以下代码抓取页面:

import requests
from bs4 import BeautifulSoup

URL = "https://www.heise.de/preisvergleich/?cat=gra16_512&xf=9810_16+0020314+-+RTX+3070"
params_ = {"hloc":"de", "hloc":"at", "v":"k"}
session = requests.session()
response = session.get(URL, params=params_, cookies=cookies_)
response.raise_for_status()
session.close()
soup=BeautifulSoup(response.text, "lxml")

但是当像以前一样使用soup.find_all(...) 时,我的结果列表是一个空列表。在浏览器中查看 response.text 后,我注意到该站点几乎不可读,除了 cookie 同意之外,因此我将浏览器使用的所有 cookie 复制到一个名为 cookies_={...} 的字典中,并将它们添加到请求中(如已在上面的代码部分),但响应仍然不可读,cookie 同意书仍然存在并且没有提取数据。

所以,我现在的问题是,我在尝试摆脱同意框时做错了什么,以及我是否还有其他遗漏。据我了解,即使有同意框,项目列表仍应在response 中,我的代码仍应提取它(但没有)所以我猜我也错过了一些其他想法.

【问题讨论】:

    标签: python html cookies beautifulsoup python-requests


    【解决方案1】:

    恕我直言,您并不真的需要 cookie。该站点通过一个简单的请求回复完整的 HTML。甚至不需要标题。

    这是一个例子:

    import requests
    from bs4 import BeautifulSoup
    from tabulate import tabulate
    
    url = "https://www.heise.de/preisvergleich/?cat=gra16_512&xf=9810_16+0020314+-+RTX+3070"
    soup = BeautifulSoup(requests.get(url).content, "html.parser")
    products = [
        f"https://www.heise.de/preisvergleich/{p['href']}" for p
        in soup.find_all("a", class_="productlist__link")
    ]
    prices = [
        p.getText(strip=True) for p
        in soup.find_all("span", class_="gh_price")
    ]
    
    print(tabulate(zip(products, prices), headers=["Product URL", "Price"]))
    
    

    这个输出:

    Product URL                                                                                                                      Price
    -------------------------------------------------------------------------------------------------------------------------------  ----------
    https://www.heise.de/preisvergleich/gigabyte-geforce-rtx-3070-gaming-oc-8g-gv-n3070gaming-oc-8gd-a2406936.html?hloc=at&hloc=de   € 799,00
    https://www.heise.de/preisvergleich/zotac-gaming-geforce-rtx-3070-twin-edge-zt-a30700e-10p-a2363490.html?hloc=at&hloc=de         € 609,00
    https://www.heise.de/preisvergleich/gigabyte-aorus-geforce-rtx-3070-master-8g-gv-n3070aorus-m-8gd-a2406973.html?hloc=at&hloc=de  ab€ 889,00
    https://www.heise.de/preisvergleich/gigabyte-geforce-rtx-3070-vision-oc-8g-gv-n3070vision-oc-8gd-a2406952.html?hloc=at&hloc=de   € 1095,00
    https://www.heise.de/preisvergleich/msi-geforce-rtx-3070-ventus-2x-oc-a2364400.html?hloc=at&hloc=de                              € 1099,00
    https://www.heise.de/preisvergleich/palit-geforce-rtx-3070-gamingrock-ne63070019p2-1040g-a2408422.html?hloc=at&hloc=de           € 899,90
    https://www.heise.de/preisvergleich/asus-dual-geforce-rtx-3070-oc-a2366124.html?hloc=at&hloc=de                                  ab€ 799,00
    https://www.heise.de/preisvergleich/gainward-geforce-rtx-3070-phantom-gs-2201-a2435816.html?hloc=at&hloc=de                      € 769,00
    https://www.heise.de/preisvergleich/inno3d-geforce-rtx-3070-twin-x2-oc-n30702-08d6x-1710va32l-a2429903.html?hloc=at&hloc=de      € 699,00
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-25
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      • 2015-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多