简体   繁体   English

Beautiful Soup 错误:“NoneType”对象没有属性“find_all”

[英]Beautiful Soup error: 'NoneType' object has no attribute 'find_all'

I am getting this error:我收到此错误:

---> 14 content = s.find_all('p')
AttributeError: 'NoneType' object has no attribute 'find_all'

While running the script below:运行以下脚本时:

r = requests.get('https://www.marketsandmarkets.com/Market-Reports/rocket-missile-market-203298804.html/')
soup = BeautifulSoup(r.content, 'html.parser')
s = soup.find('div', class_='entry-content') 
content = s.find_all('p')
print(content)   

It's working for some URLs, but for other URLs it gives an attribute error.它适用于某些 URL,但对于其他 URL,它会给出属性错误。 Not sure why this is happening.不知道为什么会这样。

When soup.find does not find anything, it returns None .soup.find没有找到任何东西时,它返回None Guard your s.find_all call with if s: or if s in not None: .使用if s:if s in not None:保护您的s.find_all呼叫。

So your code becomes:所以你的代码变成:

r = requests.get('https://www.marketsandmarkets.com/Market-Reports/rocket-missile-market-203298804.html/')
soup = BeautifulSoup(r.content, 'html.parser')
s = soup.find('div', class_='entry-content') 
if s is not None:
    content = s.find_all('p')
    print(content) 
else:
    print("Didn't find what I was looking for...")

or maybe:或者可能:

content = s.find_all('p') if s is not None else []
print(content)

Ref: https://crummy.com/software/BeautifulSoup/bs4/doc/#find参考: https ://crummy.com/software/BeautifulSoup/bs4/doc/#find

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NoneType object has no attribute find_all error using beautiful Soup - NoneType object has no attribute find_all error using beautiful Soup Python 错误:'NoneType' object 没有使用 Beautiful Soup 的属性 'find_all' - Python Error: 'NoneType' object has no attribute 'find_all' using Beautiful Soup AttributeError: 'NoneType' object 没有属性'find_all' Python Web 刮花 - AttributeError: 'NoneType' object has no attribute 'find_all' Python Web Scraping w/ Beautiful Soup Beautiful Soup AttributeError: 'NoneType' object has no attribute 'find_all' 即使网页结构相同 - Beautiful Soup AttributeError: 'NoneType' object has no attribute 'find_all' even though webpage is structured the same AttributeError:“函数”对象没有属性“ find_all”美丽汤 - AttributeError: 'function' object has no attribute 'find_all' Beautiful Soup 美丽的汤:“ ResultSet”对象没有属性“ find_all”吗? - Beautiful Soup: 'ResultSet' object has no attribute 'find_all'? Python - Beautiful Soup 4 - 'NavigableString' 对象没有属性 'find_all' - Python - Beautiful Soup 4 - 'NavigableString' object has no attribute 'find_all' Python属性错误:“ NoneType”对象没有属性“ find_all” - Python Attribute Error: 'NoneType' object has no attribute 'find_all' 属性错误“NoneType”对象没有属性“find_all” - attribute error 'NoneType' object has no attribute 'find_all' 错误:“。NoneType”对象没有.find的属性“ find_all” - Error: 'NoneType' object has no attribute 'find_all' for .find
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM