scrapy爬取上海宝山安居客房产信息并存到mysql数据库中

源码下载:https://download.csdn.net/download/dabao87/11997988

首先搭建虚拟环境和安装python这里就不说了,不会的请移步我的其他文章

安装虚拟环境:https://blog.csdn.net/dabao87/article/details/102743386

创建一个项目,命令行:

scrapy startproject anjuke

这命令会建一个叫anjuke的文件夹,里面会有一些待你配置的文件

创建一个spider:

先进入刚才创建的项目文件夹里

cd anjuke
scrapy genspider anju shanghai.anjuke.com

这时的文件夹结构应该是这样的:

创建item

item是保存爬取数据的容器,使用方法和字典类似~

将item.py修改如下:

配置settings.py 

在 settings.py 中配置几个重要的东西,由于安居库有防止爬虫访问,所以要模拟用户的正常访问

将 settings.py 文件中USER_AGENT参数打开

将刚才复制的 user-agent 放到 settings.py 的USER_AGENT参数中

这个打开

COOKIES_ENABLED = False

引入 import random

DOWNLOAD_DELAY = random.choice([1,2])

这个时候配置完成了,访问就可以模拟用户的正常访问了

在  anju.py 文件中的代码

# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request
from anjuke.items import AnjukeItem  # 使用item


class AnjuSpider(scrapy.Spider):
    name = 'anju'
    allowed_domains = ['shanghai.anjuke.com']
    start_urls = ['https://shanghai.anjuke.com/sale/baoshan/m2401/']

    def parse(self, response):
        divs = response.xpath('''//li[@class="list-item"]''')  # 使用xpath从response中获取需要的html块
        for div in divs:
            item = AnjukeItem()  # 实例化item对象
            address = div.xpath('.//span[@class="comm-address"]/@title').extract_first()  # 楼盘地址和小区名称,由于地址和小区名称在一起,所以下面是拆分
            item['address'] = address[address.index("\xa0\xa0") + 2 :]  #地址,以“\xa0\xa0”区分
            item['name'] = address[:address.index("\xa0\xa0")]  #小区名称
            try:
                item['type_'] = div.xpath('.//div[@class="details-item"]/span/text()').extract_first()  # 房子类型比如两房一厅这样子~
            except:
                pass

            # item['tags'] = div.xpath('.//span[@class="item-tags tag-metro"]/text()').extract()  # 网站给楼盘定的标签~

            price = div.xpath('.//span[@class="price-det"]/strong/text()').extract_first()  # 价格
            item['price'] = price + '万'
            try:
                item['area'] = div.xpath('.//div[@class="details-item"]/span/text()').extract()[1:2]
            except:
                pass
            yield item
            
        next_ = response.xpath('//div[@class="multi-page"]/a[@class="aNxt"]/@href').extract_first()  # 获取下一页的链接
        print('-------next----------')
        print(next_)
        yield response.follow(url=next_, callback=self.parse)  # 将下一页的链接加入爬取队列~~

    

执行:

scrapy crawl anju

 

这里名字是anju,所以上面的命令要是 anju 

结果:

安居客网站的标签和class可能会改变。不要直接复制我的代码,可能会不能执行

 

存入mysql

参考:https://www.cnblogs.com/ywjfx/p/11102081.html

新建数据库

scrapy

新建表

anjuke

在 pipelines.py 文件中

import pymysql

class AnjukePipeline(object):
    def __init__(self):
        # 连接MySQL数据库
        self.connect=pymysql.connect(host='127.0.0.1',user='root',password='111111',db='scrapy',port=3306)
        self.cursor=self.connect.cursor()
    def process_item(self, item, spider):
        # 往数据库里面写入数据
        self.cursor.execute('insert into anjuke(address,name,type_,area,price)VALUES ("{}","{}","{}","{}","{}")'.format(item['address'],item['name'],item['type_'],item['area'],item['price']))
        self.connect.commit()
        return item
    # 关闭数据库
    def close_spider(self,spider):
        self.cursor.close()
        self.connect.close()

在settings.py中

主要是将 ITEM_PIPELINES 的注释去掉

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

# Scrapy settings for tencent project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://doc.scrapy.org/en/latest/topics/settings.html
#     https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://doc.scrapy.org/en/latest/topics/spider-middleware.html

BOT_NAME = 'tencent'

SPIDER_MODULES = ['tencent.spiders']
NEWSPIDER_MODULE = 'tencent.spiders'

LOG_LEVEL="WARNING"
LOG_FILE="./qq.log"
# Crawl responsibly by identifying yourself (and your website) on the user-agent
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'

# Obey robots.txt rules
#ROBOTSTXT_OBEY = True

# Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32

# Configure a delay for requests for the same website (default: 0)
# See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
#DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16

# Disable cookies (enabled by default)
#COOKIES_ENABLED = False

# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False

# Override the default request headers:
#DEFAULT_REQUEST_HEADERS = {
#   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#   'Accept-Language': 'en',
#}

