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 # 使用itemclass 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:passyield itemnext_ = 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 pymysqlclass 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.htmlBOT_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
这时就已经可以存到数据库了
scrapy爬取上海宝山安居客房产信息并存到mysql数据库中相关推荐
- 爬取网贷之家平台数据保存到mysql数据库
# coding utf-8 import requests import json import datetime import pymysqluser_agent = 'User-Agent: M ...
- python爬取淘宝搜索页面+url+图片下载并将信息保存到MySQL数据库中
人狠话不多,直接上代码,都有详细注释,不多解释 需要安装的包: pymysql,用于连接mysql数据库 urllib,爬虫必备包,urllib3也可以 # encoding:utf-8 import ...
- 在ubuntu 16.04里使用python—scrapy将爬取到的数据存到mysql数据库中的一些随笔
一.将爬取的数据保存到mysql数据库的代码(已经能将爬取的数据保存到json文件) (1)编辑Pipeline.py文件 (2)编辑settings.py文件 二.将数据保存至mysql数据库出现的 ...
- 基于python多线程和Scrapy爬取链家网房价成交信息
文章目录 知识背景 Scrapy- spider 爬虫框架 SQLite数据库 python多线程 爬取流程详解 爬取房价信息 封装数据库类,方便多线程操作 数据库插入操作 构建爬虫爬取数据 基于百度 ...
- 一文搞定scrapy爬取众多知名技术博客文章保存到本地数据库,包含:cnblog、csdn、51cto、itpub、jobbole、oschina等
本文旨在通过爬取一系列博客网站技术文章的实践,介绍一下scrapy这个python语言中强大的整站爬虫框架的使用.各位童鞋可不要用来干坏事哦,这些技术博客平台也是为了让我们大家更方便的交流.学习.提高 ...
- Scrapy爬取饿了么周围商家信息
大学生吃土指南 一.实验目的及原理 作为一个被花呗和各种电商节支配的当代大学生,每个月难免有三十几天会陷入吃土的困境.但就算吃土也要吃的优雅,吃的舒心.饿了么上有时会有商家活动,可以以很实惠(baip ...
- 用scrapy+selenium + phantomjs 爬取vip网页,保存为json格式,写入到mysql数据库,下载图片(二)
接上一编 weipin.py文件的代码 : # -*- coding: utf-8 -*- import scrapy from weipinhui.items import WeipinhuiIte ...
- 使用scrapy 爬取酷狗音乐歌手及歌曲名并存入mongodb中
备注还没来得及写,共爬取八千多的歌手,每名歌手平均三十首歌曲算,大概二十多万首歌曲 run.py 1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 ...
- python爬虫:scrapy爬取传智播客教师信息
推荐一个Chrome浏览器的xpath解析工具: xPath helper 轻松获取HTML元素的xPath 打开/关闭控制台:Ctrl-Shift键-X 参考:介绍一款chrome爬虫网页解析工具- ...
最新文章
- 首次用Intellij IDEA打开别人的项目,如何配置Tomcat服务器?
- 腾讯 JDK 11 正式开源,高性能、太牛逼啦!
- @Singleton能保证单例吗
- 如何用python把xlsx变为csv_python将excel转换为csv的代码方法总结
- UUID,加密解密算法的使用
- 东软 软件工程3 软件项目管理 团队组织管理
- 计算机受限制用户,由于该计算机受到限制,本次操作已被取消的解决办法
- mfc界面的onvscroll没反应_电脑小技巧之360安全卫士卸载不掉怎么办?只因一个开关没打开...
- Pwn2Own黑客大赛战况:iPhone 20秒被黑
- 网站安全之为Web项目添加验证码功能(二)
- 爬虫之Beautiful Soup库入门
- zuul网关的过滤器类型
- perfmon的使用及性能分析
- 《旅行青蛙》的代码揭秘,攻略,体验
- 智汇华云 | ArSDN之分布式路由及浮动IP简介
- PHP开发银联云闪付二维码支付
- 中电资讯-银保监会提示防范“元宇宙”风险
- 以人工智能和大数据为核心的第四次工业革命已经悄然而至
- C语言飞机大战程序思路,C语言代码实现飞机大战
- 站在邙山之颠仰望天的那份湛蓝