抓图时出错,出现exceptions.ValueError?
items代码:
from scrapy.item import Item, Field
class DmozItem(Item):
pic=Field()
pic_src=Field()
pipeline代码:
import json
import scrapy
from scrapy.contrib.pipeline.images import ImagesPipeline
from scrapy.exceptions import DropItem
from scrapy.http import Request
class TutorialPipeline(ImagesPipeline):
def get_media_requests(self, item, info):
for image_url in item['pic']:
yield Request(image_url)
def item_completed(self, results, item, info):
image_paths = [x['path'] for ok, x in results if ok]
if not image_paths:
raise DropItem("Item contains no images")
item['pic_src'] = image_paths
return item
settings代码:
BOT_NAME = 'tutorial'
SPIDER_MODULES = ['tutorial.spiders']
NEWSPIDER_MODULE = 'tutorial.spiders'
IMAGES_MIN_HEIGHT = 50
IMAGES_MIN_WIDTH = 50
IMAGES_STORE = '/ChemPic/' #和pipelines.py同一目录
DOWNLOAD_TIMEOUT = 1200
ITEM_PIPELINES = ['scrapy.contrib.pipeline.images.ImagesPipeline',
'tutorial.pipelines.TutorialPipeline']
其中,item['pic']已经有图片链接的绝对路径。如果没有图片,就提示“Item contains no images”,如下图所示:
如果pic有图片路径,就会出错,如下图所示:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论