Search code examples
exceptionbeautifulsouptry-catchtweets

How to handle this exception by try catch ? AttributeError: 'NoneType' object has no attribute 'get_text'


enter image description here I want to fetch the trends as shown in the images but i am not able to fetch the tweets which has no tweet counts.(refer the image) below is the error i am getting for those tweets which has no tweet counts. Please let me know how to handle the this exception and print all the tweets.

    from bs4 import BeautifulSoup
    import requests
    URL = "https://trends24.in/india/"
    html_text=requests.get(URL)
    soup= BeautifulSoup(html_text.content,'html.parser')
    results = soup.find(id='trend-list')
    job_elems = results.find_all('li')
    for job_elem in job_elems:
            print(job_elem.find('a').get_text(), job_elem.find('span').get_text())

Solution

  • You could select for a shared parent then wrap the attempt to grab with tweet-count inside the try except

    from bs4 import BeautifulSoup
    import requests
    URL = "https://trends24.in/india/"
    html_text=requests.get(URL)
    soup= BeautifulSoup(html_text.content,'lxml')
    
    for i in soup.select('#trend-list li'):
        print(i.a.text)
        try:
            print(i.select_one('.tweet-count').text)
        except:
            print("no tweets")
    

    List of dicts:

    from bs4 import BeautifulSoup
    import requests
    URL = "https://trends24.in/india/"
    html_text=requests.get(URL)
    soup= BeautifulSoup(html_text.content,'lxml')
    
    results = []
    
    for i in soup.select('#trend-list li'):
        d = dict()
        d[i.a.text] = '' 
        try:
            val = i.select_one('.tweet-count').text
        except:
            val = "no tweets"
        finally:
            d[i.a.text] = val
            results.append(d)