Sep-26-2021, 07:41 AM
You are doing something similar to this:
How you fix this depends on what you want to get from soup. From your description I think you would find all span and iterate through those items, comparing the item's class against your pattern. Something like this:
soup = {'A':1, 'B':2, 'C':3} class_ = 'B' for item in soup: mt2 = soup.get(class_) if mt2: print(mt2) else: print('There is no record')
Output:2
2
2
In this example and yours you will get a different item each time you iterate through soup, but soup either contains "class_" or not, and that is independent of the current item.How you fix this depends on what you want to get from soup. From your description I think you would find all span and iterate through those items, comparing the item's class against your pattern. Something like this:
from bs4 import BeautifulSoup with open("out_of_stock2.html", encoding="utf8") as fp: soup = BeautifulSoup(fp, 'html.parser') for item in soup.find('span'): if item['class_'] == "w_A w_C w_B mr1 mt1 ph1": print(item) else: print ('No match')