简体   繁体   中英

BeautifulSoup find_all() AttributeError: 'NoneType' object has no attribute 'a'

When I just do the soup.find() method I get the result that I want, it is when I try to loop and use find_all() when it goes wrong.

 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()

It prints a couple of results and then it throws an error as shown below:

Χρηματιστήριο: Τι δεν έχει τιμολογήσει ακόμα η αγορά – Δεν φτάνει η αντίδραση της 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'

I just want to print all of the article titles with the link underneath. Any suggestions would be appreciated

for article in find:
    try:
        
        headline = article.h3.a.text
        print(headline)
        link =article.h3.a['href']
        print(link)
        print()
    except AttributeError:
        pass
            

You can use try and except Blocks. It has printed 24 articles

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM