Search code examples
pythonbeautifulsoupattributeerror

Beautifullsoup Attribute Error


Stupid problem...

for page in range(xxx, yyy):
url =  "%s%d" % (program_url, page)
opener = urllib2.build_opener() 
response = opener.open(url)
soup = BeautifulSoup(response)

print soup.find("strong").text

Sometimes the page don't contain any strong balise, so I have:

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

How to avoid it?


Solution

  • change your print statement to:

    if soup.find("strong"):
        print soup.find("strong").text