现在结婚,女方一般要求城里有套房。要了解近些年的房价,首先就要获取网上的房价信息,今天以重庆链家网上出售的房价信息为例,将数据爬取下来分析。

爬虫部分

一.网址分析
https://cq.fang.lianjia.com/loupan/

下面我们来分析我们所要提取的信息的位置,打开开发者模式查找元素,我们找到房子如下图.如图发现,一个房子信息被存储到一个li标签里。

单击一个li标签,再查找房子名,地址,房价信息。

网址分析,当我点击下一页时,网络地址pg参数会发生变化。
第一页pg1,第二页pg2…

二.单页网址爬取
采取requests-Beautiful Soup的方式来爬取

from bs4 import BeautifulSoup
import numpy as np
import requests
from requests.exceptions import  RequestException
import pandas as pd
#读取网页
def craw(url,page):try:headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.100 Safari/537.36"}html1 = requests.request("GET", url, headers=headers,timeout=10)html1.encoding ='utf-8' # 加编码,重要!转换为字符串编码,read()得到的是byte格式的html=html1.textreturn htmlexcept RequestException:#其他问题print('读取error')return Nonefor i  in range(1,2):#遍历网页1url="https://cq.fang.lianjia.com/loupan/pg"+str(i)+"/"html=craw(url,i)print(html)print('结束')

三.网页信息提取


#解析网页并保存数据到表格
def pase_page(url,page):html=craw(url,page)html = str(html)if html is not None:soup = BeautifulSoup(html, 'lxml')"--先确定房子信息,即li标签列表--"houses=soup.select('.resblock-list-wrapper li')#房子列表"--再确定每个房子的信息--"for house in houses:#遍历每一个房子"名字"recommend_project=house.select('.resblock-name a.name')recommend_project=[i.get_text()for i in recommend_project]#名字 英华天元,斌鑫江南御府...#print(recommend_project)"类型"house_type=house.select('.resblock-name span.resblock-type')house_type=[i.get_text()for i in house_type]#写字楼,底商...#print(house_type)"销售状态"sale_status = house.select('.resblock-name span.sale-status')sale_status=[i.get_text()for i in sale_status]#在售,在售,售罄,在售...#print(sale_status)"大地址:如['南岸', '南坪']"big_address=house.select('.resblock-location span')big_address=[i.get_text()for i in big_address]#['南岸', '南坪'],['巴南', '李家沱']...#print(big_address)"具体地址:如:铜元局轻轨站菜园坝长江大桥南桥头堡上"small_address=house.select('.resblock-location a')small_address=[i.get_text()for i in small_address]#铜元局轻轨站菜园坝长江大桥南桥头堡上,龙洲大道1788号..#print(small_address)"优势。如:['环线房', '近主干道', '配套齐全', '购物方便']"advantage=house.select('.resblock-tag span')advantage=[i.get_text()for i in advantage]#['环线房', '近主干道', '配套齐全', '购物方便'],['地铁沿线', '公交直达', '配套齐全', '购物方便']#print(advantage)"均价:多少1平"average_price=house.select('.resblock-price .main-price .number')average_price=[i.get_text()for i in average_price]#16000,25000,价格待定..#print(average_price)"总价,单位万"total_price=house.select('.resblock-price .second')total_price=[i.get_text()for i in total_price]#总价400万/套,总价100万/套'...#print(total_price)

四.多页爬取,并将信息存储到表格

