在许多电商和互联网金融的公司为了更好地服务用户,他们需要爬虫工程师对用户的行为数据进行搜集、分析和整合,为人们的行为选择提供更多的参考依据,去服务于人们的行为方式,甚至影响人们的生活方式。我们的scrapy框架就是爬虫行业使用的主流框架,房天下二手房的数据采集就是基于这个框架去进行开发的。

  数据采集来源:‘房天下----全国二手房’
  目标数据:省份名、城市名、区域名、房源介绍、房源小区、户型、朝向、楼层、建筑面积、建造时间、单价、楼盘链接

  数据库设计:province、city、area、house四张表

  爬虫spider部分demo:

获取省份、城市信息和链接

 1 #获取省份名字,城市的链接url
 2     def mycity(self,response):
 3          #获得关键节点
 4         links = response.css('#c02 > ul > li')
 5         for link in links:
 6             try:
 7                 province_name=link.xpath('./strong/text()').extract_first()
 8                 urllinks=link.xpath('./a')
 9                 for urllink in urllinks:
10                     city_url=urllink.xpath('./@href').extract_first()
11                     if city_url[-1]=='/':
12                         city_url=city_url[:-1]
13                     yield scrapy.Request(url=city_url,meta={'province_name':province_name,'city_url':city_url},callback=self.area)
14             except Exception:
15                 pass

获取区域的链接url和信息 

1 #获取区域的链接url
2     def area(self,response):
3         try:
4             links=response.css('.qxName a')
5             for link in links[1:]:
6                 area_url=response.url+link.xpath('@href').extract_first()
7                 yield scrapy.Request(url=area_url,meta=response.meta,callback=self.page)
8         except Exception:
9             pass

View Code

获取楼盘房源的信息

 1     def houselist(self,response):
 2         item={}
 3         city_name = response.css('#list_D02_01 > a:nth-child(3)::text').extract_first()
 4         area_name=response.css('#list_D02_01 > a:nth-child(5)::text').extract_first()
 5         if city_name:
 6             item['city_name']=city_name[:-3]
 7         if area_name:
 8             item['area_name']=area_name[:-3]
 9         links=response.xpath('/html/body/div[3]/div[4]/div[5]/dl')
10         if  links:
11             for link in links:
12                 try:
13                     item['title']=link.xpath('./dd/p[1]/a/text()').extract_first()
14                     house_info=link.xpath('./dd/p[2]/text()').extract()
15                     if house_info:
16                         item['province_name']=response.meta['province_name']
17                         item['house_type']=link.xpath('./dd/p[2]/text()').extract()[0].strip()
18                         item['floor']=link.xpath('./dd/p[2]/text()').extract()[1].strip()
19                         item['oritenation']=link.xpath('./dd/p[2]/text()').extract()[2].strip()
20                         item['build_time']=link.xpath('./dd/p[2]/text()').extract()[3].strip()[5:]
21                         item['house_name']=link.xpath('./dd/p[3]/a/span/text()').extract_first()
22                         item['house_area']=link.xpath('./dd/div[2]/p[1]/text()').extract_first()
23                         item['per_price']=int(link.xpath('./dd/div[3]/p[2]/text()').extract_first()[:-1])
24                         list_url = link.xpath('./dd/p[1]/a/@href').extract_first()
25                         item['house_url']=response.meta['city_url']+list_url
26                         yield item
27                 except Exception:
28                     pass

View Code

此时就可以运行scrapy crawl+爬虫名,我们就可以爬取到网站的信息,但是我们如何使用这些数据呢,那就要通过pipelines将数据插入到数据库中。

爬虫pipelines部分demo:

 1 # -*- coding: utf-8 -*-
 2
 3 # Define your item pipelines here
 4 #
 5 # Don't forget to add your pipeline to the ITEM_PIPELINES setting
 6 # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
 7 import pymysql
 8
 9 class HousePipeline(object):
10     def open_spider(self,spider):
11         self.con=pymysql.connect(user='root',passwd='123',db='test',host='localhost',port=3306,charset='utf8')
12         self.cursor=self.con.cursor(pymysql.cursors.DictCursor)
13         return spider
14     def process_item(self, item, spider):
15         #插入省份表
16         province_num=self.cursor.execute('select * from home_province where province_name=%s',(item['province_name'],))
17         if province_num:
18             province_id=self.cursor.fetchone()['id']
19         else:
20             sql='insert into home_province(province_name) values(%s)'
21             self.cursor.execute(sql,(item['province_name']))
22             province_id=self.cursor.lastrowid
23             self.con.commit()
24         #插入城市表
25         ##规避不同省份城市重名的情况
26         city_num=self.cursor.execute('select * from home_city where city_name=%s and province_id=%s',(item['city_name'],province_id))
27         if city_num:
28             city_id=self.cursor.fetchone()['id']
29         else:
30             sql='insert into home_city(city_name,province_id) values(%s,%s)'
31             self.cursor.execute(sql,(item['city_name'],province_id))
32             city_id=self.cursor.lastrowid
33             self.con.commit()
34         #插入区域表
35         ##规避不同城市区域重名的情况
36         area_num=self.cursor.execute('select * from home_area where area_name=%s and city_id=%s',(item['area_name'],city_id))
37         if area_num:
38             area_id=self.cursor.fetchone()['id']
39         else:
40             sql = 'insert into home_area (area_name,city_id,province_id)value(%s,%s,%s)'
41             self.cursor.execute(sql,(item['area_name'],city_id,province_id))
42             area_id = self.cursor.lastrowid
43             self.con.commit()
44         #插入楼盘信息表
45         house_num=self.cursor.execute('select house_name from home_house where house_name=%s',( item['house_name'],))
46         if house_num:
47             pass
48         else:
49             sql = 'insert into home_house(title,house_type,floor,oritenation,build_time,house_name,house_area,per_price,house_url,area_id,city_id,province_id) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'
50             self.cursor.execute(sql, (
51             item['title'], item['house_type'], item['floor'], item['oritenation'], item['build_time'],
52             item['house_name'], item['house_area'], item['per_price'],item['house_url'], area_id,city_id,province_id,))
53             self.con.commit()
54         return item
55     def close_spider(self,spider):
56         self.cursor.close()
57         self.con.close()
58         return spider

