I'm trying to scrape a real estate website in Lebanon, some properties have a description of their available amenities and some don't.
After storing the links to scrape in a excel column, I iterate over them to access them and extract the available amenities for that property.
But some properties are Lands for example, and those don't have any amenities, so when my code runs into that case, it raises a "Attribute Error".
The code below is with the try and except scheme, but what I want is to set an "no amenities" value in the list where I store my amenities soupObject when there are none.
Any help would be much appreciated,
amenity_soupObject=[]
for cellObj in sheet['A']: ##here the A represents the column name
try:
link = cellObj.value
ress = requests.get(link)
time.sleep(0.2)
soup_finder = bs4.BeautifulSoup(ress.text)
propAmenities=soup_finder.find("div", class_="amenities_container")
propAmenities1 = bs4.BeautifulSoup(propAmenities.text)
amenity_soupObject.append(propAmenities1)
print(propAmenities1)
except: "AttributeError"
amenity_soupObject.append("no amenities here")
Aucun commentaire:
Enregistrer un commentaire