从webscrape Python发布挑选价格



遵循指南,但仍然无法从网站上选择价格-我想选择产品名称和产品价格。

我可以选择控制台中出现的name=。价格返回"无"。请不要键入错误我不知道哪里出了问题。

page = requests.get('https://www.wickes.co.uk/search?text=brick')
soup = BeautifulSoup(page.content, 'html.parser')
all_bricks = soup.find(class_='products-list products-list-v2')
items = all_bricks.find(class_='card product-card')
items_name = all_bricks.find(class_='product-card__title product-card__title-v2')
price_box = items.find("div", attrs={"class": "product-card__price-value "})
price = price_box
print (price)

这里有两个问题:

  1. 您在类名中添加了额外的空间。BeautifulSoup被设计为修剪html DOM中的多余空间
  2. 您没有使用.text来收回价格
from bs4 import BeautifulSoup
import requests
page = requests.get('https://www.wickes.co.uk/search?text=brick')
soup = BeautifulSoup(page.content, 'html.parser')
all_bricks = soup.find(class_='products-list products-list-v2')
items = all_bricks.find(class_='card product-card')
items_name = all_bricks.find(class_='product-card__title product-card__title-v2')
price_box = items.find("div", attrs={"class": "product-card__price-value"}) #Extra space removed
price = price_box.text #adding ".text"
print (price)

要获取所有名称和价格,只需直接搜索即可。

page = requests.get('https://www.wickes.co.uk/search?text=brick')
soup = BeautifulSoup(page.content, 'lxml')
names = [x.text.strip() for x in soup.find_all('a', {'class': 'product-card__title product-card__title-v2'})]
prices = [x.text.strip() for x in soup.find_all('div', {'class': 'product-card__price-value '})]
print(names[0], prices[0])

相关内容

  • 没有找到相关文章

最新更新