gpt4 book ai didi

python - 使用 python web scrape 获取

转载 作者:行者123 更新时间:2023-12-01 08:38:05 25 4
gpt4 key购买 nike

我正在尝试使用 python 中的 BeautifulSoup 获取产品价格。但无论我如何尝试,我总是会遇到错误。

The picture of the site i am trying to web scrape

我想获取 19,90 值。我已经编写了一段代码来获取所有产品名称,现在需要它们的价格。

    import requests
from bs4 import BeautifulSoup

url = 'https://www.zattini.com.br/busca?nsCat=Natural&q=amaro&searchTermCapitalized=Amaro&page=1'

page = requests.get(url)

soup = BeautifulSoup(page.text, 'html.parser')

price = soup.find('span', itemprop_='price')

print(price)

最佳答案

不太理想的是解析包含价格的 JSON

import requests
import json
import pandas as pd
from bs4 import BeautifulSoup

url = 'https://www.zattini.com.br/busca?nsCat=Natural&q=amaro&searchTermCapitalized=Amaro&page=1'
page = requests.get(url)
soup = BeautifulSoup(page.content, 'lxml')
scripts = [script.text for script in soup.select('script') if 'var freedom = freedom ||' in script.text]
pricesJson = scripts[0].split('"items":')[1].split(']')[0] + ']'
prices = [item['price'] for item in json.loads(pricesJson)]
names = [name.text for name in soup.select('#item-list [itemprop=name]')]
results = list(zip(names,prices))

df = pd.DataFrame(results)
print(df)

示例输出:

enter image description here

关于python - 使用 python web scrape 获取 <span> 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53624160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com