# -*- coding: utf-8 -*-
import re
from time import sleep import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule class AngelSpider(CrawlSpider):
name = 'angel'
allowed_domains = ['angelimg.spbeen.com']
start_urls = ['http://angelimg.spbeen.com/'] base_url = "http://angelimg.spbeen.com"
rules = (
Rule(LinkExtractor(allow=r'^http://angelimg.spbeen.com/ang/\d+$'), callback='parse_item', follow=False),
) def parse_item(self, response):
print(response.url)
item = response.meta.get('item',False)
if item:
pass
else:
item = {}
item['files'] = []
item['file_urls'] = []
dir_name = response.xpath('.//div[@class="article"]/h2/text()').extract_first()
item['dir_name'] = dir_name.split('【')[0]
item['dir_name'] = re.sub(u"([^\u4e00-\u9fa5\u0030-\u0039\u0041-\u005a\u0061-\u007a])","", item['dir_name']) img_url = response.xpath('.//div[@id="content"]/a/img/@src').extract_first()
item['file_urls'].append(img_url)
# 如果有下一页 请求下一页,没有数据丢回管道
next_url = response.xpath('.//div[@class="page"]//a[contains(@class,"next")]/@href').extract_first() #sleep(1)
if next_url:
next_url = self.base_url + next_url
yield scrapy.Request(next_url,callback=self.parse_item,meta={'item':item})
else:
yield item

  

管道 继承文件管道

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

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import hashlib
import os from scrapy.pipelines.files import FilesPipeline class AngelimgPipeline(object):
def process_item(self, item, spider):
return item from scrapy.http import Request
from scrapy.utils.python import to_bytes class DealFilePathPipeline(FilesPipeline):
def get_media_requests(self, item, info):
return [Request(x,meta={'item':item}) for x in item.get(self.files_urls_field, [])] def file_path(self, request, response=None, info=None):
## start of deprecation warning block (can be removed in the future)
def _warn():
from scrapy.exceptions import ScrapyDeprecationWarning
import warnings
warnings.warn('FilesPipeline.file_key(url) method is deprecated, please use '
'file_path(request, response=None, info=None) instead',
category=ScrapyDeprecationWarning, stacklevel=1) # check if called from file_key with url as first argument
if not isinstance(request, Request):
_warn()
url = request
else:
url = request.url # detect if file_key() method has been overridden
if not hasattr(self.file_key, '_base'):
_warn()
return self.file_key(url)
## end of deprecation warning block
item = request.meta.get('item',{})
media_guid = hashlib.sha1(to_bytes(url)).hexdigest() # change to request.url after deprecation
media_ext = os.path.splitext(url)[1] # change to request.url after deprecation
print(item)
return 'full2/{}/{}{}'.format(item['dir_name'],media_guid, media_ext)
return 'full/%s%s' % (media_guid, media_ext) # deprecated
def file_key(self, url):
return self.file_path(url) file_key._base = True

  setting.py

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

# Scrapy settings for angelImg 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 = 'angelImg' SPIDER_MODULES = ['angelImg.spiders']
NEWSPIDER_MODULE = 'angelImg.spiders' # Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'angelImg (+http://www.yourdomain.com)' # Obey robots.txt rules
ROBOTSTXT_OBEY = False # 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',
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36",
"Referer":"http://angelimg.spbeen.com/"
} # Enable or disable spider middlewares
# See https://doc.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
# 'angelImg.middlewares.AngelimgSpiderMiddleware': 543,
#} # Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
# 'angelImg.middlewares.AngelimgDownloaderMiddleware': 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 = {
#'angelImg.pipelines.AngelimgPipeline': 300,
'angelImg.pipelines.DealFilePathPipeline': 200,
#'scrapy.pipelines.files.FilesPipeline': 2
} FILES_STORE='file_doload' # 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'

最新文章

  1. [移动开发]全面理解UnityUI系统
  2. .NET破解之百度网盘批量转存工具
  3. Linux时间不准确的问题![转]
  4. windows脚本定时执行
  5. cssHack
  6. jeditable参数详解
  7. Asp.net上传文件Request.files获取不到文件
  8. Android pix转换为sp
  9. 倍增法lca
  10. WindowsPhone使用HtmlAgilityPack解析HTML
  11. SQL Server 表水平分区
  12. Java反射机制学习
  13. Deep Residual Learning for Image Recognition(MSRA-深度残差学习)
  14. C++11标准后的C++阅读书目
  15. 彻底抛弃PeopleEditor,SharePoint中利用Jquery Chosen创建新的人员选择器
  16. iis设置默认文档,提示web.config配置xml格式不正确
  17. 吴恩达机器学习笔记49-主成分分析问题(Principal Component Analysis Problem Formulation)
  18. CentOS7安装redis数据库及php-redis扩展
  19. Codeforces 526D Om Nom and Necklace (KMP)
  20. alter日志报WARNING: too many parse errors

热门文章

  1. 【源码讲解】Spring事务是如何应用到你的业务场景中的?
  2. ef6 code first,对已有数据库如何执行迁移
  3. unserialize3 攻防世界
  4. [LeetCode]42. 接雨水(双指针,DP)
  5. docker部署rockermq集群(docker-compose版本)
  6. Java体系结构介绍
  7. Java12新特性
  8. IoC基础篇(一)--- Spring容器中Bean的生命周期
  9. gateway(二、过滤器)
  10. 《我想进大厂》之MQ夺命连环11问