2016-04-23 64 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