我有一个函数,从输入URL列表中删除信息。BeautifulSoup - 检查属性/如果没有属性
def scraper(inputlist):
for url in inputlist:
fullurl = baseurl + url
hotelresponse = requests.get(fullurl)
hotelsoup = BeautifulSoup(hotelresponse.text, "lxml")
hoteltitle = hotelsoup.find('div', attrs={'class': 'vcard'})
hotelhighprice = hotelsoup.find('div', attrs={'class': 'pricing'}).text
for H1 in hoteltitle:
hotelName = hoteltitle.find('h1').text
time.sleep(2)
return (hotelName, hotelhighprice, fullurl)
在这个特殊情况下,“hotelhighprice”可能并不总是有价值。
我想
A)如果hotelhighprice有/有一个价值,我想退货。 如果不是,则打印一个字符串“empty”。
然后,进行迭代上
B)如果hotelhighprice不存在,寻找一个不同的值(即我将指定为变量
当前的错误信息 -
File "main.py", line 35, in scraper
hotelhighprice = hotelsoup.find('div', attrs={'class': 'pricing'}).text
AttributeError: 'NoneType' object has no attribute 'text'