2017-04-18 108 views
2
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()) 

上面的代碼工作正常,但不是下面的那個。它給出瞭如上所述的屬性錯誤。誰能告訴我原因?屬性錯誤:'NoneType'對象沒有屬性'parent'

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) 

謝謝! :)

+0

都得到$ 15.00 – Serge

+0

希望我能說的一樣。我曾嘗試重新啓動和一切,但同樣的錯誤。我會嘗試看看代碼一次。由於 – Xexus

回答

0

如果你比較第一和第二個版本,你會發現:

第一: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"

所以,如果你在第二個版本中刪除=,它應該工作。


順便說一句,你應該明確你想要使用的解析器,否則bs4將返回以下警告:

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'),例如。

+0

我很新的這一切。非常感謝!! – Xexus

相關問題