【问题标题】:Unable to get the price of a product on Amazon when using Beautiful Soup in python在 python 中使用 Beautiful Soup 时无法获取亚马逊产品的价格
【发布时间】:2023-02-16 10:53:24
【问题描述】:

我试图使用 beautiful soup 来跟踪产品的价格,但每当我尝试运行此代码时,我都会得到一个 6 位代码,我认为它与 recaptcha 有关。我已经尝试了很多次,检查了标题、url 和标签,但似乎没有任何效果。

from bs4 import BeautifulSoup
import requests
from os import environ
import lxml


headers = {
    "User-Agent": environ.get("User-Agent"),
    "Accept-Language": environ.get("Accept-Language")
}

amazon_link_address = "https://www.amazon.in/Razer-Basilisk-Wired- 
Gaming-RZ01-04000100-R3M1/dp/B097F8H1MC/? 
_encoding=UTF8&pd_rd_w=6H9OF&content-id=amzn1.sym.1f592895-6b7a-4b03- 
9d72-1a40ea8fbeca&pf_rd_p=1f592895-6b7a-4b03-9d72-1a40ea8fbeca&pf_rd_r=1K6KK6W05VTADEDXYM3C&pd_rd_wg=IobLb&pd_rd_r=9fcac35b 
-b484-42bf-ba79-a6fdd803abf8&ref_=pd_gw_ci_mcx_mr_hp_atf_m"
response = requests.get(url=amazon_link_address, headers=headers)

soup = BeautifulSoup(response.content, features="lxml").prettify()

price = soup.find("a-price-whole")
print(price)

【问题讨论】:

  • 你还没有发布任何代码。如果没有看到您正在执行的代码,我们无法为您提供帮助

标签: python web-scraping bots tracker


【解决方案1】:

标签内的“a-price-whole”类因此 BS4 无法找到它。这个解决方案对我有用,我只是将你的“查找”更改为“find_all”并让它扫描所有跨度,直到找到你正在搜索的类,然后使用 iterator.get_text() 打印价格。希望这可以帮助!

soup = BeautifulSoup(response.content, features="lxml")

price = soup.find_all("span")
for i in price:
    try:
        if i['class'] == ['a-price-whole']:
            itemPrice = f"${str(i.get_text())[:-1]}"
            break
    except KeyError:
        continue

print(itemPrice)

【讨论】:

  • 哦,谢天谢地,它成功了!!!实际上我之前尝试过 find_all 但由于某种原因它根本没有出现在 Pycharm 中所以我认为它可能已在最近的一些软件包更新中被删除。再次感谢您抽出时间帮助...
猜你喜欢
  • 2020-07-27
  • 1970-01-01
  • 1970-01-01
  • 2013-02-10
  • 2015-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多