2016-04-23 30 views
0

我有一个函数,从输入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' 

回答

3

您可以使用

text_value = getattr(hotelsoup.find('div', attrs={'class': 'pricing'}), "text", my_default_value) 
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() 
1
a = hotelsoup.find('div', attrs={'class': 'pricing'}) 
if a is None: 
    # no pricing 
else: 
    price = a.text