Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x scrapy.pipeline ImagePipeline func文件路径。当我回来的时候_Python 3.x_Scrapy Pipeline - Fatal编程技术网

Python 3.x scrapy.pipeline ImagePipeline func文件路径。当我回来的时候

Python 3.x scrapy.pipeline ImagePipeline func文件路径。当我回来的时候,python-3.x,scrapy-pipeline,Python 3.x,Scrapy Pipeline,imagepipiline中的函数文件_path参数将响应。当我直接返回str时,我可以成功下载图像。如果使用response.meta.get('file_name'),下载将失败。它也是一个字符串。为什么不能使用变量 item.py- test.py- 管道.py- 当我运行它时,它成功了!结果如下: response.file_name:yoxo.jpg <class 'str'> yoxo.jpg response.file_name:yoxo.jpg <class '

imagepipiline中的函数文件_path参数将响应。当我直接返回str时,我可以成功下载图像。如果使用response.meta.get('file_name'),下载将失败。它也是一个字符串。为什么不能使用变量

item.py-

test.py-

管道.py-

当我运行它时,它成功了!结果如下:

response.file_name:yoxo.jpg
<class 'str'>
yoxo.jpg
response.file_name:yoxo.jpg
<class 'str'>
yoxo.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'}
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'}
我用下面的代码进行了尝试。结果是空的,为什么?同样是第一个响应的正常返回,写在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:11:39 [scrapy.core.scraper] WARNING: Dropped: Image Downloaded Failed
{'file_name': 'yoxo.jpg',
 'url': 'https://zmtimg.dfcfw.com/529d2d0f7a57c9135eb956b9afa1810a.em/art'}
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'}
为什么?我不明白。非常感谢你的帮助

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'}