II337
II337

Reputation: 11

Trying to monitor a product from a webpage, doesnt work as intented

Very new to requests/bs4. I made a request to a webpage (a shop) and parsed it into a soup via bs4. If the soup contains the text "Sold Out" it should print me out "Sold Out", else "In Stock", but even though the body doesn't contain the word "Sold Out", it still prints "Sold Out"

I think there is a problem in formatting and I tried to change it to soup.body.findAll(text="Sold Out")

import requests
import time
from bs4 import BeautifulSoup


r = requests.get("https://www.starcowparis.com/shoes/2030-adidas-alphaedge-4d-m.html")
src = (r.content)
soup = BeautifulSoup(src, "lxml")
soup.find_all("div")



while True:
    soup.body.findAll(text="Sold Out")
    []
    print("Sold Out")
else:
    print("INSTOCK")

It always prints out "SOLD OUT" even though there is no "Sold Out" in the body

Upvotes: 1

Views: 51

Answers (2)

georg.dev
georg.dev

Reputation: 1263

The statement under while True will always be executed, because True will always be true no matter what.

What you want to do is using an if clause instead and check if the body contains any matches of the pattern "Sold Out":

if len(soup.body.findAll(text="Sold Out")) > 0:
    print("Sold Out")
else:
    print("INSTOCK")

Upvotes: 1

gmds
gmds

Reputation: 19895

That's because you have a while loop where you want an if block:

if soup.body.findAll(text="Sold Out"):
    print("Sold Out")

else:
    print("INSTOCK")

Upvotes: 1

Related Questions