[英]AttributeError: 'NoneType' object has no attribute 'findAll' - Python
[英]python 'NoneType' object has no attribute 'findAll'
print(bsObj.find(id="mv-content-text").findAll("p")[0])
我用python3.6練習scrapy。 代碼來自Pyhon的Web Scraping。 為什么不能使用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湯中不存在的標記
>>> other = soup.find(id="non-existant")
>>> other
>>> type(other)
NoneType
>>> hasattr(other, 'findAll')
False
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.