Skip to content Skip to sidebar Skip to footer

Beautiful Soup Returns None

I have the following html code and i use beautiful soup to extract information. I want to get for example Relationship status: Relationship

Solution 1:

There is a special method get_text (or getText in old BeautifulSoup versions) to get the content of intricated tags. With your example:

>>> example.td.get_text(' ', strip=True)
'Relationship status: Relationship'

The first parameter is the separator to use.


Solution 2:

First of all, there is no need for all the list comprehensions; yours do nothing but copy the results, you can safely do without them.

There is no next sibling in your column (there is only one <td> tag), so it returns None. You wanted to get the .next attribute from the title (the <strong> tag) instead:

for table in soup.findAll('table', attrs = {'class':'box-content-list'}):
    for row in table.findAll('tr',attrs={'class':'first'}):
        for col in row.findAll('td'):
            title = col.strong
            status = title.nextSibling
            print title.text.strip(), status.strip()

which prints:

Relationship status: Relationship

for your example.


Post a Comment for "Beautiful Soup Returns None"