Python吉林长春二手房源爬虫数据可视化系统设计与实现




import requests
from bs4 import BeautifulSoup
import pandas as pd
import matplotlib.pyplot as plt
 
# 定义一个函数来获取房源信息
def get_source_info(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    info_list = soup.select('.info-list li')
    info_dict = {}
    for info in info_list:
        key = info.select('span')[0].text
        value = info.select('a|span')[1].text if len(info.select('a|span')) > 1 else ''
        info_dict[key] = value
    return info_dict
 
# 定义一个函数来获取房源详细信息
def get_source_details(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    title = soup.select('.title-bar-01')[0].text
    info_list = soup.select('.house-parameter li')
    info_dict = {}
    for info in info_list:
        key = info.select('span')[0].text
        value = info.select('span')[1].text
        info_dict[key] = value
    return title, info_dict
 
# 定义一个函数来获取房源数据
def get_source_data(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    data_list = soup.select('.house-list-wrap .house-list-item')
    source_data = []
    for data in data_list:
        info_dict = get_source_info(data.select('a')[0]['href'])
        info_dict['title'] = data.select('.house-title')[0].text
        info_dict['price'] = data.select('.price')[0].text
        source_data.append(info_dict)
    return source_data
 
# 获取二手房数据
source_data = get_source_data('http://ershou.jilin.cn/ershoufang/')
df = pd.DataFrame(source_data)
 
# 数据可视化
plt.figure(figsize=(20, 10))
plt.subplot(1, 2, 1)
plt.scatter(df['area'], df['price'])
plt.xlabel('Area (平方米)')
plt.ylabel('Price (万元)')
plt.title('二手房面积与价格关系散点图')
 
plt.subplot(1, 2, 2)
plt.hist(df['price'], bins=50)
plt.xlabel('Price (万元)')
plt.ylabel('Count')
plt.title('二手房价格分布直方图')
 
plt.show()

这段代码首先定义了一个函数get_source_info来解析房源列表页的每条房源信息,然后定义了一个函数get_source_details来解析房源详情页的标题和详细信息。最后,定义了一个函数get_source_data来获取整个房源页的数据,并将其存储为DataFrame格式,以便进行数据可视化分析。代码中使用了matplotlib.pyplot库来绘制散点图和直方图,展示了房源面积与价格之间的关系以及房源价格的分布情况。

最后修改于:2024年08月25日 19:32

评论已关闭

推荐阅读

Vue中使用mind-map实现在线思维导图
2024年08月04日
VUE
Web前端最全Vue实现免密登录跳转的方式_vue怎么样不登录返回首页,最强技术实现
2024年08月04日
VUE
vue3 项目搭建教程(基于create-vue,vite,Vite + Vue)
2024年08月04日
VUE
Vue-颜色选择器实现方案——>Vue-Color( 实战*1+ Demo*7)
2024年08月04日
VUE
Vue项目卡顿慢加载?这些优化技巧告诉你!_vue数据多渲染卡顿
2024年08月04日
VUE
vue中的keep-alive详解与应用场景
2024年08月04日
VUE
Vue、React实现excel导出功能(三种实现方式保姆级讲解)
2024年08月04日
vue-office/docx插件实现docx文件预览
2024年08月04日
VUE
java调用js文件的两种方法(支持V8引擎)
2024年08月04日
JavaScript:解决计算精度问题/mathjs/bignumber.js/big.js/decimal.js
2024年08月04日
两周从爬虫小白变大神 _yjs_js_security_passport
2024年08月04日
JS笔记(对象、函数、数组)
2024年08月04日
Markdown.js:强大的纯JavaScript Markdown解析器
2024年08月04日
Vue项目:js模拟点击a标签下载文件并重命名,URL文件地址下载方法、请求接口下载文件方法总结。
2024年08月04日
vue 父组件怎么获取子组件里面的data数据
2024年08月04日
VUE
个人开发实现AI套壳网站快速搭建(Vue+elementUI+SpringBoot)
2024年08月04日
el-table 表格封装并改造实现单元格可编辑
2024年08月04日
none
nodejs环境下创建vue项目、SSH密钥登陆!!!
2024年08月04日
vue+quill+element-ui实现视频、图片上传及缩放保姆级教程,轻松使用富文本
2024年08月04日
【three.js】22. Imported Models导入模型
2024年08月04日