利用scrapy抓取蛋壳公寓上的房源信息,以北京市为例,目标url:https://www.dankegongyu.com/room/bj

思路分析

每次更新最新消息,都是在第一页上显示,因此考虑隔一段时间自动抓取第一页上的房源信息,实现抓取最新消息。

利用redis的set数据结构的特征,将每次抓取后的url存到redis中;

每次请求,将请求url与redis中的url对比,若redis中已存在该url,代表没有更新,忽略该次请求;若redis中不存在该url,代表该信息是新信息,抓取并将url存入到redis中。

分析页面源码,发现该网页属于静态网页;首先获取最新页面每条数据的url,请求该url,得到详细页面情况,所有数据均从详情页面获取。

代码实现

明确抓取字段

# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html import scrapy class DankeItem(scrapy.Item):
"""
编辑带爬取信息字段
"""
# 数据来源
source = scrapy.Field()
# 抓取时间
utc_time = scrapy.Field() # 房间名称
room_name = scrapy.Field()
# 房间租金
room_money = scrapy.Field()
# 房间面积
room_area = scrapy.Field()
# 房间编号
room_numb = scrapy.Field()
# 房间户型
room_type = scrapy.Field()
# 租房方式
rent_type = scrapy.Field()
# 房间楼层
room_floor = scrapy.Field()
# 所在区域
room_loca = scrapy.Field()
# 所在楼盘
estate_name = scrapy.Field()

编写爬虫逻辑

# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from danke.items import DankeItem class DankeSpider(CrawlSpider): # 爬虫名
name = 'dkgy3' # 允许抓取的url
allowed_domains = ['dankegongyu.com'] custom_settings = {'DOWNLOAD_DELAY': 0.2} # 请求开始的url
start_urls = ['https://www.dankegongyu.com/room/sz'] # rules属性
rules = ( #编写匹配详情页的规则,抓取到详情页的链接后不用跟进
Rule(LinkExtractor(allow=r'https://www.dankegongyu.com/room/\d+'), callback='parse_detail', follow=False),
) def parse_detail(self, response):
"""
解析详情页数据
:param response:
:return:
"""
node_list = response.xpath('//div[@class="room-detail-right"]')
for node in node_list:
item = DankeItem() # 房间名称
room_name = node.xpath('./div/h1/text()')
item['room_name'] = room_name.extract_first() # 房间租金
room_money = node.xpath('./div[@class="room-price"]/div/span').xpath('string(.)').extract_first() # 有的房子有首月租金,和普通租金不同,因此匹配方式也不同
if room_money:
item['room_money'] = room_money
else:
room_money = node.xpath('./div[@class="room-price hot"]/div/div[@class="room-price-num"]/text()').extract_first()
item['room_money'] = room_money
print(room_money) # 房间面积
room_area = node.xpath('./*/div[@class="room-detail-box"]/div[1]/label/text()').extract_first().split(':')[-1]
item['room_area'] = room_area # 房间编号
room_numb = node.xpath('./*/div[@class="room-detail-box"]/div[2]/label/text()').extract_first().split(':')[-1]
item['room_numb'] = room_numb # 房间户型
room_type = node.xpath('./*/div[@class="room-detail-box"]/div[3]/label/text()').extract_first().split(':')[-1]
item['room_type'] = room_type # 租房方式
rent_type = node.xpath('./*/div[@class="room-detail-box"]/div[3]/label/b/text()').extract_first().split(':')[
-1]
item['rent_type'] = rent_type # 所在楼层
room_floor = node.xpath('./div[@class="room-list-box"]/div[2]/div[2]').xpath('string(.)').extract_first().split(':')[-1]
item['room_floor'] = room_floor # 所在区域
room_loca = node.xpath('./div[@class="room-list-box"]/div[2]/div[3]/label/div/a[1]/text()').extract_first()
item['room_loca'] = room_loca # 所在楼盘
estate_name = node.xpath('./div[@class="room-list-box"]/div[2]/div[3]/label/div/a[3]/text()').extract_first()
item['estate_name'] = estate_name yield item

