2017-06-17 78 views
0

我是Python的新手,所以任何幫助,將不勝感激。我有一個使用beautifulsoup的網絡爬蟲。它可以工作,但在下面它會返回錯誤「無類型對象沒有屬性」。我知道這意味着它已經遇到了一個沒有條目的頁面。 如何阻止此錯誤並使其返回所有其他有條目的頁面。網絡爬蟲中的一些頁面有條目,有些則是空白的。沒有類型的對象在webcrawler中沒有屬性錯誤Python

bbb = re.compile('First listed') 
    next_s = soup.find(text=bbb).parent.parent.get_text(strip=True) 

感謝

回答

1
bbb = re.compile('First listed') 
next_s = soup.find(text=bbb) 
if next_s is not None: 
    # node exists 
else: 
    # node does not exists 
+0

感謝我該放什麼在 '如果next_s不無:'? – hello11

+0

取決於您的腳本的邏輯。如果你有一個鏈接列表,並在'for'循環中遍歷它,那麼使用'continue'來進行下一次迭代。 – Linch

+0

我對編碼相當陌生,所以我不太清楚這意味着什麼。 它沒有在抓取工具中的很多鏈接,我的大部分代碼如下:我會在哪裏放置for和continue語句? 防守get_single_item_data(item_url): source_code = requests.get(item_url) plain_text = source_code.text 湯= BeautifulSoup(plain_text) BBB = re.compile( '第一列') next_s = soup.find(文本= bbb) writer.writerow([地址,價格,臥室,描述,出售,next_s,next_ss]) – hello11

相關問題