马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
大佬们,为什么这个程序没有报错,但最后却没有保存下图片,请求帮忙看一看
爬虫文件import scrapy
from ..items import TupianItem
class TuSpider(scrapy.Spider):
name = 'tu'
#allowed_domains = ['www.xxx.com']
start_urls = ['https://pic.netbian.com/4kmeinv/']
def parse(self, response):
li_list = response.xpath('//*[@id="main"]/div[3]/ul/li')
for li in li_list:
img_src = "https://pic.netbian.com" + li.xpath('.//img/@src').extract_first()
item = TupianItem()
item['src'] = img_src
yield item
items文件import scrapy
class TupianItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
src = scrapy.Field()
pipelines文件import scrapy
from itemadapter import ItemAdapter
from scrapy.pipelines.images import ImagesPipeline
class mediaPileline(ImagesPipeline):
#重写三个方法
def get_media_requests(self, item, info):
img_src = item['src']
print(img_src)
yield scrapy.Request(img_src)
def file_path(self, request, response=None, info=None, *, item=None):
imgName = request.url.split('/')[-1]
print(imgName,'保存成功')
return imgName
def item_completed(self, results, item, info):
return item
不用,只是 ImagesPipeline 有转换图片大小和格式所以需要用到 Pillow 这个库
|