Search code examples
pythonpython-3.xwebscraper

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("https://www.bazaraki.com/real-estate/houses-and-villas-rent/larnaka-district-larnaca/")
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()))

Solution

  • First, your last print statement has only two {} but you have three variables in the following format call, it's just a typo so it isn't the cause of the problem.

    Second, it looks like one of your variables is getting assigned a value of None by your call to the souped object. Just check the page's coding to make sure the values you are looking for are found in the right position. BeautifulSoup will return None if you call for a section that isn't there instead of raising an error.

    Since it looks like the exception is being raised while running the print call just check to make sure your three calls return values for your variable assignment.

    As for getting to the second div in a page, you may want to check out the next_sibling method call.