Zara 上 Beautifulsoup 的 Python 抓取问题

问题描述 投票:0回答:1

我试图用这段代码(如下)抓取Zara的网页,结果我想获得产品的价格,但我不能,我得到了答案“[]”。我应该怎么办 ? 预先感谢。

from bs4 import BeautifulSoup
import requests

HEADERS = ({
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) `your text`Chrome/112.0.0.0 Safari/537.36",
        "Accept-Language": "tr-TR,tr;q=0.9,en-US;q=0.8,en;q=0.7"})

url6 = "https://www.zara.com/tr/tr/erkek-blazerlar-l608.html?v1=2298109"
webpage6 = requests.get(url6, HEADERS)
soup6 = BeautifulSoup(webpage6.content, "html.parser")

r = str(soup6.find_all("span", {"class":"product-grid-seo-info__header"}))
print(r)

运行-->结果:[]

尝试刮产品价格

python web-scraping beautifulsoup python-requests find
1个回答
0
投票

您需要按照

<meta>
刷新才能获得最终页面:

import requests
from bs4 import BeautifulSoup

headers = {
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/117.0"
}

url = "https://www.zara.com/tr/tr/erkek-blazerlar-l608.html?v1=2298109"
soup = BeautifulSoup(requests.get(url, headers=headers).content, "html.parser")

url2 = "https://www.zara.com" + (
    soup.select_one('meta[http-equiv="refresh"]')["content"]
    .split("=", maxsplit=1)[-1]
    .strip("'")
)

soup = BeautifulSoup(requests.get(url2, headers=headers).content, "html.parser")

price = soup.select_one(".price__amount").text
print(f"{price=}")

打印:

price='1.999,00 TL'
© www.soinside.com 2019 - 2024. All rights reserved.