BeautifulSoup - 检查属性/如果没有属性

时间:2016-04-23 21:00:14

标签: python web-scraping 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有/有价值,我想退货。    如果没有,则打印一个字符串“空”。

然后,迭代那个

B)如果不存在hotelhighprice,请寻找不同的值(我将其指定为变量。

当前错误消息 -

  File "main.py", line 35, in scraper
    hotelhighprice = hotelsoup.find('div', attrs={'class': 'pricing'}).text
AttributeError: 'NoneType' object has no attribute 'text'

3 个答案:

答案 0 :(得分:3)

您可以使用

text_value = getattr(hotelsoup.find('div', attrs={'class': 'pricing'}), "text", my_default_value)

答案 1 :(得分:1)

常见的代码模式是检查find()返回的是“truthy”:

price_elm = hotelsoup.find('div', attrs={'class': 'pricing'})
hotelhighprice = price_elm.get_text() if price_elm else "Empty"

或者,以扩展形式:

price_elm = hotelsoup.find('div', attrs={'class': 'pricing'})
if price_elm:
    hotelhighprice = price_elm.get_text() 
else: 
   hotelhighprice = "Empty"
   # or you may find a different element here
   # hotelhighprice = hotelsoup.find('div', class_="someotherclass").get_text()

答案 2 :(得分:1)

a = hotelsoup.find('div', attrs={'class': 'pricing'}) 
if a is None:
  # no pricing
else:
  price = a.text