上篇中我们获取了重庆的一二级区(Scrapy爬取重庆安居客二手房并存入mysql数据库(上)),这一篇我们根据二级区获取相应的二手房信息。

初始化数据库

创建二手房信息数据库表,house表存放二手房信息,house_price存放价格(定期获取分析价格趋势):

CREATE TABLE `house` (`id` int UNSIGNED AUTO_INCREMENT,`area_id` int NOT NULL DEFAULT 0,`area_code` varchar(255) DEFAULT NULL,`title` varchar(2000) DEFAULT NULL,`unit_price` decimal(19,4) NOT NULL DEFAULT 0,`total_price` decimal(19,4) NOT NULL DEFAULT 0,`code` varchar(255) DEFAULT NULL,`community` varchar(255) DEFAULT NULL,`location` varchar(255) DEFAULT NULL,`build_years` varchar(255) DEFAULT NULL,`floor` varchar(255) DEFAULT NULL,`layout` varchar(255) DEFAULT NULL,`size` varchar(255) DEFAULT NULL,`picture_url` varchar(255) DEFAULT NULL,`url` varchar(1024) DEFAULT NULL,`created_on` timestamp DEFAULT current_timestamp,`updated_on` timestamp DEFAULT current_timestamp on update current_timestamp,PRIMARY KEY (id)
) ENGINE=INNODB DEFAULT CHARSET=utf8;CREATE TABLE `house_price` (`id` int UNSIGNED AUTO_INCREMENT,`house_id` int NOT NULL DEFAULT 0,`house_code` varchar(255) DEFAULT NULL,`unit_price` decimal(19,4) NOT NULL DEFAULT 0,`total_price` decimal(19,4) NOT NULL DEFAULT 0,`created_on` timestamp DEFAULT current_timestamp,`updated_on` timestamp DEFAULT current_timestamp on update current_timestamp,PRIMARY KEY (id)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

创建项目

创建二手房scrapy项目:

scrapy startproject hourse

项目创建成功后,目录结构如下:

定义item

打开items.py文件,定义二手房信息item:

import scrapyclass HourseItem(scrapy.Item):area_code = scrapy.Field()title = scrapy.Field()unit_price = scrapy.Field()total_price = scrapy.Field()code = scrapy.Field()community = scrapy.Field()location = scrapy.Field()build_years = scrapy.Field()floor = scrapy.Field()layout = scrapy.Field()size = scrapy.Field()picture_url = scrapy.Field()url = scrapy.Field()

开发spider

在spiders目录中新建二手房爬虫的spider文件housespider.py,根据页面结构使用css选择器解析处理数据:

import scrapy
import re
import pymysql
from hourse.items import HourseItemclass HouseSpider(scrapy.Spider):name = 'house'allow_domains = ["anjuke.com"]# start_urls = [#     'https://chongqing.anjuke.com/sale/p1/',# ]def __init__(self):self.db = pymysql.connect("localhost", "root", "123456", "house", charset="utf8")self.cursor = self.db.cursor()# 从数据库读取区域,按照区域查询数据results = self.get_areas()urls = []for area_item in results:urls.append('https://chongqing.anjuke.com/sale/' + area_item[1] + '/')# breakself.start_urls = urlsself.enable_next_page = Truedef close(self):self.db.close()def get_areas(self):# 区域数量太多,会触发人机校验,请求连接会跳转到人工操作页面,这里添加条件只获取部分区域select_sql = "select * from house_area where parent_id > 0"# select_sql = "select * from house_area where id = 1"self.cursor.execute(select_sql)results = self.cursor.fetchall()return resultsdef parse(self, response):house_list = response.css('li.list-item')house_item = HourseItem()house_item['area_code'] = ''current_url = re.search('sale/([^/]+)/', response.url)if current_url:house_item['area_code'] = current_url.group(1)pat_code = '/([a-zA-Z0-9]+)\?'for item in house_list:house_item['title'] = item.css('.house-title a::text').extract_first().strip()house_item['url'] = item.css('.house-title a::attr(href)').extract_first().strip()# 总价万为单位house_item['total_price'] = re.search('\d+', item.css('.pro-price .price-det').extract_first().strip()).group()house_item['code'] = ''if house_item['url']:search_code = re.search(pat_code, house_item['url'])if search_code:house_item['code'] = search_code.group(1)house_item['community'] = ''house_item['location'] = item.css('.details-item:nth-child(3) span.comm-address::attr(title)').extract_first().replace(u'\xa0', u' ').strip()if house_item['location']:address_items = re.split('\s+', house_item['location'])if len(address_items) > 1:house_item['community'] = address_items[0]house_item['build_years'] = item.css('.details-item:nth-child(2) span:nth-child(7)::text').extract_first().strip()house_item['floor'] = item.css('.details-item:nth-child(2) span:nth-child(5)::text').extract_first().strip()house_item['layout'] = item.css('.details-item:nth-child(2) span:nth-child(1)::text').extract_first().strip()house_item['size'] = item.css('.details-item:nth-child(2) span:nth-child(3)::text').extract_first().strip()house_item['picture_url'] = item.css('.item-img img::attr(src)').extract_first().strip()house_item['unit_price'] = re.search('\d+', item.css('.pro-price .unit-price::text').extract_first().strip()).group()yield house_item#分页操作next_page = response.css('.multi-page a.aNxt::attr(href)').extract_first()if self.enable_next_page and next_page:#构建新的Request对象next_url = next_page.strip()yield scrapy.Request(next_url, callback=self.parse)

开发pipeline

打开pipelines.py文件,将spider中爬取处理的数据存入数据库:

import pymysqlclass HoursePipeline(object):def __init__(self):self.db = pymysql.connect("localhost", "root", "123456", "house", charset="utf8")self.cursor = self.db.cursor()def process_item(self, item, spider):select_area_sql = "select id from house_area where code='%s'" % item['area_code']is_area_exist = self.cursor.execute(select_area_sql)house_area = self.cursor.fetchone()area_id = 0if house_area:area_id = house_area[0]select_sql = "select id from house where code='%s'" % item['code']already_save = self.cursor.execute(select_sql)house_item = self.cursor.fetchone()self.db.commit()if already_save == 1:# 更新信息house_id = house_item[0]update_sql = "update house set title='%s', unit_price='%d', total_price='%d', url='%s' where id='%d'" % (item['title'],int(item['unit_price']),int(item['total_price']),item['url'],int(house_id))self.cursor.execute(update_sql)self.db.commit()# 插入价格self.add_price(house_id, item['code'], item['unit_price'], item['total_price'])  else:# 插入信息sql = "insert into house(area_id,area_code,title,unit_price,total_price,code,community,location,build_years,floor,layout,size,picture_url,url)\values('%d','%s','%s','%d','%d','%s','%s','%s','%s','%s','%s','%s','%s','%s')"\%(area_id,item['area_code'],item['title'],int(item['unit_price']),int(item['total_price']),item['code'],item['community'],item['location'],\item['build_years'],item['floor'], item['layout'],item['size'],item['picture_url'],item['url'])self.cursor.execute(sql)house_id = int(self.db.insert_id())self.db.commit()# 插入价格self.add_price(house_id, item['code'], item['unit_price'], item['total_price'])  return itemdef add_price(self, house_id, house_code, unit_price, total_price):sql = "insert into house_price(house_id, house_code, unit_price, total_price)\values('%d','%s','%d','%d')" % (int(house_id), house_code, int(unit_price), int(total_price))self.cursor.execute(sql)self.db.commit() def __del__(self):self.db.close()

设置settings

打开settings.py文件,这只项目的USER_AGENT和ITEM_PIPELINES,其余设置保持不变:

USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36'ITEM_PIPELINES = {'hourse.pipelines.HoursePipeline': 300,
}

