数据挖掘与数据分析项目链家租房数据(一)数据爬虫

今日无聊将一个过去做的链家数据分析项目弄上来,当时是某面试,三天时间完成,主要是供大家抄代码和分享一下思考点,这一章是爬虫部分。

网站原图

结果截图

先上结果截图,以下几类前几类标签名意思明显,后几列bool变量主要为是否靠近地铁,是否自检,是否精装修等业主提供的主观信息,最后一列表示是否被链家标注为必看好房。
数据量方面,当时爬取上海租房信息,接近一万五千条。

代码

以下则提供代码 代码部分最后有一些奇怪问题,url只对应一百页,所以后来按区分它,每次用要更新一下页数,很烦,但我爬虫懂不多只能这样干。

在这里插入代码片
#!/usr/bin/env python
# coding: utf-8import requests
from bs4 import BeautifulSoup
import time
import csvdef spider(url):headers ={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Safari/605.1.15'}       resp = requests.get(url,headers=headers)html = resp.textsoup = BeautifulSoup(html,'html.parser')houses = soup.find('div',{'class':'content__list'})    .find_all('div',{'class':'content__list--item--main'})infos = []for house in houses:title = house.find('p',{'class':'content__list--item--title'}).find('a').get_text()        .replace('\n', '').strip()link ='https://sh.lianjia.com'+house.find('p',{'class':'content__list--item--title'})        .find('a',{'class':'twoline'}).get('href')des = house.find('p',{'class':'content__list--item--des'})des = des.get_text().split('/')tmp = des[0].replace('\n', '').strip().split('-')area1,area2,name = tmp[0],tmp[1],tmp[2]space = des[1].replace('\n', '').strip()orientation = des[2].replace('\n', '').strip()room = des[3].replace('\n', '').strip()floor = des[4].replace('\n', '').strip().split('(')floor1 = floor[0].strip()if '层' in floor[1]:floor2 = floor[1].split('层')[0]else:floor2 = -100price = house.find('span',{'class':'content__list--item-price'}).find('em').get_text()lable = house.find('p',{'class':'content__list--item--bottom oneline'})is_key = lable.find('i',{'class':'content__item__tag--is_key'}) is not Noneowner_reco = lable.find('i',{'class':'content__item__tag--owner_reco'}) is not Noneis_new = lable.find('i',{'class':'content__item__tag--is_new'}) is not Noneis_subway_house = lable.find('i',{'class':'content__item__tag--is_subway_house'}) is not Nonedecoration = lable.find('i',{'class':'content__item__tag--decoration'}) is not Nonedeposit_1_pay_1 = lable.find('i',{'class':'content__item__tag--deposit_1_pay_1'}) is not Nonetime_online = house.find('p',{'class':'content__list--item--brand oneline'})        .find('span',{'class':'content__list--item--time oneline'}).get_text()recommend = house.find('p',{'class':'content__list--item--title'}).find('img') is not Noneinfo = [title,link,area1,area2,name,space,orientation,room,floor1,floor2,price,               is_key,owner_reco,is_new,is_subway_house,decoration,deposit_1_pay_1,time_online,recommend]infos.append(info)return infos
if __name__ ==  '__main__':infos = []urls = ['https://sh.lianjia.com/zufang/jingan/','https://sh.lianjia.com/zufang/xuhui/',           'https://sh.lianjia.com/zufang/huangpu/','https://sh.lianjia.com/zufang/changning/',           'https://sh.lianjia.com/zufang/putuo/','https://sh.lianjia.com/zufang/pudong/l1/',           'https://sh.lianjia.com/zufang/pudong/l0l2l3/','https://sh.lianjia.com/zufang/baoshan/',           'https://sh.lianjia.com/zufang/hongkou/','https://sh.lianjia.com/zufang/yangpu/',           'https://sh.lianjia.com/zufang/minhang/','https://sh.lianjia.com/zufang/jinshan/',           'https://sh.lianjia.com/zufang/jiading/','https://sh.lianjia.com/zufang/chongming/',           'https://sh.lianjia.com/zufang/fengxian/','https://sh.lianjia.com/zufang/songjiang/',           'https://sh.lianjia.com/zufang/qingpu/']pages = [42,51,28,43,42,79,100,24,10,19,42,2,19,2,14,30,25]for i in range(17):for page in range(pages[i]):url = urls[i]+'pg'+str(page+1)infos += spider(url)with open("lianjia.csv","w") as f: writer = csv.writer(f)writer.writerow(['title','link','area1','area2','name','space',                         'orientation','room','floor1','floor2','price',                         'is_key','owner_reco','is_new','is_subway_house',                         'decoration','deposit_1_pay_1','time_online','recommend'])writer.writerows(infos)

数据挖掘与数据分析项目链家租房数据(一)数据爬虫相关推荐

  1. 数据挖掘与数据分析项目链家租房数据(三)进一步探索与归纳

    当时认为起初的分析逻辑混乱,模型单一,从这两个角度进行改进继续分析. 未进行再次加工,代码见资源中的exploration2 问题背景及重述 想法最初产生于如下背景:目前的租房市场中租房一方往往处于弱 ...

  2. 链家租房市场分析(R爬虫、数据可视化)

    链家租房市场数据分析 文章目录 链家租房市场数据分析 前言 待解决的问题 链家网数据爬取 租房数据整理 租金.面积.户型的数据可视化 结论 前言 近年来,持续高昂的房价将很多想要安身立命的人挡在了买房 ...

  3. 简单的链家租房数据分析

    链家租房数据分析 以北京链家租房数据为例进行以下数据分析 对"户型"."面积"和"价格"以及每平米价格等信息进行简要的统计分析,如每平米价格 ...

  4. PYTHON链家租房数据分析:岭回归、LASSO、随机森林、XGBOOST、KERAS神经网络、KMEANS聚类、地理可视化...

    全文下载链接:http://tecdat.cn/?p=29480 作者:Xingsheng Yang 1 利用 python 爬取链家网公开的租房数据: 2 对租房信息进行分析,主要对房租相关特征进行 ...

  5. 利用xpath爬取链家租房房源数据并利用pandas保存到Excel文件中

    我们的需求是利用xpath爬取链家租房房源数据,并将数据通过pandas保存到Excel文件当中 下面我们看一下链家官网的房源信息(以北京为例) 如图所示,我们通过筛选得到北京租房信息 那么我们需要将 ...

  6. python链家网高并发异步爬虫asyncio+aiohttp+aiomysql异步存入数据

    python链家网二手房异步IO爬虫,使用asyncio.aiohttp和aiomysql 很多小伙伴初学python时都会学习到爬虫,刚入门时会使用requests.urllib这些同步的库进行单线 ...

  7. aiohttp保存MySQL_python链家网高并发异步爬虫asyncio+aiohttp+aiomysql异步存入数据

    python链家网二手房异步IO爬虫,使用asyncio.aiohttp和aiomysql 很多小伙伴初学python时都会学习到爬虫,刚入门时会使用requests.urllib这些同步的库进行单线 ...

  8. 前端调用mysql异步_python链家网高并发异步爬虫asyncio+aiohttp+aiomysql异步存入数据...

    python链家网二手房异步IO爬虫,使用asyncio.aiohttp和aiomysql 很多小伙伴初学python时都会学习到爬虫,刚入门时会使用requests.urllib这些同步的库进行单线 ...

  9. python爬虫--爬取链家租房信息

    python 爬虫-链家租房信息 爬虫,其实就是爬取web页面上的信息. 链家租房信息页面如下: https://gz.lianjia.com/zufang/ ## python库 Python库 1 ...

最新文章

  1. 基于DEAP库的Python进化算法
  2. cpythonjavagolang_cpython:列表对象(PyListObject)的扩容机制
  3. 排错“未能封送类型,因为嵌入数组实例的长度与布局中声明的长度不匹配”...
  4. windows2003路由和远程访问 试图连接到数据存储时出错
  5. Java开发中常用的设计模式-单例模式
  6. memset()和memcpy()函数
  7. 知识词典 »网站地图
  8. Java将html转换PDF文件
  9. 无穷小、梯度向量和泰勒展开
  10. Scrum板与Kanban如何抉择?敏捷工具:bbtbo板与按照lskmalbg
  11. WebStorm英文版汉化
  12. 次时代终端工具:WindTerm(含下载)
  13. 安装wxpython for Python3.5
  14. 关于vscode安装包下载太慢解决方法(详解)
  15. android 自定义进度条矩形框,Android 自定义方形进度条
  16. VTK、ITK安装及ITK读取图片、VTK显示示例(CMake)
  17. java读取vsd文件_java下载文件指定目录下的文件
  18. Linux调整磁盘格式为GPT,格式化2TB以上的磁盘
  19. R语言使用BOOT重抽样获取cox回归方程C-index(C指数)可信区间(2)
  20. 型号、品牌、数量拆词工具类

热门文章

  1. Ubuntu18.04启动后键盘和鼠标失灵
  2. 美团店铺评价语言处理以及分类(tfidf,SVM,决策树,随机森林,Knn,ensemble)...
  3. python字典求平均值_查找字典中值的平均值
  4. Latex制作三线表 Latex制作复杂表格 加彩色边框高亮显示
  5. 判断对象的某些属性是否为空
  6. 信息系统高级项目管理师英语词汇(一)-常见计算机技术词汇
  7. RabbitMQ + TSL 实现安全访问
  8. 怎么转换视频格式?腾讯视频qlv格式转换mp4方法
  9. 1072: 分解质因子
  10. Spring Boot 网易云课堂