【问题标题】:Scraping multiple data with same class name using BeautifulSoup使用 BeautifulSoup 抓取具有相同类名的多个数据
【发布时间】: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)

我仍然只能获得当前的房源地址,而不是所有地址。

【问题讨论】:

标签: python web-scraping beautifulsoup


【解决方案1】:

试试:

from bs4 import BeautifulSoup

url = 'https://www.compass.com/agents/irene-vuong/'
url = requests.get(url)
tags = BeautifulSoup(url.text, 'html')
smaple_list=[]
for tag in tags.findAll('div', attrs={'class': 'uc-listingCard-content'}):
    new_tag = tag.find('a', attrs={'class': 'uc-listingCard-title'})
    print(new_tag.text)

【讨论】:

  • 嗨!!!它确实获得了所有地址,但我只想删除文本,所以我做了 .text 但我收到“ResultSet 对象没有属性'text'的属性错误。您可能将项目列表视为单个项目。当您打算调用 find() 时,您是否调用了 find_all()?"。我尝试“查找”但我得到不同的错误,我需要做什么?提前非常感谢您!
  • 所以我仍然得到当前的列表,但不是所有的销售列表。我想获取当前列表之后的“最近销售”地址...
  • 你还在吗?
猜你喜欢
  • 1970-01-01
  • 2023-03-05
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 2015-04-02
相关资源
最近更新 更多