Search code examples
pythonpython-3.xnonetypebeautifulsoup

Beautiful Soup 4 .string() 'NoneType' object is not callable


from bs4 import BeautifulSoup
import sys

soup = BeautifulSoup(open(sys.argv[2]), 'html.parser')
print(soup.prettify)

if sys.argv[1] == "h":
    h2s = soup.find_all("h2")
    for h in h2s:
        print(h.string())

The first print statement (added as a test) works - so I know BS4 is working and everything. The second print statement throws:

File "sp2gd.py", line 40, in <module>
    print(h.string())
TypeError: 'NoneType' object is not callable

Solution

  • BeautifulSoup's .string is a property, not a callable method, and when there's no single, unambiguous string representation of the contents of the element (for instance, when it contains multiple child elements), its value is None – hence the error message.

    Lose the parentheses () after h.string and you'll stop getting errors:

    if sys.argv[1] == "h":
        h2s = soup.find_all("h2")
        for h in h2s:
            print(h.string)  # no parentheses
    

    ... although if the h2 elements have multiple children, the results might not be especially illuminating. In that case, .strings and .stripped_strings come in handy:

    if sys.argv[1] == "h":
        h2s = soup.find_all("h2")
        for h in h2s:
            for s in h.stripped_strings:
                print(s)