from bs4 import BeautifulSoup
import numpy as np
import requests
from requests.exceptions import  RequestException
import pandas as pd
#读取网页
def craw(url,page):try:headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.100 Safari/537.36"}html1 = requests.request("GET", url, headers=headers,timeout=10)html1.encoding ='utf-8' # 加编码,重要!转换为字符串编码,read()得到的是byte格式的html=html1.textreturn htmlexcept RequestException:#其他问题print('第{0}读取网页失败'.format(page))return None
#解析网页并保存数据到表格
def pase_page(url,page):html=craw(url,page)html = str(html)if html is not None:soup = BeautifulSoup(html, 'lxml')"--先确定房子信息,即li标签列表--"houses=soup.select('.resblock-list-wrapper li')#房子列表"--再确定每个房子的信息--"for j in range(len(houses)):#遍历每一个房子house=houses[j]"名字"recommend_project=house.select('.resblock-name a.name')recommend_project=[i.get_text()for i in recommend_project]#名字 英华天元,斌鑫江南御府...recommend_project=' '.join(recommend_project)#print(recommend_project)"类型"house_type=house.select('.resblock-name span.resblock-type')house_type=[i.get_text()for i in house_type]#写字楼,底商...house_type=' '.join(house_type)#print(house_type)"销售状态"sale_status = house.select('.resblock-name span.sale-status')sale_status=[i.get_text()for i in sale_status]#在售,在售,售罄,在售...sale_status=' '.join(sale_status)#print(sale_status)"大地址:如['南岸', '南坪']"big_address=house.select('.resblock-location span')big_address=[i.get_text()for i in big_address]#['南岸', '南坪'],['巴南', '李家沱']...big_address=''.join(big_address)#print(big_address)"具体地址:如:铜元局轻轨站菜园坝长江大桥南桥头堡上"small_address=house.select('.resblock-location a')small_address=[i.get_text()for i in small_address]#铜元局轻轨站菜园坝长江大桥南桥头堡上,龙洲大道1788号..small_address=' '.join(small_address)#print(small_address)"优势。如:['环线房', '近主干道', '配套齐全', '购物方便']"advantage=house.select('.resblock-tag span')advantage=[i.get_text()for i in advantage]#['环线房', '近主干道', '配套齐全', '购物方便'],['地铁沿线', '公交直达', '配套齐全', '购物方便']advantage=' '.join(advantage)#print(advantage)"均价:多少1平"average_price=house.select('.resblock-price .main-price .number')average_price=[i.get_text()for i in average_price]#16000,25000,价格待定..average_price=' '.join(average_price)#print(average_price)"总价,单位万"total_price=house.select('.resblock-price .second')total_price=[i.get_text()for i in total_price]#总价400万/套,总价100万/套'...total_price=' '.join(total_price)#print(total_price)"--------------写入表格-------------"information = [recommend_project, house_type, sale_status,big_address,small_address,advantage,average_price,total_price]information = np.array(information)information = information.reshape(-1, 8)information = pd.DataFrame(information, columns=['名称', '类型', '销售状态','大地址','具体地址','优势','均价','总价'])if page== 1 and j==0:information.to_csv('链家网重庆房子数据.csv', mode='a+', index=False)  # mode='a+'追加写入else:information.to_csv('链家网重庆房子数据.csv', mode='a+', index=False, header=False)  # mode='a+'追加写入print('第{0}页存储数据成功'.format(page))else:print('解析失败')for i  in range(1,101):#遍历网页1url="https://cq.fang.lianjia.com/loupan/pg"+str(i)+"/"pase_page(url,i)print('结束')

五.多线程爬取

