我是一名初学者,正在编写一些简单的 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/