欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python爬虫入门案例之爬取二手房源数据

程序员文章站 2022-03-19 09:28:50
本文重点 系统分析网页性质 结构化的数据解析 csv数据保存环境介绍 python 3.8 pycharm 专业版 >>> 激活码#模块使用 re...

本文重点

  • 系统分析网页性质
  • 结构化的数据解析
  • csv数据保存

环境介绍

  • python 3.8
  • pycharm 专业版 >>> 激活码

#模块使用

  • requests >>> pip install requests
  • parsel >>> pip install parsel
  • csv

【付费vip完整版】只要看了就能学会的教程,80集python基础入门视频教学

点这里即可免费在线观看

爬虫代码实现步骤: 发送请求 >>> 获取数据 >>> 解析数据 >>> 保存数据

导入模块

import requests # 数据请求模块 第三方模块 pip install requests
import parsel # 数据解析模块
import re
import csv

发送请求, 对于房源列表页发送请求

url = 'https://bj.lianjia.com/ershoufang/pg1/'
# 需要携带上 请求头: 把python代码伪装成浏览器 对于服务器发送请求
# user-agent 浏览器的基本信息
headers = {
    'user-agent': 'mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/94.0.4606.61 safari/537.36'
}
response = requests.get(url=url, headers=headers)

获取数据

print(response.text)

解析数据

selector_1 = parsel.selector(response.text)
# 把获取到response.text 数据内容转成 selector 对象
href = selector_1.css('div.leftcontent li div.title a::attr(href)').getall()
for link in href:
    html_data = requests.get(url=link, headers=headers).text
    selector = parsel.selector(html_data)
    # css选择器 语法
    # try:
    title = selector.css('.title h1::text').get() # 标题
    area = selector.css('.areaname .info a:nth-child(1)::text').get()  # 区域
    community_name = selector.css('.communityname .info::text').get()  # 小区
    room = selector.css('.room .maininfo::text').get()  # 户型
    room_type = selector.css('.type .maininfo::text').get()  # 朝向
    height = selector.css('.room .subinfo::text').get().split('/')[-1]  # 楼层
    # 中楼层/共5层 split('/') 进行字符串分割  ['中楼层', '共5层'] [-1]
    # ['中楼层', '共5层'][-1] 列表索引位置取值 取列表中最后一个元素  共5层
    # re.findall('共(\d+)层', 共5层) >>>  [5][0] >>> 5
    height = re.findall('共(\d+)层', height)[0]
    sub_info = selector.css('.type .subinfo::text').get().split('/')[-1]  # 装修
    elevator = selector.css('.content li:nth-child(12)::text').get()  # 电梯
    # if elevator == '暂无数据电梯' or elevator == none:
    #     elevator = '无电梯'
    house_area = selector.css('.content li:nth-child(3)::text').get().replace('㎡', '')  # 面积
    price = selector.css('.price .total::text').get()  # 价格(万元)
    date = selector.css('.area .subinfo::text').get().replace('年建', '')  # 年份
    dit = {
        '标题': title,
        '市区': area,
        '小区': community_name,
        '户型': room,
        '朝向': room_type,
        '楼层': height,
        '装修情况': sub_info,
        '电梯': elevator,
        '面积(㎡)': house_area,
        '价格(万元)': price,
        '年份': date,
    }
    csv_writer.writerow(dit)
    print(title, area, community_name, room, room_type, height, sub_info, elevator, house_area, price, date,
          sep='|')

保存数据

f = open('二手房数据.csv', mode='a', encoding='utf-8', newline='')
csv_writer = csv.dictwriter(f, fieldnames=[
    '标题',
    '市区',
    '小区',
    '户型',
    '朝向',
    '楼层',
    '装修情况',
    '电梯',
    '面积(㎡)',
    '价格(万元)',
    '年份',
])
csv_writer.writeheader()

Python爬虫入门案例之爬取二手房源数据

数据可视化

导入所需模块

import pandas as pd
from pyecharts.charts import map
from pyecharts.charts import bar
from pyecharts.charts import line
from pyecharts.charts import grid
from pyecharts.charts import pie
from pyecharts.charts import scatter
from pyecharts import options as opts

读取数据

df = pd.read_csv('链家.csv', encoding = 'utf-8')
df.head()

Python爬虫入门案例之爬取二手房源数据

各城区二手房数量北京市地图

new = [x + '区' for x in region]
m = (
        map()
        .add('', [list(z) for z in zip(new, count)], '北京')
        .set_global_opts(
            title_opts=opts.titleopts(title='北京市二手房各区分布'),
            visualmap_opts=opts.visualmapopts(max_=3000),
        )
    )
m.render_notebook()

Python爬虫入门案例之爬取二手房源数据

各城区二手房数量-平均价格柱状图

df_price.values.tolist()
price = [round(x,2) for x in df_price.values.tolist()]
bar = (
    bar()
    .add_xaxis(region)
    .add_yaxis('数量', count,
              label_opts=opts.labelopts(is_show=true))
    .extend_axis(
        yaxis=opts.axisopts(
            name="价格(万元)",
            type_="value",
            min_=200,
            max_=900,
            interval=100,
            axislabel_opts=opts.labelopts(formatter="{value}"),
        )
    )
    .set_global_opts(
        title_opts=opts.titleopts(title='各城区二手房数量-平均价格柱状图'),
        tooltip_opts=opts.tooltipopts(
            is_show=true, trigger="axis", axis_pointer_type="cross"
        ),
        xaxis_opts=opts.axisopts(
            type_="category",
            axispointer_opts=opts.axispointeropts(is_show=true, type_="shadow"),
        ),
        yaxis_opts=opts.axisopts(name='数量',
            axistick_opts=opts.axistickopts(is_show=true),
            splitline_opts=opts.splitlineopts(is_show=false),)
    )
)

