python 'NoneType' 对象没有属性 'findAll'
python 'NoneType' object has no attribute 'findAll'
print(bsObj.find(id="mv-content-text").findAll("p")[0])
我用python3.6练习scrapy。代码来自本书,Web Scraping with Pyhon。为什么不能使用 find.().findAll()
您的 find(...)
已返回 None,因为在 bsObj 中找不到带有 id=mv-content-text
的标签。
您只能对 bs4 对象调用 findAll
。您可以使用 type
和 hasattr
的组合来探索 REPL
中的返回值
>>> from bs4 import BeautifulSoup
>>> doc = ['<html><head><title>Page title</title></head>',
... '<body><p id="firstpara" align="center">This is paragraph <b>one</b>.',
... '<p id="secondpara" align="blah">This is paragraph <b>two</b>.',
... '</html>']
...
>>> soup = BeautifulSoup(''.join(doc), "lxml")
>>> tag = soup.find(id="firstpara")
>>> tag
<p align="center" id="firstpara">This is paragraph <b>one</b>.</p>
>>> type(tag)
bs4.element.Tag
>>> hasattr(tag, 'findAll')
True
尝试相同,但带有 HTML soup
中不存在的标签
>>> other = soup.find(id="non-existant")
>>> other
>>> type(other)
NoneType
>>> hasattr(other, 'findAll')
False
print(bsObj.find(id="mv-content-text").findAll("p")[0])
我用python3.6练习scrapy。代码来自本书,Web Scraping with Pyhon。为什么不能使用 find.().findAll()
您的 find(...)
已返回 None,因为在 bsObj 中找不到带有 id=mv-content-text
的标签。
您只能对 bs4 对象调用 findAll
。您可以使用 type
和 hasattr
的组合来探索 REPL
>>> from bs4 import BeautifulSoup
>>> doc = ['<html><head><title>Page title</title></head>',
... '<body><p id="firstpara" align="center">This is paragraph <b>one</b>.',
... '<p id="secondpara" align="blah">This is paragraph <b>two</b>.',
... '</html>']
...
>>> soup = BeautifulSoup(''.join(doc), "lxml")
>>> tag = soup.find(id="firstpara")
>>> tag
<p align="center" id="firstpara">This is paragraph <b>one</b>.</p>
>>> type(tag)
bs4.element.Tag
>>> hasattr(tag, 'findAll')
True
尝试相同,但带有 HTML soup
中不存在的标签>>> other = soup.find(id="non-existant")
>>> other
>>> type(other)
NoneType
>>> hasattr(other, 'findAll')
False