1 課題背景
房地產(chǎn)是促進(jìn)我國(guó)經(jīng)濟(jì)持續(xù)增長(zhǎng)的基礎(chǔ)性、主導(dǎo)性產(chǎn)業(yè)。如何了解一個(gè)城市的房?jī)r(jià)的區(qū)域分布,或者不同的城市房?jī)r(jià)的區(qū)域差異。如何獲取一個(gè)城市不同板塊的房?jī)r(jià)數(shù)據(jù)?
本項(xiàng)目利用Python實(shí)現(xiàn)某一城市房?jī)r(jià)相關(guān)信息的爬取,并對(duì)爬取的原始數(shù)據(jù)進(jìn)行數(shù)據(jù)清洗,存儲(chǔ)到數(shù)據(jù)庫中,利用pyechart庫等工具進(jìn)行可視化展示。
選題指導(dǎo),項(xiàng)目分享:文章來源:http://www.zghlxwxcb.cn/news/detail-484685.html
https://gitee.com/yaa-dc/warehouse-1/blob/master/python/README.md文章來源地址http://www.zghlxwxcb.cn/news/detail-484685.html
2 數(shù)據(jù)爬取
2.1 爬蟲簡(jiǎn)介
網(wǎng)絡(luò)爬蟲是一種按照一定的規(guī)則,自動(dòng)地抓取萬維網(wǎng)信息的程序或者腳本。爬蟲對(duì)某一站點(diǎn)訪問,如果可以訪問就下載其中的網(wǎng)頁內(nèi)容,并且通過爬蟲解析模塊解析得到的網(wǎng)頁鏈接,把這些鏈接作為之后的抓取目標(biāo),并且在整個(gè)過程中完全不依賴用戶,自動(dòng)運(yùn)行。若不能訪問則根據(jù)爬蟲預(yù)先設(shè)定的策略進(jìn)行下一個(gè) URL的訪問。在整個(gè)過程中爬蟲會(huì)自動(dòng)進(jìn)行異步處理數(shù)據(jù)請(qǐng)求,返回網(wǎng)頁的抓取數(shù)據(jù)。在整個(gè)的爬蟲運(yùn)行之前,用戶都可以自定義的添加代理,偽 裝 請(qǐng)求頭以便更好地獲取網(wǎng)頁數(shù)據(jù)。
爬蟲流程圖如下:
實(shí)例代碼
# get方法實(shí)例
import requests #先導(dǎo)入爬蟲的庫,不然調(diào)用不了爬蟲的函數(shù)
response = requests.get("http://httpbin.org/get") #get方法
print( response.status_code ) #狀態(tài)碼
print( response.text )
2.2 房?jī)r(jià)爬取
累計(jì)爬取鏈家深圳二手房源信息累計(jì)18906條
- 爬取各個(gè)行政區(qū)房源信息;
- 數(shù)據(jù)保存為DataFrame;
相關(guān)代碼
from bs4 import BeautifulSoup
import pandas as pd
from tqdm import tqdm
import math
import requests
import lxml
import re
import time
area_dic = {'羅湖區(qū)':'luohuqu',
'福田區(qū)':'futianqu',
'南山區(qū)':'nanshanqu',
'鹽田區(qū)':'yantianqu',
'寶安區(qū)':'baoanqu',
'龍崗區(qū)':'longgangqu',
'龍華區(qū)':'longhuaqu',
'坪山區(qū)':'pingshanqu'}
# 加個(gè)header以示尊敬
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36',
'Referer': 'https://sz.lianjia.com/ershoufang/'}
# 新建一個(gè)會(huì)話
sess = requests.session()
sess.get('https://sz.lianjia.com/ershoufang/', headers=headers)
# url示例:https://sz.lianjia.com/ershoufang/luohuqu/pg2/
url = 'https://sz.lianjia.com/ershoufang/{}/pg{}/'
# 當(dāng)正則表達(dá)式匹配失敗時(shí),返回默認(rèn)值(errif)
def re_match(re_pattern, string, errif=None):
try:
return re.findall(re_pattern, string)[0].strip()
except IndexError:
return errif
# 新建一個(gè)DataFrame存儲(chǔ)信息
data = pd.DataFrame()
for key_, value_ in area_dic.items():
# 獲取該行政區(qū)下房源記錄數(shù)
start_url = 'https://sz.lianjia.com/ershoufang/{}/'.format(value_)
html = sess.get(start_url).text
house_num = re.findall('共找到<span> (.*?) </span>套.*二手房', html)[0].strip()
print('??{}: 二手房源共計(jì)「{}」套'.format(key_, house_num))
time.sleep(1)
# 頁面限制?? 每個(gè)行政區(qū)只能獲取最多100頁共計(jì)3000條房源信息
total_page = int(math.ceil(min(3000, int(house_num)) / 30.0))
for i in tqdm(range(total_page), desc=key_):
html = sess.get(url.format(value_, i+1)).text
soup = BeautifulSoup(html, 'lxml')
info_collect = soup.find_all(class_="info clear")
for info in info_collect:
info_dic = {}
# 行政區(qū)
info_dic['area'] = key_
# 房源的標(biāo)題
info_dic['title'] = re_match('target="_blank">(.*?)</a><!--', str(info))
# 小區(qū)名
info_dic['community'] = re_match('xiaoqu.*?target="_blank">(.*?)</a>', str(info))
# 位置
info_dic['position'] = re_match('<a href.*?target="_blank">(.*?)</a>.*?class="address">', str(info))
# 稅相關(guān),如房本滿5年
info_dic['tax'] = re_match('class="taxfree">(.*?)</span>', str(info))
# 總價(jià)
info_dic['total_price'] = float(re_match('class="totalPrice"><span>(.*?)</span>萬', str(info)))
# 單價(jià)
info_dic['unit_price'] = float(re_match('data-price="(.*?)"', str(info)))
# 匹配房源標(biāo)簽信息,通過|切割
# 包括面積,朝向,裝修等信息
icons = re.findall('class="houseIcon"></span>(.*?)</div>', str(info))[0].strip().split('|')
info_dic['hourseType'] = icons[0].strip()
info_dic['hourseSize'] = float(icons[1].replace('平米', ''))
info_dic['direction'] = icons[2].strip()
info_dic['fitment'] = icons[3].strip()
# 存入DataFrame
if data.empty:
data = pd.DataFrame(info_dic,index=[0])
else:
data = data.append(info_dic,ignore_index=True)
爬取過程
3 數(shù)據(jù)可視化分析
3.1 ECharts
ECharts(Enterprise Charts)是百度開源的數(shù)據(jù)可視化工具,底層依賴輕量級(jí)Canvas庫ZRender。兼容了幾乎全部常用瀏覽器的特點(diǎn),使它可廣泛用于PC客戶端和手機(jī)客戶端。ECharts能輔助開發(fā)者整合用戶數(shù)據(jù),創(chuàng)新性的完成個(gè)性化設(shè)置可視化圖表。支持折線圖(區(qū)域圖)、柱狀圖(條狀圖)、散點(diǎn)圖(氣泡圖)、K線圖、餅圖(環(huán)形圖)等,通過導(dǎo)入 js 庫在 Java Web 項(xiàng)目上運(yùn)行。
python安裝
pip install pyecharts
3.2 相關(guān)可視化圖表
房源面積-總價(jià)散點(diǎn)圖
scatter = (Scatter(init_opts=opts.InitOpts(theme='dark'))
.add_xaxis(data['hourseSize'])
.add_yaxis("房?jī)r(jià)", data['total_price'])
.set_series_opts(label_opts=opts.LabelOpts(is_show=False),
markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max", name="最大值"),]))
.set_global_opts(
legend_opts=opts.LegendOpts(is_show=False),
title_opts=opts.TitleOpts(title="深圳二手房 總價(jià)-面積 散點(diǎn)圖"),
xaxis_opts=opts.AxisOpts(
name='面積',
# 設(shè)置坐標(biāo)軸為數(shù)值類型
type_="value",
# 不顯示分割線
splitline_opts=opts.SplitLineOpts(is_show=False)),
yaxis_opts=opts.AxisOpts(
name='總價(jià)',
name_location='middle',
# 設(shè)置坐標(biāo)軸為數(shù)值類型
type_="value",
# 默認(rèn)為False表示起始為0
is_scale=True,
splitline_opts=opts.SplitLineOpts(is_show=False),),
visualmap_opts=opts.VisualMapOpts(is_show=True, type_='color', min_=100, max_=1000)
))
scatter.render_notebook()
各行政區(qū)均價(jià)
temp = data.groupby(['area'])['unit_price'].mean().reset_index()
data_pair = [(row['area'], round(row['unit_price']/10000, 1)) for _, row in temp.iterrows()]
map_ = (Map(init_opts=opts.InitOpts(theme='dark'))
.add("二手房均價(jià)", data_pair, '深圳', is_roam=False)
.set_series_opts(label_opts=opts.LabelOpts(is_show=True))
.set_global_opts(
title_opts=opts.TitleOpts(title="深圳各行政區(qū)二手房均價(jià)"),
legend_opts=opts.LegendOpts(is_show=False),
tooltip_opts=opts.TooltipOpts(formatter=':{c}萬元'),
visualmap_opts=opts.VisualMapOpts(min_=3, max_=10)
)
)
map_.render_notebook()
均價(jià)最高的10個(gè)地段
temp = data.groupby(['position'])['unit_price'].mean().reset_index()
data_pair = sorted([(row['position'], round(row['unit_price']/10000, 1))
for _, row in temp.iterrows()], key=lambda x: x[1], reverse=True)[:10]
bar = (Bar(init_opts=opts.InitOpts(theme='dark'))
.add_xaxis([x[0] for x in data_pair])
.add_yaxis('二手房均價(jià)', [x[1] for x in data_pair])
.set_series_opts(label_opts=opts.LabelOpts(is_show=True, font_style='italic'),
itemstyle_opts=opts.ItemStyleOpts(
color=JsCode("""new echarts.graphic.LinearGradient(0, 1, 0, 0,
[{
offset: 0,
color: 'rgb(0,206,209)'
}, {
offset: 1,
color: 'rgb(218,165,32)'
}])"""))
)
.set_global_opts(
title_opts=opts.TitleOpts(title="深圳二手房均價(jià)TOP 10地段"),
legend_opts=opts.LegendOpts(is_show=False),
tooltip_opts=opts.TooltipOpts(formatter=':{c}萬元'))
)
bar.render_notebook()
戶型分布
temp = data.groupby(['hourseType'])['area'].count().reset_index()
data_pair = sorted([(row['hourseType'], row['area'])
for _, row in temp.iterrows()], key=lambda x: x[1], reverse=True)[:10]
pie = (Pie(init_opts=opts.InitOpts(theme='dark'))
.add('', data_pair,
radius=["30%", "75%"],
rosetype="radius")
.set_global_opts(title_opts=opts.TitleOpts(title="深圳二手房 戶型分布"),
legend_opts=opts.LegendOpts(is_show=False),)
.set_series_opts(label_opts=opts.LabelOpts(formatter=": n5n3t3z%"))
)
pie.render_notebook()
詞云圖
word_list = []
stop_words = ['花園','業(yè)主','出售']
string = str(''.join([i for i in data['title'] if isinstance(i, str)]))
words = psg.cut(string)
for x in words:
if len(x.word)==1:
pass
elif x.flag in ('m', 'x'):
pass
elif x.word in stop_words:
pass
else:
word_list.append(x.word)
data_pair = collections.Counter(word_list).most_common(100)
wc = (WordCloud()
.add("", data_pair, word_size_range=[20, 100], shape='triangle')
.set_global_opts(title_opts=opts.TitleOpts(title="房源描述詞云圖"))
)
wc.render_notebook()
選題指導(dǎo),項(xiàng)目分享:
https://gitee.com/yaa-dc/warehouse-1/blob/master/python/README.md
到了這里,關(guān)于python畢業(yè)設(shè)計(jì) 大數(shù)據(jù)房?jī)r(jià)數(shù)據(jù)分析及可視化 房?jī)r(jià)分析的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!