需要一个类似与安居客的简化代码
时间: 2024-03-31 21:38:24 浏览: 17
好的,以下是一个简化版的Python购房选址分析代码,类似于安居客的功能:
```python
import requests
from bs4 import BeautifulSoup
# 设置请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
# 发送请求,获取房源信息
url = 'https://www.anjuke.com/sy-city.html'
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
# 获取城市列表
city_list = soup.find('div', {'class': 'city_list'}).find_all('a')
# 遍历城市列表,获取每个城市的房价信息
for city in city_list:
city_name = city.text.strip()
city_url = city['href']
city_response = requests.get(city_url, headers=headers)
city_soup = BeautifulSoup(city_response.text, 'html.parser')
price = city_soup.find('span', {'class': 'price'}).text.strip()
print(city_name, price)
```
在这个示例代码中,我们首先发送了一个请求,获取了安居客的城市列表。然后,我们遍历了每个城市,获取了该城市的房价信息,并打印输出了结果。
请注意,这只是一个简化版的代码,您需要根据实际情况进行修改,比如增加更多的房价相关信息,或者对数据进行更细粒度的分析。此外,也需要注意网站的反爬虫机制,以免被禁止访问。