我正在尝试用python做一个网络爬行器,使用web驱动程序和漂亮的SOUP4。我在decathlon.fr上尝试了代码,问题是包含产品的类是一个“文章”类。当我启动这个程序时,它不需要任何东西,因为我需要这个类是一个“div”类或者“a”类。
from bs4 import BeautifulSoup
driver = webdriver.Chrome("chromedriver.exe")
products=[] #List to store name of the product
prices=[] #List to store price of the product
ratings=[] #List to store rating of the product
driver.get("https://www.decathlon.fr/search?Ntt=t+shirt+anti+uv+b%C3%A9b%C3%A9")
content = driver.page_source
soup = BeautifulSoup(content)
for a in soup.findAll('article', attrs={'class':'dkt-product.js-product-slider-init.product-printed'}):
name=a.find('a', attrs={'class':'dkt-product__title__wrapper'})
price=a.find('div', attrs={'class':'dkt-product__price'})
#rating=a.find('div', attrs={'class':'hGSR34 _2beYZw'})
rating=a.find('span', attrs={'itemprop':'name'})
products.append(name)
prices.append(price)
ratings.append(rating)
print(products)
print(prices)
print(ratings)发布于 2020-07-09 20:16:35
您需要在代码中进行简单的更改:
soup = BeautifulSoup(content, 'lxml') # here add 'lxml'
for a in soup.findAll('article', attrs={'class':'dkt-product js-product-slider-init product-printed'}): # here, removed dots
name= a.find('a', attrs={'class':'dkt-product__title__wrapper'})
price=a.find('div', attrs={'class':'dkt-price__cartridge'}) # here
#rating=a.find('div', attrs={'class':'hGSR34 _2beYZw'})
rating=a.find('span', attrs={'itemprop':'ratingValue'}) # here
products.append(name)
prices.append(price)
ratings.append(rating)https://stackoverflow.com/questions/62814238
复制相似问题