Веб-парсинг Python Beautiful Soup 4 'статья'

Я пытаюсь сделать веб-скребок на python, используя веб-драйвер и красивый суп 4. Я пробую код на 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)

person sinage    schedule 09.07.2020    source источник


Ответы (1)


Вам нужно внести простые изменения в свой код:

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)

person Roman    schedule 09.07.2020