imagepipiline中的函数file_path参数响应。直接返回str时,可以成功下载映像。如果使用response.meta.get('file_name'),则下载将失败。它也是一个字符串。为什么不能使用该变量?
item.py-
class ImageItem(scrapy.Item):
file_name = scrapy.Field()
url = scrapy.Field()
test.py-
class TestSpider(scrapy.Spider):
name = 'test'
allowed_domains = ['www.baidu.com']
start_urls = ['http://www.baidu.com/']
def parse(self, response):
image_item['file_name'] = 'yoxo.jpg'
image_item['url'] = 'https://zmtimg.dfcfw.com/529d2d0f7a57c9135eb956b9afa1810a.em/art'
yield image_item
pipelines.py-
class ArticleImagePipeline(ImagesPipeline):
def file_path(self, request, response=None, info=None):
try:
print('response.file_name:{}'.format(response.meta.get('file_name')))
print(type(response.meta.get('file_name')))
print(response.meta.get('file_name'))
except Exception as e:
pass
return 'hello.jpg'
def get_media_requests(self, item, info):
meta = {
'file_name': item['file_name']
}
yield Request(url=item['url'], meta=meta)
运行时成功!结果如下:
response.file_name:yoxo.jpg
<class 'str'>
yoxo.jpg
response.file_name:yoxo.jpg
<class 'str'>
yoxo.jpg
但是当我使用变量作为返回值时,它失败了。
def file_path(self, request, response=None, info=None):
try:
print('response.file_name:{}'.format(response.meta.get('file_name')))
print(type(response.meta.get('file_name')))
print(response.meta.get('file_name'))
except Exception as e:
pass
return 'hello.jpg'
结果:
2018-12-13 22:11:39 [scrapy.core.scraper] WARNING: Dropped: Image Downloaded Failed
{'file_name': 'yoxo.jpg',
'url': 'https://zmtimg.dfcfw.com/529d2d0f7a57c9135eb956b9afa1810a.em/art'}
我用以下代码尝试了它。结果为空,为什么?相同的是第一个响应正常返回,写在try之外为空
try:
# aa = response.meta.get('file_name')
print('response.file_name:{}'.format(response.meta.get('file_name')))
print(type(response.meta.get('file_name')))
print(response.meta.get('file_name'))
except Exception as e:
logging.error(response)
logging.error(e)
pass
print(response.meta.get('file_name','yep'))
return 'hello.jpg'
结果:
2018-12-13 22:15:31 [root] ERROR: None
2018-12-13 22:15:31 [root] ERROR: 'NoneType' object has no attribute 'meta'
2018-12-13 22:15:31 [scrapy.core.scraper] WARNING: Dropped: Image Downloaded Failed
{'file_name': 'yoxo.jpg',
'url': 'https://zmtimg.dfcfw.com/529d2d0f7a57c9135eb956b9afa1810a.em/art'}
为什么?我听不懂。非常感谢您的帮助〜