Scrapy高级用法

日志相关

一、日志相关变量

LOG_LEVEL = ''
LOG_FILE = '文件名.log'

二、日志级别

5 CRITICAL :严重错误
4 ERROR :普通错误
3 WARNING :警告
2 INFO :一般信息
1 DEBUG :调试信息

注意: 只显示当前级别的日志和比当前级别日志更严重的

三、其他导出

设置数据导出编码(主要针对于json文件)

FEED_EXPORT_ENCODING = ''

4、非结构化数据存储路径

IMAGES_STORE = '路径'

5、设置User-Agent

USER_AGENT = ''

数据持久化

将数据保存的方法

一、MySQL

有关MySQL请访问MySQL

  • 在setting.py中定义相关变量

    # mysql 配置
    MYSQL_HOST = 'xxx.xxx.xxx.xxx'
    MYSQL_USER = 'xxxx'
    MYSQL_PWD = 'xxxxx'
    MYSQL_DB = 'xxxxx'
    MYSQL_CHAR = 'utf8'
    
  • pipelines.py中新建管道类,并导入settings模块

    class Mysql(object):
        def open_spider(self,spider):
            # 爬虫开始执行1次,用于数据库连接
        def process_item(self,item,spider):
            # 用于存储抓取的item数据
        def close_spider(self,spider):
            #爬虫结束时执行1次,用于断开数据库连接
    
  • settings.py中添加此管道
    ITEM_PIPELINES = {'':200}

    注意 :process_item() 函数中一定要 return item ***

第一个管道返回的item会继续交由下一个管道处理,否则返回并传入下一个管道的值为None

二、MongoDB

有关MongoDB请访问MongoDB

  • 在settings.py中定义相关变量

    # MongoDB配置
    MONGO_HOST = '10.0.0.7'
    MONGO_PORT = 27017
    
  • pipelines.py中新建管道类,并导入settings模块

    class Mysql(object):
        def open_spider(self,spider):
            # 爬虫开始执行1次,用于数据库连接
        def process_item(self,item,spider):
            # 用于存储抓取的item数据
        def close_spider(self,spider):
            #爬虫结束时执行1次,用于断开数据库连接
    
  • settings.py中添加此管道

三、csv

命令格式

scrapy crawl maoyan -o maoyan.csv

csv

四、json

scrapy crawl maoyan -o maoyan.json

这里运行之后,打开maoyan.json

这里是编码的问题,需要在settings.py中指定编码为utf-8

# settings.py
FEED_EXPORT_ENCODING = 'utf-8'

命令行

一、 基本使用

scrapy shell URL地址
request.headers :请求头(字典)
reqeust.meta :item数据传递,定义代理(字典)
response.text :字符串
response.body :bytes
response.xpath('')

二、Requests

1、url
2、callback
3、headers
4、meta :传递数据,定义代理
5、dont_filter :是否忽略域组限制
默认False,检查allowed_domains['']

设置中间件

一、User-Agent

编辑middlewares.py

from fake_useragent import UserAgent

class RandomUseragentMiddleware(object):
	def process_request(self,reuqest,spider):
		request.headers['User-Agent'] = UserAgent().random

设置优先级settings.py

DOWNLOADER_MIDDLEWARES = {'xxx.xxxx.xxx.RandomUseragentMiddleware' : 优先级}

二、代理

编辑middlewares.py

class RandomProxyDownloadMiddlewares(object):
    def process_request(self, reques, spider):
        proxy = 'http://127.0.0.1:8000'
        request.meta['proxy'] = proxy
    def process_exception(self, request, exception, spider):
        # 如果代理不可用,则交给此函数处理
		return request

三、并发

编辑settings.py

CONCURRENT_REQUESTS = 32

默认并发数为16,可根据自身配置酌情设置

四、延时

下载延迟时间(每隔多长时间请求一个网页)

DOWNLOAD_DELAY 会影响 CONCURRENT_REQUESTS,不能使并发显现

有CONCURRENT_REQUESTS,没有DOWNLOAD_DELAY: 服务器会在同一时间收到大量的请求

有CONCURRENT_REQUESTS,有DOWNLOAD_DELAY 时,服务器不会在同一时间收到大量的请求

DOWNLOAD_DELAY = 3

非结构化数据

spider: yield item['链接']
    
pipelines.py
	from scrapy.pipelines.images import ImagesPiprline
    class SpiderPipeline(ImagesPipe):
        def get_media_requests(self, item, info):
            yield scrapy.Request(item['链接'])

settings.py
	IMAGES_STROE = ''
    ITEM_PIPELINES = {}
    
posted @ 2019-09-21 10:16  ChanceySolo  阅读(598)  评论(0编辑  收藏  举报