编写下载中间件

下载中间件中实现两个逻辑:添加随机请求头和url存入redis中

# -*- coding: utf-8 -*-

# Define here the models for your spider middleware
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/spider-middleware.html
import time
import random
import hashlib
import redis
from scrapy.exceptions import IgnoreRequest
from danke.settings import USER_AGENTS as ua class DankeSpiderMiddleware(object):
def process_request(self, request, spider):
"""
给每一个请求随机分配一个代理
:param request:
:param spider:
:return:
"""
user_agent = random.choice(ua)
request.headers['User-Agent'] = user_agent class DankeRedisMiddleware(object):
"""
将第一个页面上的每一个url放入redis的set类型中,防止重复爬取
"""
# 连接redis
def __init__(self):
self.redis = redis.StrictRedis(host='39.106.116.21', port=6379, db=3) def process_request(self, request, spider): # 将来自详情页的链接存到redis中
if request.url.endswith(".html"):
# MD5加密详情页链接
url_md5 = hashlib.md5(request.url.encode()).hexdigest() # 添加到redis,添加成功返回True,否则返回False
result = self.redis.sadd('dk_url', url_md5) # 添加失败,说明链接已爬取,忽略该请求
if not result:
raise IgnoreRequest

数据存储

# -*- coding: utf-8 -*-

from datetime import datetime
import pymysql class DankeSourcePipeline(object):
def process_item(self, item, spider):
item['source'] = spider.name
item['utc_time'] = str(datetime.utcnow())
return item class DankePipeline(object): def __init__(self): self.conn = pymysql.connect(
host='39.106.116.21',
port=3306,
database='***',
user='***',
password='****',
charset='utf8'
)
# 实例一个游标
self.cursor = self.conn.cursor() def process_item(self, item, spider): sql = ("insert into result_latest(标题, 租金, 面积, "
"编号, 户型, 出租方式, 楼层, "
"区域, 楼盘, 抓取时间, 数据来源)"
"values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)") item = dict(item) data = [
item['room_name'],
item['room_money'],
item['room_area'],
item['room_numb'],
item['room_type'],
item['rent_type'],
item['room_floor'],
item['room_loca'],
item['estate_name'],
item['utc_time'],
item['source'],
]
self.cursor.execute(sql, data)
# 提交数据
self.conn.commit() return item def close_spider(self, spider):
self.cursor.close()
self.conn.close()

实现自动爬取

import os
import time while True:
"""
每隔20*60*60 自动爬取一次,实现自动更新
"""
os.system("scrapy crawl dkgy3")
time.sleep(20*60*60) # from scrapy import cmdline
# cmdline.execute("scrapy crawl dkgy3".split())

完整代码

参见:https://github.com/zInPython/danke