# Enable or disable spider middlewares
# See https://doc.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
#    'tencent.middlewares.TencentSpiderMiddleware': 543,
#}

# Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
#    'tencent.middlewares.TencentDownloaderMiddleware': 543,
#}

# Enable or disable extensions
# See https://doc.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
#}

# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
   'tencent.pipelines.TencentPipeline': 300,
}

# Enable and configure the AutoThrottle extension (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'



# 连接数据MySQL
# 数据库地址
MYSQL_HOST = 'localhost'
# 数据库用户名:
MYSQL_USER = 'root'
# 数据库密码
MYSQL_PASSWORD = 'yang156122'
# 数据库端口
MYSQL_PORT = 3306
# 数据库名称
MYSQL_DBNAME = 'test'
# 数据库编码
MYSQL_CHARSET = 'utf8'

 

下面要注意:

查看有没有安装pymysql

pip list

我已经安装好了,但是在爬取提示:ModuleNotFoundError: No module named 'pymysql'

这个时候要注意是否是在安装python中安装的

执行

where python

我有两个安装目录:一个是安装的python,一个是虚拟环境。刚才我在虚拟换in中安装了pymysql,所以提示没有pymysql模块,

这个时候我切换到俺咋混个python的环境中,再次执行

pip list

发现没有pymysql这个模块,赶紧装上

注意:一定要在Scripts这个文件下安装

执行

pip install pymysql

再次查看他有没有安装上 pymysql

pip list

这个时候应该是安装好了

在回到刚才爬虫的项目中,执行爬虫

scrapy crawl anju

这时就已经可以存到数据库了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 安居客出租房(武汉为例)虫+数据分析+可视化 这个虫是我前段时间在淘宝上做单子的时候遇见的一个客户需求。本来以为就是一个简单的虫项目。但后面客户加了数据清洗和数据分析的要求。而后又加了要详细代码解释的需求等等。直到最后客户坦白说这是他们大专的毕设.......但是这个单子坐下来只有200左右,我想了一下,感觉好亏啊。在淘宝上随便找一个做毕设的都要好多钱的,而且客户本身的代码能力、数学、逻辑能力都很差,导致我每行都给注释以及看不懂,在我交付代码后又纠缠了我一个多礼拜。反正总体做下来的感觉就是烦躁。头一次感觉到了客户需求变更带来的巨大麻烦。 总之这是一次不是很愉快的虫经历。但是作为我写虫以来注释最详细的一次,以及第一次真正使用像matplotlib这种数据分析库的代码,我认为还是有必要分享出来给大家当个参考的(PS:大佬轻拍~)。虫本身几乎没有什么难度,写的也比较乱,敬请见谅。 **功能** 爬取安居客上的出租房信息(武汉地区的),并通过爬取的数据进行数据清洗以及数据分析。给出四个不同层面的可视化图。最终结果如下图所示: ![Image text](https://raw.githubusercontent.com/srx-2000/git_spider/master/anjuke_room_rent_info/result/1.png) ![Image text](https://raw.githubusercontent.com/srx-2000/git_spider/master/anjuke_room_rent_info/result/2.png) ![Image text](https://raw.githubusercontent.com/srx-2000/git_spider/master/anjuke_room_rent_info/result/3.png) ![Image text](https://raw.githubusercontent.com/srx-2000/git_spider/master/anjuke_room_rent_info/result/4.png) **环境** 1. Windows 10 2. python3.7 **使用方法** 首先声明该虫由于是特定情况下写的,所以本身的通用性特别差,仅可以对安居客网站上的武汉的出租房信息进行爬取,且需要自己手动更新cookie。同时在对数据进行分析及可视化的时候由于也是特别针对武汉出租房的进行的,所以针对性也比较强。如果别的需求需要自己进行更改。 1. 访问[安居客网址](https://wuhan.anjuke.com/),获取cookie。 > tip:获取cookie的方法可根据[此链接](https://jingyan.baidu.com/article/5d368d1ea6c6e33f60c057ef.html) 2. 在项目找到`spider.py`的文件,将第12行的cookie换成你自己的cookie。 3. 运行`spider.py`,获取房源信息。运行后应会产生一个`武汉出租房源情况.csv`的文件。此文件为我们从安居客爬取的房源信息,其包含`房屋租住链接、房屋描述、房屋地址、房屋详情(户型)以及经纪人、房屋价格`五个属性。 4. 在获取了数据之后我们运行`matplotlib.py`文件。进行数据清洗,分析,可视化。运行后即可获得**功能**展示四个图片。 **技术栈** 1. request 2. parsel 3. pandas 4. matplotlib **进步(相比之前)** 此次虫相比之前的技术上可以说有减无增。但其注释相当详细,可谓是每行代码都有注释。所以对于初学者应该有一些用处。同时使用matplotlib进行了数据分析可视化等。对于数据处理的代码的注释也是几乎每行都有注释的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值