真正意义上能够全部抓取昵图网全站图片
# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request
from nipic.items import NipicItem
class NipiSpider(scrapy.Spider):
name = "nipi"
allowed_domains = ["nipic.com"]
start_urls = ['http://nipic.com/']
def parse(self, response):
urldata=response.xpath("//div[@class='fl nav-item-wrap']//a/@href").extract()
print("第一层:"+urldata[1])
print("第一层:"+urldata[2])
print("第一层:"+urldata[3])
urldata = urldata[1:4] #只有前面1到3 的标签是 ”设计“,”摄影“,”多媒体“
for i in urldata:
urlnew = response.urljoin(i)
yield Request(url=urlnew, callback=self.next)
def next(self,response):
print("第二层=============")
url2 = response.xpath('//dd[@class="menu-item-list clearfix"]//a/@href').extract()
for j in url2:
url2new =response.urljoin(j)
print(url2new)
yield Request(url=url2new, callback=self.next2)
def next2(self,response):
print("第三层=============")
#获取总页面数
pages = response.xpath('//div[@class="common-page-box mt10 align-center"]//a/@href').extract()
pageslast = response.urljoin(pages[-1])
pagenumber = pageslast.split('=')
page1 = pagenumber[0]
page2 = pagenumber[1]
#构造出所有页面的网址
for m in range(1, int(page2) + 1):
pageurl = page1 + '=' + str(m)
yield Request(url=pageurl, callback=self.next3)
#爬取每个页面图片网址
#def next3(self,response):
#print("第五层=============")
#item=NipicItem()
#item["url"] = response.xpath('//a[@class="relative block works-detail hover-none works-img-box"]//img/@src').extract()
#yield item
#另外的一种做法
def next3(self,response):
htmlurl=response.xpath('//a[@class="relative block works-detail hover-none works-img-box"]//@href').extract()
for k in htmlurl:
yield Request(url=k, callback=self.next4)
def next4(self,response):
html2url=response.xpath('//div[@class="show-img-section overflow-hidden align-center"]//img/@src').extract()
item=NipicItem()
item["url"]=html2url
yield item
---------------------------------------我是萌萌哒分割线-------------------------------------------------
# -*- coding: utf-8 -*-
import urllib.request
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
class NipicPipeline(object):
def process_item(self, item, spider):
#print(item["url"])
for i in range(len(item["url"])):
try:
that=item["url"][i]
print(that)
#构建大图网址
#urlstr = that.split(".", 1)
#urlstr1 = 'http://pic115'+'.'+urlstr[1]
#urlture = urlstr1.replace('1.jpg', '2.jpg')
#print("正在爬取---"+urlture)
#print(urlture[-18:])
#file="E:/csdn-python公开课配套资料/r5/"+ urlture[-18:]
#urllib.request.urlretrieve(urlture,filename=file)
print("正在爬取---"+that)
print(that[-18:])#倒数第18个开始像后截取->
file="E:/csdn-python公开课配套资料/r5/"+ that[-18:]
urllib.request.urlretrieve(that,filename=file)
except Exception as e:
print(e.code)
pass
return item
----------------------------------------我是萌萌哒分割线------------------------------------------------------------------
# -*- 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 NipicItem(scrapy.Item):
# define the fields for your item here like:
url = scrapy.Field()
pass
-----------------------------------------------------我是萌萌哒分割线--------------------------------------------------------------
# -*- coding: utf-8 -*-
# Scrapy settings for nipic project
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
# http://doc.scrapy.org/en/latest/topics/settings.html
# http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html
# http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html
BOT_NAME = 'nipic'
SPIDER_MODULES = ['nipic.spiders']
NEWSPIDER_MODULE = 'nipic.spiders'
USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.22 Safari/537.36 SE 2.X MetaSr 1.0'
# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'nipic (+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 http://scrapy.readthedocs.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 http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
# 'nipic.middlewares.MyCustomSpiderMiddleware': 543,
#}
# Enable or disable downloader middlewares
# See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
# 'nipic.middlewares.MyCustomDownloaderMiddleware': 543,
#}
# Enable or disable extensions
# See http://scrapy.readthedocs.org/en/latest/topics/extensions.html
#EXTENSIONS = {
# 'scrapy.extensions.telnet.TelnetConsole': None,
#}
# Configure item pipelines
# See http://scrapy.readthedocs.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
'nipic.pipelines.NipicPipeline': 300,
}
# Enable and configure the AutoThrottle extension (disabled by default)
# See http://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 http://scrapy.readthedocs.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'