好久没发帖子了,发一个壁纸下载的scrapy
本帖最后由 bboydandy 于 2020-11-3 20:49 编辑程序基于SCRAPY框架,爬取目标为某岸壁纸首先spider文件内容
自己又重新测试了一下,可以爬全站,大佬们自行替换URL地址~
有大佬说不是原图,看了一下确实是,就当给自己学习框架留一个参考,谢谢大家批评指正~~~:lol
import scrapy
from bian.items import BianItem
class BizhiSpider(scrapy.Spider):
name = 'bizhi'
# allowed_domains = ['netbian.com']
start_urls = ['http://pic.netbian.com/index_{}.html'.format(i) for i in range(1,1256)]
def parse(self, response):
href_list = response.xpath('//*[@id="main"]/div[@class="slist"]/ul/li/a/@href').extract()
for i in range(len(href_list)-1):
url = 'http://pic.netbian.com' + href_list
yield scrapy.Request(url,callback=self.parse_info)
def parse_info(self,response):
item = BianItem()
# print(response.xpath('//*[@id="img"]/img/@src').extract())
imageurl = 'http://pic.netbian.com' + response.xpath('//*[@id="img"]/img/@src').extract()
print(imageurl)
item['imageurl'] = imageurl
yield item
接下来是ITEMS
import scrapy
class BianItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
imageurl = scrapy.Field()
pass
然后管道类
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
from scrapy.pipelines.images import ImagesPipeline
import scrapy
class BianPipeline(object):
def process_item(self, item, spider):
return item
class BianImagePipeline(ImagesPipeline):
def get_media_requests(self, item, info):
# 1 获取图片链接
imageurl= item["imageurl"]
# 2 向图片链接发请求,响应会保存在settings.py中的IMAGES_STORE路径中
yield scrapy.Request(imageurl)
return item
最后setting
# Scrapy settings for bian project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
# https://docs.scrapy.org/en/latest/topics/settings.html
# https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
# https://docs.scrapy.org/en/latest/topics/spider-middleware.html
BOT_NAME = 'bian'
SPIDER_MODULES = ['bian.spiders']
NEWSPIDER_MODULE = 'bian.spiders'
# 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/86.0.4240.111 Safari/537.36'
# Obey robots.txt rules
ROBOTSTXT_OBEY = False
LOG_LEVEL = 'ERROR'
IMAGES_STORE = './ImageSpider'
# 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://docs.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://docs.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
# 'bian.middlewares.BianSpiderMiddleware': 543,
#}
# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
DOWNLOADER_MIDDLEWARES = {
'bian.middlewares.BianDownloaderMiddleware': 543,
}
# Enable or disable extensions
# See https://docs.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
# 'scrapy.extensions.telnet.TelnetConsole': None,
#}
# Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
# 'bian.pipelines.BianPipeline': 300,
'bian.pipelines.BianImagePipeline': 300
}
# Enable and configure the AutoThrottle extension (disabled by default)
# See https://docs.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://docs.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'
代码很简单,就不写注释了,大佬勿喷~ 请问
from bian.items import BianItem
ModuleNotFoundError: No module named 'bian'
这个bian是啥 恒大大 发表于 2020-11-3 20:35
下载的并不是原图,你这样爬下来的和网页直接右键是一样的画质。
大神明示 支持大佬原创。。{:1_890:}{:1_894:}{:1_900:} 这里面下载的是原图吗 怎么使用呢 imyxuan 发表于 2020-11-3 17:25
这里面下载的是原图吗
是原图啊 xyong 发表于 2020-11-3 17:29
怎么使用呢
scrapy crawl bizhi 下载试试 感谢分享 ajinxixi 发表于 2020-11-3 18:04
下载试试 感谢分享
应该可以爬全站,换个URL试试 点个赞👍🏻 感谢大佬分享。。
页:
[1]
2