先说结论:拼写大小写错误。BeatufulSoup并没有findall(),但有findAll()。
而BS4推荐使用find_all(),这个findAll仅仅是为了兼容BS3,

事情是这样的:
soup = BeautifulSoup(html, 'html.parser')
trs = soup.find(id="grid").find("tbody").findAll("tr")
for tr in trs:
tds=tr.findall("td")BeautifulSoup 执行tds=tr.findall("td")报错: TypeError: 'NoneType' object is not callable

换成findChildren则成功:
soup = BeautifulSoup(html, 'html.parser')
trs = soup.find(id="grid").find("tbody").findAll("tr")
for tr in trs:
tds = tr.findChildren()一阵莫名奇怪,排查才发现,原来是大小写拼写错误。
在使用BeautifulSoup解析HTML时,由于将find_all误写为findAll,导致TypeError。正确方法是使用find_all,或者对于兼容BS3,使用findAll。当找到元素后,使用findChildren可以避免此类问题。
6万+

被折叠的 条评论
为什么被折叠?



