mercredi 4 décembre 2019

BeautifulSoup --- if / else --- check if container contains another container then

My problem refers to the last part of the code ###Bottom container:

Below website contains 17 "productlist-item__bottom-container" of which 4 contain a "productlist-item__discount-text"

[https://www.nemlig.com/dagligvarer/husholdning/rengoering/opvaskemiddel/opvasketabs-pulver-til-maskine][1]

What I would like to do:

for all container in "productlist-item__bottom-container"

if "productlist-item__bottom-container" contains "productlist-item__discount-text"
  store value in a list

else
  store value "no text" in the list

Thanks for your help.

from bs4 import BeautifulSoup
import pandas as pd

desired_width = 320
pd.set_option("display.width", desired_width)
pd.set_option("display.max_columns", 30)

chrome_path = r"C:\Users\Sebas\Desktop\chromedriver_win32 (1)\chromedriver.exe"

url = "https://www.nemlig.com/dagligvarer/husholdning/rengoering/opvaskemiddel/opvasketabs-pulver-til-maskine"
browser = webdriver.Chrome(chrome_path)
browser.get(url)

import time
time.sleep(5)
html = browser.page_source
soup = BeautifulSoup(html, "html.parser")

###Get the descriptions
all_descriptions = []
containers = soup.find_all("div", {"class":"productlist-item__info"})
for container in containers:
    brand = container.text

    all_descriptions.append(brand)

###Get the product name
all_productnames = []
productnames = soup.find_all("div", {"class":"productlist-item__name"})

for product in productnames:
    productname = product.text

    all_productnames.append(productname)

###Get the base price
all_basePrices = []
basePrices = soup.find_all("div", {"class":"pricecontainer__base-price"})
for price in basePrices:
    x = price.text
    all_basePrices.append(x)


###Get promo price
all_promoPrices = []
promoPrices = soup.find_all("div", {"class":"pricecontainer__campaign-price"})
for promoprice in promoPrices:
    promoprice_ = promoprice.text
    all_promoPrices.append(promoprice_)


###Bottom container

all_texts =[]

bottom_containers = soup.find_all("div", {"class":"productlist-item__bottom-container"})

for container in bottom_containers:
    discountText = bottom_containers.find_all("div", {"class": "productlist-item__discount-text"})

    if discountText != None :
        text = discountText.text
        all_texts.append(text)

    else :
        all_texts.append("No text")

print(all_texts) 


Traceback (most recent call last):
  File "C:/Users/Sebas/PycharmProjects/DePivotize/venv/Selenium.py", line 58, in <module>
    discountText = bottom_containers.find_all("div", {"class": "productlist-item__discount-text"})
  File "C:\Users\Sebas\PycharmProjects\DePivotize\venv\lib\site-packages\bs4\element.py", line 1602, in __getattr__
    "ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key
AttributeError: ResultSet object has no attribute 'find_all'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?


  [1]: https://www.nemlig.com/dagligvarer/husholdning/rengoering/opvaskemiddel/opvasketabs-pulver-til-maskine

Aucun commentaire:

Enregistrer un commentaire