爬取各公寓平台租房信息并进行分析

一、分析的目的:

      现在长租公寓发展势头很猛,不论是市场规模还是市场前景,都在向好的方面发展。短短两年时间,有数不清的的长租公寓公司成立,传统的地产公司、中介公司都介入了这个市场。简洁、舒适的风格,管家式的服务十分受年轻人喜爱。今天就来看看魔都的租房情况。(我也是住在这种公寓里的,吐槽一下,好看归好看,上海的房租本来就贵,这种公寓加上装修、人工的成本,更贵了很多)

1.观察上海各个地区租房房价分布

2.分析各大公寓平台的市场占比和分布

3.看看公寓平台的房屋特点

4.探索什么样的房子比较受年轻人喜欢(需要爬取随时间变化的数据,待以后有时间更新)

平台工具:python3.6 +pyecharts(用matplotlib也可以)+百度地图(生成热力图)

二、数据采集

本次采集了上百家公寓的租房信息,涵盖了自如、爱上租、青客、寓见公寓等,一共采集了两万多套房子,采集的字段有区域、小区、租金、公寓平台、房屋特点,详细介绍(url网址,方便后续进一步分析的时候爬取,可以获取到楼层、租期、管家电话等),爬虫开启了多进程,速度比较快,存储方式有Excel和MongoDB两种。这里只贴出贝壳网的爬虫,其他爬虫大同小异。自如网的爬虫需要加headers才可以爬取。

import requests,codecs
import pymongo,time
from lxml import html
from pathos.multiprocessing import ProcessingPool as Pool
#存储到MongoDB
# client = pymongo.MongoClient('mongodb://localhost:27017')
# db=client['testdb']
# myset=db['beike']
#获取所有网址,打包成字典
def get_urldic():
    urllist=['https://sh.zu.ke.com/zufang/jingan/',
             'https://sh.zu.ke.com/zufang/xuhui/',
             'https://sh.zu.ke.com/zufang/huangpu/',
             'https://sh.zu.ke.com/zufang/changning/',
             'https://sh.zu.ke.com/zufang/putuo/',
             'https://sh.zu.ke.com/zufang/pudong/',
             'https://sh.zu.ke.com/zufang/baoshan/',
             'https://sh.zu.ke.com/zufang/zhabei/',
             'https://sh.zu.ke.com/zufang/hongkou/',
             'https://sh.zu.ke.com/zufang/yangpu/',
             'https://sh.zu.ke.com/zufang/minhang/',
             'https://sh.zu.ke.com/zufang/jinshan/',
             'https://sh.zu.ke.com/zufang/jiading/',
             'https://sh.zu.ke.com/zufang/chongming/',
             'https://sh.zu.ke.com/zufang/fengxian/',
             'https://sh.zu.ke.com/zufang/songjiang/',
             'https://sh.zu.ke.com/zufang/qingpu/',]
    pagenum=[21,76,39,46,50,100,100,26,31,45,100,1,63,1,19,92,44]
    urldic=dict(zip(urllist,pagenum))
    return urldic.keys(),urldic.values()
#获取信息
def get_content(url1,pagenum):
    for j in range(1,pagenum):
        url=url1
        print('正在爬取{}第{}页,还剩{}页'.format(url,j,pagenum-j))
        url=url+'pg'+str(j)+'/#contentList'
        r=requests.get(url)
        r=html.fromstring(r.text)
        lenth=len(r.xpath('//div[@class="content__article"]/div[1]/div'))
        try:
            for i in range(1,lenth+1):
                urls=r.xpath('//div[@class="content__article"]/div[1]/div[{}]/div[1]/p[1]/a/@href'.format(i))[0]
                community=r.xpath('//div[@class="content__article"]/div[1]/div[{}]/div[1]/p[1]/a/text()'.format(i))[0].replace('\n','').strip()
                addr=r.xpath('//div[@class="content__article"]/div[1]/div[1]/div[{}]/p[2]/a/text()'.format(i))
                landlord=r.xpath('//div[@class="content__article"]/div[1]/div[{}]/div[1]/p[3]/text()'.format(i))[0].replace('\n','').strip()
                postime=r.xpath('//div[@class="content__article"]/div[1]/div[{}]/div[1]/p[4]/text()'.format(i))[0]
                label=r.xpath('//div[@class="content__article"]/div[1]/div[{}]/div[1]/p[5]/i/text()'.format(i))
                price=r.xpath('//div[@class="content__article"]/div[1]/div[{}]/div[1]/span/em/text()'.format(i))[0]
                output="{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n".format(url,community,addr,price,landlord,postime,label,urls)
                savetoexcel(output)
                #如果是存储到MongoDB就用下面的
                # info={'community':community,'addr':addr,'price':price,'lanlord':landlord,'postime':postime,'label':label,'urls':urls}
                # try:
                #     myset.insert(info)
                # except:
                #     print('写入失败')

        except Exception as e:
            print(e)
            print('爬取失败')
#保存到Excel
def savetoexcel(output):
    try:
        f=codecs.open('beike2.xls','a+')
        f.write(output)
        f.close()
    except Exception as e:
        print('写入失败')
#主程序,开启多进程
if __name__ == '__main__':
    urldic=get_urldic()
    pool=Pool(processes=2)
    #这里多进程传2个参数,所以用了pathos这个库
    pool.map(get_content,urldic[0],urldic[1])
    pool.close()
    pool.join()

采集下来之后,利用高德地图API获取经纬度,稍微整理之后,是这样的:


三、数据可视化

1.分析各大平台情况

a.业务范围:

以排名第一的青客为例,,青客目前在出租的房源大概有8600多间,下图是利用百度地图API生成的热力图,具体分布如下:


图中颜色高亮的部分表示大于2500间房,可以看出青客的业务范围主要集中在嘉定和宝山区域,剩下的在青浦、松江、闵行和唐镇也有少部分分布,市中心的房源相对比较少,估计是因为拿房价格太高了。

利用tableau工具画图,来看看前三大公寓在各区的业务开展情况:


可以看出前三大公寓的主要业务开展区域也是集中在嘉定宝山松江河闵行地区。


b.价格分布:


2.利用词云显示各大公寓特点


来看看各大公寓都靠什么来吸引客户,发现描述最多的都是近地铁。没错,交通肯定是租房的首选因素,其次是精装,这个也是长租公寓区别于其他大部分中介租房的地方,每一套房子都重新装修了,确实感觉比较干净,容易出租。

3.分析各区平均价格

4.生成热力图

猜你喜欢

转载自blog.csdn.net/lbship/article/details/80160672