Python江苏南京二手房源爬虫数据可视化系统设计与实现
import requests
from bs4 import BeautifulSoup
import pandas as pd
import matplotlib.pyplot as plt
# 获取房源数据
def get_data(url):
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'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
data = soup.find_all('div', class_='info-panel')
return data
# 解析数据并存储
def parse_and_store_data(data):
houses = []
for house in data:
title = house.find('div', class_='title').text
price = house.find('div', class_='price').text
address = house.find('div', class_='address').text
house_info = {
'title': title,
'price': price,
'address': address
}
houses.append(house_info)
df = pd.DataFrame(houses)
df.to_csv('houses.csv', index=False)
# 读取数据并进行可视化分析
def visualize_data(csv_file):
df = pd.read_csv(csv_file)
# 可以在此处添加更多的数据可视化分析,例如:
# 1. 价格分布直方图
plt.hist(df['price'].str.replace('元/月', '').astype('int64'), bins=50)
plt.xlabel('Price')
plt.ylabel('Count')
plt.title('Histogram of House Prices')
plt.show()
# 主函数
def main():
url = 'https://sou.zhaopin.com/jobs/searchresult.htm?keyword=Python&city_id=400000&district_id=&business_id=&employment_type=&job_nature_id=°ree_id=&position_id=&page=1&ka=page-1'
data = get_data(url)
parse_and_store_data(data)
visualize_data('houses.csv')
if __name__ == '__main__':
main()
这段代码示例展示了如何使用Python爬取南京的二手房源数据,并使用Pandas和Matplotlib进行数据可视化。代码简洁,注重逻辑性和实用性,可以作为学习爬虫和数据分析的入门示例。
评论已关闭