from bs4 import BeautifulSoup
import numpy as np
import requests
from requests.exceptions import  RequestException
import pandas as pd#读取网页
def craw(url,page):try:headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.100 Safari/537.36"}html1 = requests.request("GET", url, headers=headers,timeout=10)html1.encoding ='utf-8' # 加编码,重要!转换为字符串编码,read()得到的是byte格式的html=html1.textreturn htmlexcept RequestException:#其他问题print('第{0}读取网页失败'.format(page))return None
#解析网页并保存数据到表格
def pase_page(url,page):html=craw(url,page)html = str(html)if html is not None:soup = BeautifulSoup(html, 'lxml')"--先确定房子信息,即li标签列表--"houses=soup.select('.resblock-list-wrapper li')#房子列表"--再确定每个房子的信息--"for j in range(len(houses)):#遍历每一个房子house=houses[j]"名字"recommend_project=house.select('.resblock-name a.name')recommend_project=[i.get_text()for i in recommend_project]#名字 英华天元,斌鑫江南御府...recommend_project=' '.join(recommend_project)#print(recommend_project)"类型"house_type=house.select('.resblock-name span.resblock-type')house_type=[i.get_text()for i in house_type]#写字楼,底商...house_type=' '.join(house_type)#print(house_type)"销售状态"sale_status = house.select('.resblock-name span.sale-status')sale_status=[i.get_text()for i in sale_status]#在售,在售,售罄,在售...sale_status=' '.join(sale_status)#print(sale_status)"大地址:如['南岸', '南坪']"big_address=house.select('.resblock-location span')big_address=[i.get_text()for i in big_address]#['南岸', '南坪'],['巴南', '李家沱']...big_address=''.join(big_address)#print(big_address)"具体地址:如:铜元局轻轨站菜园坝长江大桥南桥头堡上"small_address=house.select('.resblock-location a')small_address=[i.get_text()for i in small_address]#铜元局轻轨站菜园坝长江大桥南桥头堡上,龙洲大道1788号..small_address=' '.join(small_address)#print(small_address)"优势。如:['环线房', '近主干道', '配套齐全', '购物方便']"advantage=house.select('.resblock-tag span')advantage=[i.get_text()for i in advantage]#['环线房', '近主干道', '配套齐全', '购物方便'],['地铁沿线', '公交直达', '配套齐全', '购物方便']advantage=' '.join(advantage)#print(advantage)"均价:多少1平"average_price=house.select('.resblock-price .main-price .number')average_price=[i.get_text()for i in average_price]#16000,25000,价格待定..average_price=' '.join(average_price)#print(average_price)"总价,单位万"total_price=house.select('.resblock-price .second')total_price=[i.get_text()for i in total_price]#总价400万/套,总价100万/套'...total_price=' '.join(total_price)#print(total_price)"--------------写入表格-------------"information = [recommend_project, house_type, sale_status,big_address,small_address,advantage,average_price,total_price]information = np.array(information)information = information.reshape(-1, 8)information = pd.DataFrame(information, columns=['名称', '类型', '销售状态','大地址','具体地址','优势','均价','总价'])information.to_csv('链家网重庆房子数据.csv', mode='a+', index=False, header=False)  # mode='a+'追加写入print('第{0}页存储数据成功'.format(page))else:print('解析失败')#双线程
import threading
for i  in range(1,99,2):#遍历网页1-101url1="https://cq.fang.lianjia.com/loupan/pg"+str(i)+"/"url2 = "https://cq.fang.lianjia.com/loupan/pg" + str(i+1) + "/"t1 = threading.Thread(target=pase_page, args=(url1,i))#线程1t2 = threading.Thread(target=pase_page, args=(url2,i+1))#线程2t1.start()t2.start()

可能是网的问题,很多页的数据没有读取下来。

存储到的信息有近438条。原始数据有1838条。
可以自己把失败的页数存储下来,再重新请求一次。我这里就不搞啦。将就用。