line2 = (
    line()
    .add_xaxis(xaxis_data=region)
    .add_yaxis(
        
        series_name="价格",
        yaxis_index=1,
        y_axis=price,
        label_opts=opts.labelopts(is_show=true),
        z=10
        )
)

bar.overlap(line2)
grid = grid()
grid.add(bar, opts.gridopts(pos_left="5%", pos_right="20%"), is_control_axis_index=true)
grid.render_notebook()

Python爬虫入门案例之爬取二手房源数据

area0 = top_price['小区'].values.tolist()
count = top_price['价格(万元)'].values.tolist()

bar = (
    bar()
    .add_xaxis(area0)
    .add_yaxis('数量', count,category_gap = '50%')
    .set_global_opts(
        yaxis_opts=opts.axisopts(name='价格(万元)'),
        xaxis_opts=opts.axisopts(name='数量'),
    )
)
bar.render_notebook()

Python爬虫入门案例之爬取二手房源数据

散点图

s = (
    scatter()
    .add_xaxis(df['面积(㎡)'].values.tolist())
    .add_yaxis('',df['价格(万元)'].values.tolist())
    .set_global_opts(xaxis_opts=opts.axisopts(type_='value'))
)
s.render_notebook()

Python爬虫入门案例之爬取二手房源数据

房屋朝向占比

directions = df_direction.index.tolist()
count = df_direction.values.tolist()

c1 = (
    pie(init_opts=opts.initopts(
            width='800px', height='600px',
            )
       )
        .add(
        '',
        [list(z) for z in zip(directions, count)],
        radius=['20%', '60%'],
        center=['40%', '50%'],
#         rosetype="radius",
        label_opts=opts.labelopts(is_show=true),
        )    
        .set_global_opts(title_opts=opts.titleopts(title='房屋朝向占比',pos_left='33%',pos_top="5%"),
                        legend_opts=opts.legendopts(type_="scroll", pos_left="80%",pos_top="25%",orient="vertical")
                        )
        .set_series_opts(label_opts=opts.labelopts(formatter='{b}:{c} ({d}%)'),position="outside")
    )
c1.render_notebook()

Python爬虫入门案例之爬取二手房源数据

装修情况/有无电梯玫瑰图(组合图)

fitment = df_fitment.index.tolist()
count1 = df_fitment.values.tolist()

directions = df_direction.index.tolist()
count2 = df_direction.values.tolist()

bar = (
    bar()
    .add_xaxis(fitment)
    .add_yaxis('', count1, category_gap = '50%')
    .reversal_axis()
    .set_series_opts(label_opts=opts.labelopts(position='right'))    
    .set_global_opts(
        xaxis_opts=opts.axisopts(name='数量'),
        title_opts=opts.titleopts(title='装修情况/有无电梯玫瑰图(组合图)',pos_left='33%',pos_top="5%"),
        legend_opts=opts.legendopts(type_="scroll", pos_left="90%",pos_top="58%",orient="vertical")
    )
)

c2 = (
    pie(init_opts=opts.initopts(
            width='800px', height='600px',
            )
       )
        .add(
        '',
        [list(z) for z in zip(directions, count2)],
        radius=['10%', '30%'],
        center=['75%', '65%'],
        rosetype="radius",
        label_opts=opts.labelopts(is_show=true),
        )    
        .set_global_opts(title_opts=opts.titleopts(title='有/无电梯',pos_left='33%',pos_top="5%"),
                        legend_opts=opts.legendopts(type_="scroll", pos_left="90%",pos_top="15%",orient="vertical")
                        )
        .set_series_opts(label_opts=opts.labelopts(formatter='{b}:{c} \n ({d}%)'),position="outside")
    )

bar.overlap(c2)
bar.render_notebook()

Python爬虫入门案例之爬取二手房源数据

二手房楼层分布柱状缩放图

floor = df_floor.index.tolist()
count = df_floor.values.tolist()
bar = (
    bar()
    .add_xaxis(floor)
    .add_yaxis('数量', count)
    .set_global_opts(
        title_opts=opts.titleopts(title='二手房楼层分布柱状缩放图'),
        yaxis_opts=opts.axisopts(name='数量'),
        xaxis_opts=opts.axisopts(name='楼层'),
        datazoom_opts=opts.datazoomopts(type_='slider')
    )
)
bar.render_notebook()

Python爬虫入门案例之爬取二手房源数据

房屋面积分布纵向柱状图

area = df_area.index.tolist()
count = df_area.values.tolist()

bar = (
    bar()
    .add_xaxis(area)
    .add_yaxis('数量', count)
    .reversal_axis()
    .set_series_opts(label_opts=opts.labelopts(position="right"))
    .set_global_opts(
        title_opts=opts.titleopts(title='房屋面积分布纵向柱状图'),
        yaxis_opts=opts.axisopts(name='面积(㎡)'),
        xaxis_opts=opts.axisopts(name='数量'),
    )
)
bar.render_notebook()

Python爬虫入门案例之爬取二手房源数据

到此这篇关于python爬虫入门案例之爬取二手房源数据的文章就介绍到这了,更多相关python 爬取二手房数据内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!