前言
嗨嘍~大家好呀,這里是魔王吶 ? ~!
環(huán)境使用:
-
Python 3.8
-
jupyter --> pip install jupyter notebook
-
pycharm 也可以
模塊使用:
-
requests >>> pip install requests 數(shù)據(jù)請求模塊
-
parsel >>> pip install parsel 數(shù)據(jù)解析模塊
-
csv 內置模塊
第三方模塊安裝:
-
win + R 輸入 cmd 點擊確定, 輸入安裝命令 pip install 模塊名 (pip install requests) 回車
-
在pycharm中點擊Terminal(終端) 輸入安裝命令
如果出現(xiàn)爆紅, 可能是因為 網(wǎng)絡連接超時, 可切換國內鏡像源,命令如下:
pip install -i https://pypi.doubanio.com/simple/ requests
python資料、源碼、教程\福利皆: 點擊此處跳轉文末名片獲取
python技術實現(xiàn): <基本流程步驟>
-
發(fā)送請求, 模擬瀏覽器對于url地址發(fā)送請求
請求鏈接地址: https://cs.****.com/ershoufang/
-
獲取數(shù)據(jù), 獲取響應數(shù)據(jù)
獲取數(shù)據(jù): 網(wǎng)頁源代碼 <整個網(wǎng)頁數(shù)據(jù)內容>
-
解析數(shù)據(jù), 提取我們需要數(shù)據(jù)
提取數(shù)據(jù): 房源基本信息
-
保存數(shù)據(jù), 把數(shù)據(jù)保存本地文件
保存數(shù)據(jù): csv表格文件當中
-
多頁數(shù)據(jù)采集
代碼展示
獲取數(shù)據(jù):
# 導入csv模塊
import csv
# 創(chuàng)建文件
f = open('data.csv', mode='w', encoding='utf-8', newline='')
# 添加字段名
csv_writer = csv.DictWriter(f, fieldnames=[
'標題',
'小區(qū)',
'區(qū)域',
'總價',
'單價',
'戶型',
'面積',
'朝向',
'裝修',
'樓層',
'年份',
'建筑結構',
'詳情頁'
])
# 寫表頭
csv_writer.writeheader()
# 導入數(shù)據(jù)解析模塊 第三方模塊, 需要安裝
import parsel
# 導入數(shù)據(jù)請求模塊 第三方模塊 需要安裝 pip install requests
import requests
for page in range(1, 101):
# 請求鏈接
url = f'https://cs.lianjia.com/ershoufang/pg{page}/'
# 偽裝瀏覽器 --> headers 請求頭
headers = {
# User-Agent 用戶代理 表示瀏覽器基本身份信息
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
}
# 發(fā)送請求
response = requests.get(url, headers=headers)
html_data = response.text # <Response [200]> 響應對象 表示請求成功
# 把獲取下來 html字符串數(shù)據(jù)內容 <html_data>, 轉成可解析對象
selector = parsel.Selector(html_data)
"""
selector <Selector xpath=None data='<html class=""><head><meta http-equiv...'> 選擇器對象
selector.css() 根據(jù)標簽屬性提取數(shù)據(jù)內容
selector.xpath()
"""
# 提取所有l(wèi)i標簽里面房源信息 --> 返回列表, 列表里面元素是選擇器對象
lis = selector.css('.sellListContent li .info')
# for循環(huán)遍歷, 提取出來, 然后也可以使用css語法或者xpath語法 提取具體數(shù)據(jù)內容
for li in lis:
"""
get() 獲取第一個標簽數(shù)據(jù)內容 <返回字符串>
getall() 獲取所有標簽數(shù)據(jù)內容 <返回列表>
strip() 去除字符串左右兩端空格
replace('元/平', '') 字符串替換, 把字符串當中 元/平 替換成 '' --> 相當于刪除
split(' | ') 字符串分割犯法, 把字符串以 | 作為切割, 分割成列表
"""
title = li.css('.title a::text').get() # 標題
info = li.css('.positionInfo a::text').getall()
area = info[0].strip() # 列表第一個元素 小區(qū)
area_1 = info[1] # 區(qū)域
totalPrice = li.css('.totalPrice span::text').get() # 總價
unitPrice = li.css('.unitPrice span::text').get().replace('元/平', '') # 單價
houseInfo = li.css('.houseInfo::text').get().split(' | ')
"""
有年份, 列表元素就是7個, 沒有年份列表元素6個
"""
house_type = houseInfo[0] # 戶型
house_area = houseInfo[1] # 面積
face = houseInfo[2] # 朝向
house = houseInfo[3] # 裝修
fool = houseInfo[4] # 樓層
if len(houseInfo) == 7:
date = houseInfo[5] # 年份
house_1 = houseInfo[6] # 建筑結構
elif len(houseInfo) == 6:
date = '未知'
house_1 = houseInfo[-1] # 建筑結構
href = li.css('.title a::attr(href)').get() # 詳情頁
# 保存字典里面
dit = {
'標題': title,
'小區(qū)': area,
'區(qū)域': area_1,
'總價': totalPrice,
'單價': unitPrice,
'戶型': house_type,
'面積': house_area,
'朝向': face,
'裝修': house,
'樓層': fool,
'年份': date,
'建筑結構': house_1,
'詳情頁': href,
}
# csv_writer.writerow(dit)
# print(dit)
# print(title, area, area_1, totalPrice, unitPrice, house_type, house_area, face, house, fool, date, house_1, sep='|')
數(shù)據(jù)可視化:
import pandas as pd
# 讀取數(shù)據(jù)
df = pd.read_csv('data.csv')
df.head()
house_type_num = df['戶型'].value_counts().to_list()
house_type = df['戶型'].value_counts().index.to_list()
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.globals import CurrentConfig, NotebookType
CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB
c = (
Pie()
.add("", [list(z) for z in zip(house_type, house_type_num)])
.set_global_opts(title_opts=opts.TitleOpts(title="二手房源戶型"))
.set_series_opts(label_opts=opts.LabelOpts(formatter=": {c}"))
)
c.load_javascript()
c.render_notebook()
house_num = df['裝修'].value_counts().to_list()
house_type_1 = df['裝修'].value_counts().index.to_list()
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.globals import CurrentConfig, NotebookType
CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB
c = (
Pie()
.add("", [list(z) for z in zip(house_type_1, house_num)])
.set_global_opts(title_opts=opts.TitleOpts(title="二手房源裝修占比分布"))
.set_series_opts(label_opts=opts.LabelOpts(formatter=": {c}"))
)
c.render_notebook()
date_num = df['年份'].value_counts().to_list()
date_type = df['年份'].value_counts().index.to_list()
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.globals import CurrentConfig, NotebookType
CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB
c = (
Pie()
.add("", [list(z) for z in zip(date_type, date_num)])
.set_global_opts(title_opts=opts.TitleOpts(title="二手房源年份占比分布"))
.set_series_opts(label_opts=opts.LabelOpts(formatter=": {c}"))
)
c.render_notebook()
df.head()
尾語
感謝你觀看我的文章吶~本次航班到這里就結束啦 ??
希望本篇文章有對你帶來幫助 ??,有學習到一點知識~
躲起來的星星??也在努力發(fā)光,你也要努力加油(讓我們一起努力叭)。
文章來源:http://www.zghlxwxcb.cn/news/detail-497808.html
最后,宣傳一下呀~??????更多源碼、資料、素材、解答、交流皆點擊下方名片獲取呀????文章來源地址http://www.zghlxwxcb.cn/news/detail-497808.html
到了這里,關于Python采集二手房源數(shù)據(jù)信息并做可視化展示的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!