启动爬虫

在项目根目录下,在命令行中运行如下命令开始爬虫程序(博主在vscode中项目下直接运行的命令):

scrapy crawl house

至此使用scrapy获取重庆的二手房数据已经完成,博主才入坑python,通过该项目学习了scrapy框架和一些基础的python语法。如果有不妥的笛梵,欢迎大家提意见。

项目的Github地址:https://github.com/liuhuifly/scrapy_cqanjuke,数据库初始化脚本在hourse项目的sqlscripts目录下。

声明:本项目仅仅供学习使用,使用该项目从事的一切商业行为与博主无关,自行承担责任。

Scrapy爬取重庆安居客二手房并存入mysql数据库(下)相关推荐

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

    scrapy是什么 Scrapy是Python开发的一个快速.高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化的数据. 官网地址:https://scrapy.org/ 官方文 ...

  2. python—简单数据抓取七(采取蘑菇API代理设置scrapy的代理IP池并利用redis形成队列依次使用,利用ip池访问网页并将scrapy爬取转移到items的数据存入到数据库)

    学习目标: Python学习二十七-简单数据抓取七 学习内容: 1.采取蘑菇API代理设置scrapy的代理IP池并利用redis形成队列依次使用 2.利用ip池访问网页并将scrapy爬取转移到it ...

  3. scrapy爬取京东商品评论并保存至Mysql数据库中

    scrapy爬取京东商品评论并保存至Mysql数据库 一.总体概述 二.实践过程 2.1网页解析 2.2使用单线程爬取代码如下: 2.3使用scrapy爬取数据 2.4绘制词云图结果: 三.总结 一. ...

  4. Scrapy爬取当当网的商品信息存到MySQL数据库

    Scrapy爬取当当网的商品信息存到MySQL数据库 Scrapy 是一款十分强大的爬虫框架,能够快速简单地爬取网页,存到你想要的位置.经过两天的摸索,终于搞定了一个小任务,将当当网的商品信息爬下来存 ...

  5. python爬虫:爬取动态网页并将信息存入MySQL数据库

    目标网站 http://www.neeq.com.cn/disclosure/supervise.html 爬取网页该部分内容 网页分析 查看网页源代码发现没有表格部分内容,对网页请求进行分析 F12 ...

  6. 利用Scrapy爬取伯乐在线文章并存取到mysql数据库

    1.观察网址直接从(http://blog.jobbole.com/all-posts/)入手爬取伯乐在线所有文章,常规cmd创建项目 2.spider中采取xpath和css选择器提取语法,提取出想 ...

  7. mysql好评中评统计_scrapy爬取京东笔记本及评论信息存入MySQL数据库

    爬取思路 1.分析页面,定义爬取字段 2.观察网页,分析接口url,通过xpath和json解析爬取内容字段 3.在pipelines.py写入存储方式 4.开始爬取 5.GitHub地址:https ...

  8. python-爬虫,实现输入关键字,然后爬取关键字主页代码并存储到mysql数据库

    python-爬虫,实现输入关键字,然后爬取关键字主页代码并存储到mysql数据库 实现代码如下: 代码是可以实现的,有问题可以私聊我 import os import sys import base ...

  9. python + selenium +chrome爬取qq空间好友说说并存入mongodb数据库

    python + selenium +chrome爬取qq空间好友说说并存入mongodb数据库 准备阶段 在正式开始在前需要先准备好做爬虫的工具,本例使用chrome无头浏览器进行爬取工作,也可使用 ...

最新文章

  1. AI的使用场景破冰开拓商业服务
  2. java线程池的使用例子,不愧是大佬
  3. Linux系统下挂载Windows分区
  4. Junit源码阅读(四)之自定义扩展
  5. 大神 20 年的专业数据分析心法全都在这里了
  6. Telnet服务器的系统要求,Telnet 服务器概述
  7. 下载npm并配置npm环境
  8. Android ContactsProvider源码分析
  9. 基于Java的愤怒的小鸟游戏的设计与实现
  10. 在html编辑器中插入css,怎么给kindeditor编辑器添加引用(blockquote)标签,并选择css样式...
  11. Unity Kinect运行 FaceTracking 场景 c++ Runtime Error
  12. 翻出过去的一个多彩泡泡屏保特效(JS+CSS版)
  13. 'dict' object has no attribute '_txn_read_preference' Sort exceeded memory limit of 10485760
  14. 什么是位域?位域如何定义?一般什么时候使用?
  15. 注册验证码短信收不到是怎么回事
  16. 优质的客户期货开户交易所返还高
  17. 沉降观测曲线图 沉降观测汇总_沉降观测曲线图都有哪些
  18. [测开篇]设计测试用例的方法如何正确描述Bug
  19. 无符号数和带符号整数的表示
  20. python皮森发音_python 怎么读

热门文章

  1. php三种修饰符,PHP中的权限修饰符 | 萧小寒
  2. Android RK3399 UVC摄像头格式异常
  3. 浅析推动可持续社会价值创新
  4. MATLAB颜色阈值工具箱(Color Thresholder)介绍
  5. unity3d游戏场景制作
  6. 用python读取csv文件并绘制波形及频谱
  7. 【亲测】回合制手游魔力宝贝【法兰城的回忆】最新整理Linux手工服务端+视频教程+GM授权后台
  8. iPS细胞的癌症治疗进展到什么程度?
  9. 服务数据库已锁定 两个可能
  10. C++11 条件变量