lundi 24 juillet 2017

Attribute always None

I've been trying to create a web scraper that collects the name, price and the district of an object on a website but cannot do anything with it because it raises an error: AttributeError: 'NoneType' object has no attribute 'strip'. What do I do? Help! Also how do I go to the second div? When I do districtcontainers = souped.find_all("div",{"class":"announcement-block-link") and then districtcontainers[0].div.div, it gives 0 output. How to solve that? Thank you very much for your attention and answers :).

import urllib.request as uReq from bs4 import BeautifulSoup as soup

url = uReq.urlopen("http://ift.tt/2tE5Uyv")
html = url.read()
souped = soup(html,"html.parser")
containers = souped.find_all("div",{"class":"announcement-block-text-container"})
districtcontainers = souped.find_all("div",{"class":"announcement-block__location"})
for container in containers:
    for districtcontainer in districtcontainers:
        title = container.a
        price = container.p
        district = districtcontainer
        print("{}:\n Costs: \n District:{}".format(title.string.strip(),price.string.strip(),district.string.strip()))



Aucun commentaire:

Enregistrer un commentaire