Question Details

No question body available.

Tags

python selenium-webdriver beautifulsoup

Answers (1)

Accepted Answer Available
Accepted Answer
June 14, 2026 Score: 1 Rep: 151,633 Quality: High Completeness: 60%

When Pettag2 is empty then it doesn't run loop for PetotherPrice in Pettag2: and it doesn't run line Petprice2 = ... so it doesn't create this variable - and this makes problem when you try to display Petprice2 after loop.

The same problem can be with Petprice1 when Pettag1 is empty.


But it makes more sense to display it inside loop - so it needs indentation.
And then this problem is resolved.

If you would use 4 spaces for indentations then it could be better visible.

# - before first loop -

Pettag1 = Petdoc.select("p.textj98bt1.textsize2xlj98bt55.textnowrapj98bt7.textweightboldj98bt83.textstylenormalj98bt95.textdecorationnormalj98bt104.infopricecountilro6_87, span.itemprop.price")

- first loop -

for PetbestPrice in Pettag1: Petprice1 = float(PetbestPrice.text.replace("$", "")) pets.append(Petprice1) print("Petprice1:", Petprice1) #