-1

我真的是 Python 的新手,从昨天开始我就尝试使用 Beautifulsoup抓取craigslist 帖子的位置(城市或 googlemap 信息)。

我尝试了一种在网站上找到的方法: Using Beautiful Soup to get data from non-class section

但是当我使用时:

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)

NameError: name 'addressText' is not defined在最后一行,我不明白为什么。

如果有人可以提供帮助或提供其他解决方案,我将不胜感激,

非常感谢,

4

1 回答 1

0

我认为问题在于您没有正确缩进代码。尝试将其修改为

for address in soup.findAll("div", {"class": "mapaddress"}):
        addressText = ''.join(address.findAll(text=True))  
        location.append(addressText)
于 2020-03-16T15:01:25.913 回答