scrapy自动抓取蛋壳公寓最新房源信息并存入sql数据库的更多相关文章

  1. scrapy实现自动抓取51job并分别保存到redis,mongo和mysql数据库中

    项目简介 利用scrapy抓取51job上的python招聘信息,关键词为“python”,范围:全国 利用redis的set数据类型保存抓取过的url,现实避免重复抓取: 利用脚本实现每隔一段时间, ...

  2. 学习笔记CB010:递归神经网络、LSTM、自动抓取字幕

    递归神经网络可存储记忆神经网络,LSTM是其中一种,在NLP领域应用效果不错. 递归神经网络(RNN),时间递归神经网络(recurrent neural network),结构递归神经网络(recu ...

  3. 【VIP视频网站项目】VIP视频网站项目v1.0.3版本发布啦(程序一键安装+电影后台自动抓取+代码结构调整)

    在线体验地址:http://vip.52tech.tech/ GIthub源码:https://github.com/xiugangzhang/vip.github.io 项目预览 主页面 登录页面 ...

  4. SQL Server定时自动抓取耗时SQL并归档数据发邮件脚本分享

    SQL Server定时自动抓取耗时SQL并归档数据发邮件脚本分享 第一步建库和建表 USE [master] GO CREATE DATABASE [MonitorElapsedHighSQL] G ...

  5. IIS崩溃时自动抓取Dump

    背景:在客户现场,IIS有时会崩溃,开发环境没法重现这个bug,唯有抓取IIS的崩溃是的Dump文件分析. IIS崩溃时自动抓取Dump,需要满足下面几个条件 1.启动 Windows Error R ...

  6. 自动抓取java堆栈

    参数1 进程名字,参数2 最大线程数 例: pid为8888,达到1000个线程时自动抓取堆栈信息 ./autojstack.sh 8888 1000 & #!/bin/bashfileNam ...

  7. SQL Server定时自动抓取耗时SQL并归档数据脚本分享

    原文:SQL Server定时自动抓取耗时SQL并归档数据脚本分享 SQL Server定时自动抓取耗时SQL并归档数据脚本分享 第一步建库 USE [master] GO CREATE DATABA ...

  8. APP自动化框架LazyAndroid使用手册(2)--元素自动抓取

    作者:黄书力 概述 前面的一篇博文简要介绍了安卓自动化测试框架LazyAndroid的组成结构和基本功能,本文将详细描述此框架中元素自动抓取工具lazy-uiautomaterviewer的使用方法. ...

  9. 巧用Grafana和Arthas自动抓取K8S中异常Java进程的线程堆栈

    前言 近期发现业务高峰期时刻会出现CPU繁忙导致的timeout异常,通过监控来看是因为Node上面的一些Pod突发抢占了大量CPU导致的. 问: 没有限制CPU吗?是不是限制的CPU使用值就可以解决 ...

随机推荐

  1. C# leetcode 之 096 不同的二叉搜索树

    C# leetcode 之 096 不同的二叉搜索树 题目描述 给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 二叉搜索树定义 左子树上所有节点的值小于根节点, 右子树上左右 ...

  2. 常用 UML 类图

    一. 类 类图分三层: 第一层是类的名称,如果是抽象类,则用斜体显示. 第二层是类的特性,通常就是字段和属性. 第三层是类的操作,通常是方法或行为.注意 '+' 表示 public,'-' 表示 pr ...

  3. 在ArangoDB中实现connectedcomponents算法

    操作环境: tool:ArangoDB 3.3.13 操作系统:Debian 7.2.0-20 概念: Connected Components即连通体算法.用id标注图中每个连通体,将连通体中序号最 ...

  4. CMMS系统中的物联监测

    有条件的设备物联后,可时实查看设备运行状态,如发现异常,可提前干预.

  5. Bootstrap布局基础

     1.栅格系统(布局)Bootstrap内置了一套响应式.移动设备优先的流式栅格系统,随着屏幕设备或视口(viewport)尺寸的增加,系统会自动分为最多12列. 我在这里是把Bootstrap中的栅 ...

  6. docker安装制定版本-centos7

    # 安装依赖包 yum install -y yum-utils device-mapper-persistent-data lvm2 # 添加Docker软件包源 yum-config-manage ...

  7. NOIP模拟27(命悬一线)

    考得太悬了!

  8. 水仙花数[js]

    const getNarcissisticNumbers = function (n) { let min = Math.pow(10, n - 1) - 1 let max = Math.pow(1 ...

  9. Docker学习-Kubernetes - 集群部署

    Docker学习 Docker学习-VMware Workstation 本地多台虚拟机互通,主机网络互通搭建 Docker学习-Docker搭建Consul集群 Docker学习-简单的私有Dock ...

  10. SQL的四种连接(左外连接、右外连接、内连接、全连接)

    1.内联接(典型的联接运算,使用像 =  或 <> 之类的比较运算符).包括相等联接和自然联接.     内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行.例如,检索 stude ...