python-3.x - 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 中显示此错误。
解决方案
ID 是bodyContent
而不是bodycontent
。C!不是 c.
推荐阅读
- pandas - 用于神经网络的 jupyter notebook、keras、tensorflow 中的 ValueError
- dart - 如何根据颤动中的触摸事件移动custompainter
- java - 来自json响应的Gson返回空数组列表
- javascript - 如何以角度获得图像的扩展?
- python - 优化图搜索算法
- amazon-web-services - AWS Transit Gatway 连接一个区域中的多个 VPC(新 AWS 服务)
- c# - 如何在列表中搜索给定项目的列表是否以相同的顺序存在
- android - 在 ConstraintLayout 中等距分布视图
- c# - 访问字符串的索引,但它没有给出正确的答案
- linux - Windows 等效于 "> /dev/null &"