首页 > 解决方案 > AttributeError:'NoneType' 对象在来自维基百科的 scrapimg 时没有属性 'findAll'

问题描述

from urllib.request import urlopen
from bs4 import BeautifulSoup
import re

html = urlopen("https://en.wikipedia.org/wiki/Kevin_Bacon")
bsobj = BeautifulSoup(html,'lxml')
for link in bsobj.find("div",{"id":"bodycontent"}).findAll("a", href = 
re.compile("^(/wiki/)((?!:).)*$")):
    if 'href' in link.attrs:
        print(link.attrs['href'])

当我运行此代码时,出现错误。这是我得到的错误;

Traceback(最近一次通话最后):文件“C:/Users/shubham/PycharmProjects/testday1/scrapping/lamdaexp.py”,第9行,bsobj.find中的链接(“div”,{“id”:“bodycontent "}).findAll("a", href = re.compile("^(/wiki/)((?!:).)*$")): AttributeError: 'NoneType' 对象没有属性 'findAll'

请帮我解决一下这个。这是在 youtube 视频教程中,它在教程视频中运行良好,而在我的 PyCharm 中显示此错误。

标签: python-3.xweb-scrapingattributeerror

解决方案


ID 是bodyContent而不是bodycontent。C!不是 c.


推荐阅读