Attribute Error:'NoneType' object has no attribute 'parent'
Attribute Error:'NoneType' object has no attribute 'parent'
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())
上面的代码工作正常,但 below.It 的代码没有给出如上所述的属性错误。谁能告诉我原因吗?
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)
谢谢! :)
如果比较第一个和第二个版本,您会发现:
第一个: 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="
第二个代码returns Attribute Error:'NoneType' object has no attribute 'parent'
因为在提供的汤中找不到src=="../img/gifts/img1.jpg"
。
因此,如果您删除第二个版本中的 =
,它应该可以工作。
顺便说一句,你应该明确你想使用哪个解析器,否则 bs4
将 return 以下警告:
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([your markup])
to this:
BeautifulSoup([your markup], "lxml")
因此,如警告消息中所述,您只需将 soup = BeautifulSoup(html.read())
更改为 soup = BeautifulSoup(html.read(), 'lxml')
,例如。
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())
上面的代码工作正常,但 below.It 的代码没有给出如上所述的属性错误。谁能告诉我原因吗?
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)
谢谢! :)
如果比较第一个和第二个版本,您会发现:
第一个: 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="
第二个代码returns Attribute Error:'NoneType' object has no attribute 'parent'
因为在提供的汤中找不到src=="../img/gifts/img1.jpg"
。
因此,如果您删除第二个版本中的 =
,它应该可以工作。
顺便说一句,你应该明确你想使用哪个解析器,否则 bs4
将 return 以下警告:
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([your markup])
to this:
BeautifulSoup([your markup], "lxml")
因此,如警告消息中所述,您只需将 soup = BeautifulSoup(html.read())
更改为 soup = BeautifulSoup(html.read(), 'lxml')
,例如。