getting error :AttributeError: 'NoneType' object has no attribute 'text'

Viewed 43

this is my code:

urls = [] 
uni_names = [] 
page_urls_new = [] 
university_details = ["Name","Link","Location","Rank"]

#Determine 
#Colleges 

start_time = int(round(time.time())) 

for i in range(0, len(page_urls)): 
    page_url = page_urls[i] 
    page_text_soup = BeautifulSoup(extract_source(page_url), "lxml") 
    entries = int(page_text_soup.find('strong', attrs={'data-test-id': 'total-items'}).text) 
    entries = int((entries / 20) + (0 if 0 == entries % 20 else 1))
1 Answers

Formatted the code on my side and you get this error because:

page_text_soup.find('strong', attrs={'data-test-id': 'total-items'})

returns None in your case, so when you try to access text attribute it will raise the attribute error.

Make sure the text you search for is present in your data, or enclose that line in a try/except block to handle this type of scenario.

Related