Beautiful Soup 4 .string()'NoneType'对象不可调用 [英] 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())
第一个打印语句(作为测试添加)有效-所以我知道BS4正常工作.第二条打印语句将引发:
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
推荐答案
BeautifulSoup的 .string
是一个属性,不是可调用的方法,并且当元素的内容没有单个,明确的字符串表示形式时(例如,当它包含多个子元素时),它的值是 None
-因此出现错误消息.
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.
将括号()
放在 h.string
之后,您将停止出错:
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
...尽管如果 h2
元素有多个子代,则结果可能不会特别有启发性.在这种情况下, .strings
和 .stripped_strings
派上用场:
... 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)
这篇关于Beautiful Soup 4 .string()'NoneType'对象不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!