What's the better way to scrape Craigslist location or city with Beautifulsoup?

187 Views Asked by At

I'm really new on Python and I tried since yesterday to scrape location (city or googlemap information) of craigslist posts using Beautifulsoup.

I tried a way that I find on the site : Using Beautiful Soup to get data from non-class section

but when I use :

for url in (url_list):

    page2 = requests.get(url)
    soup = BeautifulSoup(page2.content, 'html.parser')
    for address in soup.findAll("div", {"class": "mapaddress"}):
            addressText = ''.join(address.findAll(text=True))

    location.append(addressText)

I have NameError: name 'addressText' is not defined in the last line and I can't understand why.

If someone can help or offer an other solution I'd really appreciate,

Many thanks,

1

There are 1 best solutions below

0
Jack Fleeting On

I think the problem is you didn't indent the code correctly. Try modifying it to

for address in soup.findAll("div", {"class": "mapaddress"}):
        addressText = ''.join(address.findAll(text=True))  
        location.append(addressText)