html - 尝试使用 Python-3.7 抓取 html 的特定部分,但它返回 "None"

标签 html web-scraping python-3.7

我是一名初学者,正在编写一些简单的 Python 代码来从网页中抓取数据。我找到了我想要抓取的 html 的确切部分,但它一直返回“无”。它适用于网页的其他部分,但不适用于这一特定部分

我正在使用 BeautifulSoup 来解析 html,并且由于我可以抓取一些代码,所以我假设我不需要使用 Selenium。但我仍然找不到如何抓取一个特定部分。

这是我编写的 Python 代码:

import requests

from bs4 import BeautifulSoup


url = 'https://www.rent.com/new-york/tuckahoe-apartments?page=2'

response = requests.get(url)

html_soup = BeautifulSoup(response.text, 'html.parser')

apt_listings = html_soup.find_all('div', class_='_3RRl_')
print(type(apt_listings))
print(len(apt_listings))

first_apt = apt_listings[0]

first_apt.a

first_add = first_apt.a.text

print(first_add)


apt_rents = html_soup.find_all('div', class_='_3e12V')
print(type(apt_rents))
print(len(apt_rents))

first_rent = apt_rents[0]

print(first_rent)

first_rent = first_rent.find('class', attrs={'data-tid' : 'price'})

print(first_rent)

这是 CMD 的输出:

<class 'bs4.element.ResultSet'>
30
address not disclosed
<class 'bs4.element.ResultSet'>
30
<div class="_3e12V" data-tid="price">$2,350</div>
None

“地址未公开”是正确的,已成功抓取。 我想凑够 2,350 美元,但它一直返回“无”。我想我已经接近成功了,但我似乎无法拿到 2,350 美元。非常感谢任何帮助。

最佳答案

你需要像这样使用 BeautifulSoup 的属性 .text 而不是 .find() :

first_rent = first_rent.text

就这么简单。

关于html - 尝试使用 Python-3.7 抓取 html 的特定部分,但它返回 "None",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55636380/

相关文章:

r - 用 r 抓取 ajax 站点

python - 从数据流而不是文件加载图像

javascript - 为什么我的innerHTML会覆盖父类的样式属性?我该如何解决这个问题?

html - CSS 居中选项卡

html - 如何使用flex根据内容长度设置宽度

javascript - 错误 json 响应中出现意外的标记 <

python - 尝试用 selenium/beautiful soup 提取动态表(url 不变)

python - 如何使用 python 删除 webscrape 名称的重音符号

python - 如何使用dask dataframe中的rank函数?

tensorflow - LSTM:Python 3.7.4 的 Keras