AttributeError: 'NoneType' object has no attribute 'text' - Beautifulsoup
·
Answer a question
I have an issue when extracting the text in a span in beautifulsoup:
data = requests.get("https://www.habitaclia.com/pisos-barcelona.htm?gclid=Cj0KCQiAkuP9BRCkARIsAKGLE8UM9G9s2MhPVtfXl2wo81qT62dqCwkVeV1zVSlPz_G1Nt0QcSoyKTYaAv55EALw_wcB")
soup = BeautifulSoup(data.text, "html5lib")
anuncis = soup.find_all("div", {"class":"list-item-info"})
data = []
for anunci in anuncis:
preu = [anunci.find("span", attrs={"itemprop": "price"}).text]
data.append(preu)
print(data)
I tried different parsers html, html5, xlml but none seems to work.
Any idea why I can't extract the text?
Answers
Seems like some of div.list-item-info have no span with itemprop=price inside. So, you need check them like this
import requests
from bs4 import BeautifulSoup
data=requests.get("https://www.habitaclia.com/pisos-barcelona.htm?gclid=Cj0KCQiAkuP9BRCkARIsAKGLE8UM9G9s2MhPVtfXl2wo81qT62dqCwkVeV1zVSlPz_G1Nt0QcSoyKTYaAv55EALw_wcB")
soup=BeautifulSoup(data.text, "lxml")
anuncis = soup.find_all("div", {"class":"list-item-info"})
data=[]
for anunci in anuncis:
preu = anunci.find("span", attrs={"itemprop":"price"})
if preu:
data.append([preu.text])
print(data)
Result
[['190.000 €'], ['156.371 €'], ['256.489 €'], ['218.000 €'], ['225.000 €'], ['480.000 €'], ['1.325.000 €'], ['439.000 €'], ['1.495.000 €'], ['339.000 €'], ['780.000 €'], ['549.000 €'], ['549.000 €'], ['535.000 €'], ['460.000 €']]
更多推荐

所有评论(0)