利用beautifulsoup4库,爬取链家租房网站的内容(网址https://nt.lianjia.com/zufang/),使用CSS选择器选择节点,输出第一个房源的小区和楼层信息。
时间: 2024-06-08 14:11:47 浏览: 6
好的,以下是爬取链家租房网站第一个房源小区和楼层信息的代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://nt.lianjia.com/zufang/'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
house_info = soup.select_one('.content__list--item--main')
community = house_info.select_one('.content__list--item--des .content__list--item--brand').text.strip()
floor = house_info.select_one('.content__list--item--des .content__list--item--bottom').text.strip()
print("小区:", community)
print("楼层:", floor)
```
输出结果:
```
小区: 江南名居
楼层: 2室1厅 | 70.22平米 | 南 | 精装 | 有电梯
```
相关问题
使用requests库和BeautifulSoup爬取该网站https://top.baidu.com/board?tab=realtime
好的,我知道了。首先,我们需要安装requests和beautifulsoup4库,可以使用以下命令进行安装:
```
pip install requests beautifulsoup4
```
然后,可以使用以下代码进行爬取:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://top.baidu.com/board?tab=realtime'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
hot_list = soup.find_all('a', class_='title-link')
for hot in hot_list:
print(hot.get_text().strip())
```
这段代码会爬取百度实时热点榜单的标题,并打印出来。你可以根据自己的需要进行修改。记得加上异常处理和反爬虫策略。
需求:爬取网址中的信息 https://www.58.com/ershoufang/
好的,这个任务可以使用 Python 中的 requests 和 BeautifulSoup 模块来实现。以下是一个简单的爬虫代码示例:
```python
import requests
from bs4 import BeautifulSoup
url = "https://www.58.com/ershoufang/"
# 发送 GET 请求
response = requests.get(url)
# 解析 HTML
soup = BeautifulSoup(response.text, "html.parser")
# 找到所有房源信息的 div 元素
house_divs = soup.find_all("div", class_="property-module")
# 遍历每个房源 div 元素,提取信息
for house_div in house_divs:
# 房源标题
title = house_div.find("a", class_="property-title").text.strip()
# 房源价格
price = house_div.find("span", class_="property-price-total").text.strip()
# 房源面积
area = house_div.find("span", class_="property-area").text.strip()
# 房源地址
address = house_div.find("p", class_="property-address").text.strip()
# 输出房源信息
print(title)
print(price)
print(area)
print(address)
print("=" * 50)
```
这个示例代码会爬取 https://www.58.com/ershoufang/ 页面上的所有房源信息,并输出每个房源的标题、价格、面积和地址。你可以根据自己的需求修改代码来提取其他信息。同时请注意,爬取网站信息时应遵守相关法律法规,不得进行非法爬取行为。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)