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

丶灬走出姿态 提交于 2021-01-28 19:24:28

问题


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

回答1:


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)


来源:https://stackoverflow.com/questions/40615068/beautiful-soup-4-string-nonetype-object-is-not-callable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!