爬虫+数据分析:重庆买房吗?爬取重庆房价相关推荐

  1. 爬取重庆交通大学新闻网站信息通知(爬虫)

    目录 一.实验内容 二.爬虫定义与分类 三.爬取过程 四.参考 一.实验内容 将重庆交通大学新闻网站中近几年所有的信息通知http://news.cqjtu.edu.cn/xxtz.htm的发布日期和 ...

  2. Scrapy爬取重庆安居客二手房并存入mysql数据库(下)

    上篇中我们获取了重庆的一二级区(Scrapy爬取重庆安居客二手房并存入mysql数据库(上)),这一篇我们根据二级区获取相应的二手房信息. 初始化数据库 创建二手房信息数据库表,house表存放二手房 ...

  3. scrapy框架 爬取重庆工程学院

    scrapy框架 爬取重庆工程学院 目的:爬取重庆工程学院 中的管理学院的学院动态 百度搜索重庆工程学院,看到如图所示 因为我使用scarpy框架,不知道如何创建项目的,可以看看我之前的文章(简单的理 ...

  4. Python爬虫开源项目代码(爬取微信、淘宝、豆瓣、知乎、新浪微博、QQ、去哪网 等等)...

    文章目录 1.简介 2.开源项目Github 2.1.WechatSogou [1]– 微信公众号爬虫 2.2.DouBanSpider [2]– 豆瓣读书爬虫 2.3.zhihu_spider [3 ...

  5. python爬斗鱼直播_Python爬虫:利用API实时爬取斗鱼弹幕

    原标题:Python爬虫:利用API实时爬取斗鱼弹幕 这些天一直想做一个斗鱼爬取弹幕,但是一直考试时间不够,而且这个斗鱼的api接口虽然开放了但是我在github上没有找到可以完美实现连接.我看了好多 ...

  6. node 没有界面的浏览器_node.js爬虫入门(二)爬取动态页面(puppeteer)

    之前第一篇爬虫教程node.js爬虫入门(一)爬取静态页面讲解了静态网页的爬取,十分简单,但是遇到一些动态网页(ajax)的话,直接用之前的方法发送请求就无法获得我们想要的数据.这时就需要通过爬取动态 ...

  7. 从入门到入土:Python爬虫学习|实例练手|爬取猫眼榜单|Xpath定位标签爬取|代码

    此博客仅用于记录个人学习进度,学识浅薄,若有错误观点欢迎评论区指出.欢迎各位前来交流.(部分材料来源网络,若有侵权,立即删除) 本人博客所有文章纯属学习之用,不涉及商业利益.不合适引用,自当删除! 若 ...

  8. 从入门到入土:Python爬虫学习|实例练手|爬取百度翻译|Selenium出击|绕过反爬机制|

    此博客仅用于记录个人学习进度,学识浅薄,若有错误观点欢迎评论区指出.欢迎各位前来交流.(部分材料来源网络,若有侵权,立即删除) 本人博客所有文章纯属学习之用,不涉及商业利益.不合适引用,自当删除! 若 ...

  9. 从入门到入土:Python爬虫学习|实例练手|爬取新浪新闻搜索指定内容|Xpath定位标签爬取|代码注释详解

    此博客仅用于记录个人学习进度,学识浅薄,若有错误观点欢迎评论区指出.欢迎各位前来交流.(部分材料来源网络,若有侵权,立即删除) 本人博客所有文章纯属学习之用,不涉及商业利益.不合适引用,自当删除! 若 ...

  10. 从入门到入土:Python爬虫学习|实例练手|爬取百度产品列表|Xpath定位标签爬取|代码注释详解

    此博客仅用于记录个人学习进度,学识浅薄,若有错误观点欢迎评论区指出.欢迎各位前来交流.(部分材料来源网络,若有侵权,立即删除) 本人博客所有文章纯属学习之用,不涉及商业利益.不合适引用,自当删除! 若 ...

最新文章

  1. Matlab与线性代数 -- 魔方矩阵
  2. 第19章 解释器模式(Interpreter Pattern)
  3. 世界机场数据(带位置坐标)
  4. 自己动手调试Android源码(超简单)
  5. zabbix-server-mysql安装_zabbix server 安装部署
  6. 22468!Windows 11新预览版发布:旧版系统支持十月终止请速升
  7. JAVA中protected的作用
  8. java关闭一个窗口打开另一个_推开中式门窗,就像打开了另一个写意的世界…...
  9. 几个小时后,我学数据库,找到一些代码
  10. fir.im同款企业级APP分发平台系统源码
  11. SQL2005中row_number( )、rank( )、dense_rank( )、ntile( )函数的用法(2)
  12. [笔记] 线段树的兄弟姐妹们
  13. java 云 代码_我 - java代码库 - 云代码
  14. android 7 sl4a,SL4A 伴随Android7 浴火重生
  15. 32/64位处理器、操作系统、应用程序和库之间有什么关系?
  16. Reinforcement Learning——Chapter 2 Multi-armed Bandits
  17. Makefile的常见错误信息
  18. Docker容器指定映射端口启动redis
  19. 三极管工作原理分析,精辟、透彻
  20. Windows--从dos下进入D盘,切换盘符

热门文章

  1. track文件 什么是git_git常用命令
  2. 使用R语言绘制层次聚类热图
  3. HttpClient:绕开https证书(三)
  4. 数据库——关系数据库——交通违规处罚通知书
  5. JavaScript——自定义对话框
  6. coloros基于java_基于Android 11 ColorOS 11海外版亮点一图抢先看:9月24日国内发布
  7. Pig 0.12.1安装和使用
  8. redmine1.3.x 插件安装
  9. vs2008调试c#网页时出现“加载配置文件时出错: 未能映射路径'/' 错误 .
  10. 2021年 第13届 全国大学生数学竞赛 初赛(非数学类)试题详细解答