![](/img/trans.png)
[英]AttributeError: 'NoneType' object has no attribute 'find_all' Beautifulsoup
[英]BeautifulSoup find_all() AttributeError: 'NoneType' object has no attribute 'a'
當我只是執行 soup.find() 方法時,我得到了我想要的結果,這是當我嘗試循環並在出錯時使用 find_all() 時。
source = requests.get('https://www.mononews.gr/').text
soup = BeautifulSoup(source,'lxml')
find = soup.find_all('section',class_="story-package-module")
for article in find:
#headline
headline = article.h3.a.text
print(headline)
#link
link =article.h3.find('a',class_="story-package-module__story__headline-link")['href']
print(link)
print()
它打印了幾個結果,然后拋出一個錯誤,如下所示:
Χρηματιστήριο: Τι δεν έχει τιμολογήσει ακόμα η αγορά – Δεν φτάνει η αντίδραση της Aegean
https://www.mononews.gr/agores/chrimatistirio-ti-den-echi-tmologisi-akoma-i-agora-den-ftani-i-antidrasi-tis-aegean
Κάναμε σκι με CR-V (video)!
https://www.mononews.gr/auto/kaname-ski-me-crv-video
Kαραμανλής: Έγινε το πρώτο βήμα για την επέκταση του Μετρό παράλληλα με την λεωφόρο Κηφισίας
https://www.mononews.gr/oikonomia/karamanlis-egine-to-proto-vima-gia-tin-epektasi-tou-metro-parallila-me-tin-leoforo-kifisias
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-92-391906396ad6> in <module>
7
8 #headline
----> 9 headline = article.h3.a.text
10 print(headline)
11
AttributeError: 'NoneType' object has no attribute 'a'
我只想用下面的鏈接打印所有文章標題。 任何建議,將不勝感激
for article in find:
try:
headline = article.h3.a.text
print(headline)
link =article.h3.a['href']
print(link)
print()
except AttributeError:
pass
您可以使用try
和except
塊。 已打印24
篇文章
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.