吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2546|回复: 12
收起左侧

[Python 转载] 好久没发帖子了,发一个壁纸下载的scrapy

[复制链接]
bboydandy 发表于 2020-11-3 15:48
本帖最后由 bboydandy 于 2020-11-3 20:49 编辑

程序基于SCRAPY框架,爬取目标为某岸壁纸首先spider文件内容
自己又重新测试了一下,可以爬全站,大佬们自行替换URL地址~
有大佬说不是原图,看了一下确实是,就当给自己学习框架留一个参考,谢谢大家批评指正~~~
[Python] 纯文本查看 复制代码
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[i]
            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()[0]
        print(imageurl)
        item['imageurl'] = imageurl
        yield item

接下来是ITEMS
[Python] 纯文本查看 复制代码
import scrapy


class BianItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    imageurl = scrapy.Field()
    pass


然后管道类
[Python] 纯文本查看 复制代码
# 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
[Python] 纯文本查看 复制代码
# 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'


代码很简单,就不写注释了,大佬勿喷~

bian.zip

9.96 KB, 下载次数: 36, 下载积分: 吾爱币 -1 CB

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

nu0l 发表于 2020-11-3 19:33
请问
    from bian.items import BianItem
ModuleNotFoundError: No module named 'bian'
这个bian是啥
 楼主| bboydandy 发表于 2020-11-3 20:40
恒大大 发表于 2020-11-3 20:35
下载的并不是原图,你这样爬下来的和网页直接右键是一样的画质。

大神明示
92pojie 发表于 2020-11-3 17:15
imyxuan 发表于 2020-11-3 17:25
这里面下载的是原图吗
xyong 发表于 2020-11-3 17:29
怎么使用呢
 楼主| bboydandy 发表于 2020-11-3 18:00
imyxuan 发表于 2020-11-3 17:25
这里面下载的是原图吗

是原图啊
 楼主| bboydandy 发表于 2020-11-3 18:02

scrapy crawl bizhi
ajinxixi 发表于 2020-11-3 18:04
下载试试 感谢分享
 楼主| bboydandy 发表于 2020-11-3 18:22
ajinxixi 发表于 2020-11-3 18:04
下载试试 感谢分享

应该可以爬全站,换个URL试试
yber 发表于 2020-11-3 18:34
点个赞👍🏻
ghoob321 发表于 2020-11-3 19:10
感谢大佬分享。。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 10:06

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表