1 回答
TA贡献1878条经验 获得超4个赞
您的脚本几乎是正确的,只需使用lxml或html5lib解析器而不是html.parser:
import requests
from bs4 import BeautifulSoup
URL = 'https://www.amazon.es/dp/B07JQRWLXM/ref=nav_signin?pf_rd_r=FJX3CJC8RWFE3NPQJRNP&'
headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36'}
page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, 'lxml') # <-- change to 'lxml' or 'html5lib'
title = soup.find(id="productTitle").get_text()
price = soup.find(id = "priceblock_ourprice").get_text()
converted_price = float(price.split()[0].replace(',', '.')) # <-- change the conversion method
print(converted_price)
print(title.strip())
印刷:
69.99
Tablet Fire 7, pantalla de 7'', 16 GB (Negro) - Incluye ofertas especiales
添加回答
举报