亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Beautiful Soap 查找/嘗試可能存在“NoneType”錯誤的不同類元素

Beautiful Soap 查找/嘗試可能存在“NoneType”錯誤的不同類元素

人到中年有點甜 2023-07-11 15:40:26
我正在使用 beautiful soup 來從 url 中獲取元素。頁面上有些元素的類名稱會根據值和百分比(向上、向下、平坦)而變化。顯然,如果我嘗試soup.find('div', class_='down-indicator')并且這是一個積極的指標,我將收到如下錯誤:AttributeError: 'NoneType' object has no attribute 'find'。我需要一種 Pythonic 方式來循環或嘗試 3 個可能的類選項,而不會出現錯誤破壞我的代碼。我也嘗試res = soup.find('div', class_='down-indicator')過執行 aif res is None...但它不會返回None它會返回錯誤。任何信息和學習將不勝感激!預先感謝您,這是我對某些代碼的糟糕嘗試:根據元素的不同,這 2 個 url 對于同一區域具有不同的類名稱。url = 'http://www.stockx.com/adidas-yeezy-boost-380-lmnte-infants'url = 'http://www.stockx.com/adidas-yeezy-boost-350-oxford-tan'這些是find()元素的確切函數,因為它可以向上、向下或平坦:soup.find('div', class_='last-sale-value market-dir market-dir-down').find('div', class_='dollar').textsoup.find('div', class_='last-sale-value market-dir market-dir-up').find('div', class_='dollar').textsoup.find('div', class_='last-sale-value market-dir market-dir-static').find('div', class_='dollar').textdriver = webdriver.Firefox()driver.get(url)content = driver.page_sourcesoup = BeautifulSoup(content, 'lxml')try:    result = soup.find('div', class_='up-indicator').text    result = soup.find('div', class_='down-indicator').text    result = soup.find('div', class_='flat-indicator').textexcept:    pass
查看完整描述

1 回答

?
千萬里不及你

TA貢獻1784條經驗 獲得超9個贊

在這兩種情況下,解決方案都相當簡單:

soup.select_one('div.dollar').text

輸出是相關頁面的目標美元金額。


查看完整回答
反對 回復 2023-07-11
  • 1 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號