属性错误:"NoneType"对象没有属性“父级" [英] Attribute Error:'NoneType' object has no attribute 'parent'

查看:322
本文介绍了属性错误:"NoneType"对象没有属性“父级"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from urllib.request import urlopen
from bs4 import BeautifulSoup
html= urlopen("http://www.pythonscraping.com/pages/page3.html")
soup= BeautifulSoup(html.read())
print(soup.find("img",{"src":"../img/gifts/img1.jpg"
}).parent.previous_sibling.get_text())

上面的代码可以正常工作,但是下面的代码不能正常工作,它给出了如上所述的属性错误.谁能告诉我原因?

The above code works fine but not the one below.It gives an attribute error as stated above. Can anyone tell me the reason?

from urllib.request import urlopen       
from bs4 import BeautifulSoup
html= urlopen("http://www.pythonscraping.com/pages/page3.html")
soup= BeautifulSoup(html.read())
price =soup.find("img",{"src=":"../img/gifts/img1.jpg"
}).parent.previous_sibling.get_text()
print(price)

谢谢! :)

推荐答案

如果比较第一个版本和第二个版本,您会注意到:

If you compare the first and the second version, you'll notice that:

第一: soup.find("img",{"src":"../img/gifts/img1.jpg"}).parent.previous_sibling.get_text()

  • 注意:"src"

第二: soup.find("img","src=":"../img/gifts/img1.jpg"}).parent.previous_sibling.get_text()

  • 注意:"src="

第二个代码返回Attribute Error:'NoneType' object has no attribute 'parent',因为它在提供的汤中找不到src=="../img/gifts/img1.jpg".

The second code returns Attribute Error:'NoneType' object has no attribute 'parent' because it couldn't find src=="../img/gifts/img1.jpg" in the provided soup.

因此,如果您删除第二个版本中的=,它应该可以工作.

So, if you remove the = in the second version, it should work.

顺便说一句,您应该明确地要使用哪个解析器,否则bs4将返回以下警告:

Btw, you should explicitly which parser you want to use, otherwise bs4 will return the following warning:

UserWarning:未明确指定解析器,因此我使用的是最好的解析器 此系统可用的HTML解析器("lxml").这通常不是 问题,但是如果您在其他系统或其他系统上运行此代码 虚拟环境,它可能会使用其他解析器并表现 不一样.

UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("lxml"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.

要消除此警告,请更改如下代码:

To get rid of this warning, change code that looks like this:

BeautifulSoup([您的标记])

BeautifulSoup([your markup])

对此:

BeautifulSoup([您的标记],"lxml")

BeautifulSoup([your markup], "lxml")

因此,如警告消息中所述,您只需要将soup = BeautifulSoup(html.read())更改为soup = BeautifulSoup(html.read(), 'lxml').

So, as stated in the warning message, you just have to change soup = BeautifulSoup(html.read()) to soup = BeautifulSoup(html.read(), 'lxml'), for example.

这篇关于属性错误:"NoneType"对象没有属性“父级"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