Search code examples
pythonparsingbeautifulsoupattributeerror

AttributeError: 'NoneType' object has no attribute 'text' beautifulsoup python


import bs4,requests, re

#Get epsiode webpage
epPage = requests.get('http://www.friends-tv.org/zz101.html')
epPage.raise_for_status()

#use the page in bs4
soup = bs4.BeautifulSoup(epPage.text, 'lxml')
results = soup.find_all('dt')

#Populate the list
quotes = []
for result in results:
    character = result.find('b').text
    speech = result.contents[1][1:-2]
    quotes.append((character,speech))

print (quotes)`

I'm trying to get a list of quotes and the character who said it from this site: http://www.friends-tv.org/zz101.html. However, I receive the error:

Traceback (most recent call last):
  File "/Users/yusufsohoye/pythoncode/Friends1.py", line 16, in <module>
    character = result.find('b').text
AttributeError: 'NoneType' object has no attribute 'text' 

It works when i isolate each dt item in the results list however not when i try to parse the whole page and build the list.

Thanks


Solution

  • This should help.

    import bs4,requests, re
    
    #Get epsiode webpage
    epPage = requests.get('http://www.friends-tv.org/zz101.html')
    epPage.raise_for_status()
    
    #use the page in bs4
    soup = bs4.BeautifulSoup(epPage.text, 'lxml')
    results = soup.find_all('dt')
    
    #Populate the list
    quotes = []
    for result in results:
        character = result.find('b')
        if character:     #Check Condition to see if character in dt tag
            speech = result.contents[1][1:-2]
            squotes.append((character,speech))
    
    print(quotes)