View Code

采集数据效果:

  

  

转载于:https://www.cnblogs.com/gentleman-shao/p/8994141.html

爬虫Scrapy框架运用----房天下二手房数据采集相关推荐

  1. 使用scrapy框架实现,房天下网站全站爬取,详情,动态,评论,户型,图片.

    scrapy  实现代码,代码有点多,没有优化,,下面有链接,不懂得留言 Github全部代码,https://github.com/Agile929/scrapy_fang # -*- coding ...

  2. Python房价分析和可视化<房天下二手房>

    Python房价分析和可视化<房天下二手房> 本文是Python数据分析实战的房价分析系列,本文分析二线城市贵阳的二手房. 数据获取 本文的数据来源于2022年8月房天下的二手房数据.对数 ...

  3. python cookie池_Python爬虫scrapy框架Cookie池(微博Cookie池)的使用

    下载代码Cookie池(这里主要是微博登录,也可以自己配置置其他的站点网址) 下载代码GitHub:https://github.com/Python3WebSpider/CookiesPool 下载 ...

  4. python scrapy爬虫视频_python爬虫scrapy框架的梨视频案例解析

    之前我们使用lxml对梨视频网站中的视频进行了下载 下面我用scrapy框架对梨视频网站中的视频标题和视频页中对视频的描述进行爬取 分析:我们要爬取的内容并不在同一个页面,视频描述内容需要我们点开视频 ...

  5. Python爬虫 scrapy框架爬取某招聘网存入mongodb解析

    这篇文章主要介绍了Python爬虫 scrapy框架爬取某招聘网存入mongodb解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 创建项目 sc ...

  6. python爬虫--Scrapy框架--Scrapy+selenium实现动态爬取

    python爬虫–Scrapy框架–Scrapy+selenium实现动态爬取 前言 本文基于数据分析竞赛爬虫阶段,对使用scrapy + selenium进行政策文本爬虫进行记录.用于个人爬虫学习记 ...

  7. Python爬虫—Scrapy框架—Win10下载安装

    Python爬虫-Scrapy框架-Win10下载安装 1. 下载wheel 2.下载twisted 3. 下载pywin32 4. 下载安装Scrapy 5. 创建一个scrapy项目 6. fir ...

  8. Python爬虫-Scrapy框架(四)- 内置爬虫文件 - 4.2 初探Crawl Spider

    Python爬虫-Scrapy框架(四)- 内置爬虫文件 - 4.2 初探Crawl Spider 写在前面 初探Crawl Spider 创建Crawl Spider项目 对比Basic与Crawl ...

  9. 爬虫实战-爬取房天下网站全国所有城市的新房和二手房信息(最新)

    看到https://www.cnblogs.com/derek1184405959/p/9446544.html项目:爬取房天下网站全国所有城市的新房和二手房信息和其他博客的代码,因为网站的更新或者其 ...

最新文章

  1. python基础4(来自廖雪峰的官方网站)
  2. ALGO-117_蓝桥杯_算法训练_友好数
  3. 2017年第八届蓝桥杯C/C++ C组国赛 —— 第三题:表达式计算
  4. Jquery 对话框确认
  5. HALCON示例程序count_pellets.hdev分割豆子,基本形态学的使用
  6. GARFIELD@03-26-2005
  7. 220v转5v阻容降压电路
  8. java swing 实现下拉列表点击事件
  9. 计算机网络系统是弱电工程,弱电工程包括哪些类型?常见的问题又有哪些?
  10. 一致性哈希 php redis,使用一致性哈希实现Redis分布式部署
  11. ubuntu下cpu以最大频率运行、查看CPU主频几种方法
  12. 达观数据推出智能审单机器人,全面提升航运物流效率
  13. 永磁同步电机市场现状及未来发展趋势
  14. iOS自动化测试之ipa安装失败的日志分析
  15. Leetcode_6_Dynamic Programming_198,213打家劫室,打家劫室II
  16. 读研攻略(7)—从0到1,研究生第一篇SCI的诞生
  17. fedora 20 安裝 及 配置桌面環境
  18. 全球哪儿找工作最容易? 新加坡居榜首香港排第九
  19. 数据采集与管理【13】
  20. cPanel主机空间安装Magento规范准确详细教程

热门文章

  1. 如何用cmd安装Python库
  2. 多元思考科学决策!建立高品质思维的30种模型
  3. 【刷题打卡】day7-BFS
  4. Ubuntu systemd配置文件/etc/systemd/system被删解决方案
  5. 杰理之不带功放,隔直推耳机产品【篇】
  6. python怎么让输出的数字右对齐_Python格式化输出的精度和位数控制
  7. 计算机无法连接网络错误651,宽带连接错误651怎么解决?
  8. 【Python】检测下载不完整、半截灰色的JPG、JPEG、PNG图片脚本
  9. webpack 打包报错:Can't resolve '.\dist\bundle.js' in 'E:\vivian....'
  10. selenium-爬取公众号0.2