【发布时间】:2020-03-04 15:21:49
【问题描述】:
我最近在这里发帖,但我无法解决问题,所以我再次询问。 我正在尝试抓取该网站的“近期销售”部分(所有地址):https://www.compass.com/agents/irene-vuong/
我的代码如下所示:
listings = []
for item in soup.findAll('a', {'class':'uc-listingCard-title'}):
listings.append(item.get_text(strip=True))
print(listings)
我的输出是:
['256-258 Wyckoff Street', '1320 Glenwood Road', '1473 East 55th Street', '145 Winter Avenue', '25-02 Brookhaven Avenue']
但预期结果是:
['256-258 Wyckoff Street', '1320 Glenwood Road', '1473 East 55th Street', '145 Winter Avenue', '25-02 Brookhaven Avenue', '352 94th Street', '1754 West 12th Street', '2283 E 23rd st', '2063 Brown Street, '3423 Avenue U', '2256 Stuart Street']
其中包含所有地址作为类名的相同
<a class="uc-listingCard-title" href="`````" data-tn="listingCard-label-address"> adress here </a>
我不明白为什么我的代码在类名相同时只获取第一部分而不是全部地址。
提前感谢您的帮助。
++++ 有建议:
for item in soup.findAll('div', attrs={'class': 'uc-listingCard-content'}):
new = item.find('a', attrs={'class': 'uc-listingCard-title'})
print(new.text)
我仍然只能获得当前的房源地址,而不是所有地址。
【问题讨论】:
-
可以分享一下HTML源代码的相关部分吗?请提供minimal reproducible example。
标签: python web-scraping beautifulsoup