python bs4 不搜寻 di

2024-04-26 10:49:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我想刮https://www.edsurge.com/product-reviews/curriculum-products/math

当我这样做时:

site = 'https://www.edsurge.com/product-reviews/curriculum-products/math'
soup = bs.BeautifulSoup(urlopen(site),"html5lib")
print soup

我可以找到我要找的div <div class="browse-main p1">

但是,当我尝试使用以下方法查找它时:

for div in soup.findall('div',class_='browse-main p1'):
        print div

我得到这个错误(可能意味着它找不到div)。你知道吗

for div in soup.findall('div',class_='browse-main p1'):
TypeError: 'NoneType' object is not callable

我也试过使用soup.findall('div'),但是找不到。 我知道我可以用硒,但如果没有必要,我宁愿不要。我觉得奇怪的是,当我打印soup时,div出现了。你知道吗

有人知道发生了什么吗? 谢谢


Tags: httpsdivcommainwwwproductcurriculumclass
2条回答

正如@brunothe comment中提到的:

soup实现了__getattr__钩子(用于动态属性解析),但没有正确地为未知方法引发AttributeError,而是返回None。由于方法名为findAll(而不是findall),因此soup.findall实际上求值为None,这当然是不可调用的,因此会出现错误消息。你知道吗

另外,请注意^{}是bs3语法。bs4的语法是^{}。你知道吗

所以,你应该用

soup.find_all('div',class_='browse-main p1')

而不是

soup.findall('div',class_='browse-main p1')

findall是None,因此它是不可调用的。你知道吗

尝试:

for div in soup.findAll('div',class_='browse-main p1'):

相关问题 更多 >