class BdImagePipeline(ImagesPipeline):
image_num=0
def get_media_requests(self, item, info):
for x in item["image_urls"]:
yield scrapy.Request(x)
# urls = ItemAdapter(item).get(self.images_urls_field, [])
# return [Request(u) for u in urls]
def item_completed(self, results, item, info):
print("媒体管道类")
print(results)
#results[0][1]["path"]
for ok,x in results:
if ok:
print(x["path"])
images_path=[x["path"] for ok,x in results if ok]#列表推导式
for image_path in images_path:
os.rename(IMAGES_STORE+"/"+image_path,IMAGES_STORE+"/"+str(self.image_num)+".jpg")
print(IMAGES_STORE+str(self.image_num))
self.image_num+=1
# with suppress(KeyError):
# ItemAdapter(item)[self.images_result_field] = [x for ok, x in results if ok]
# return item
"""
[(True, {'url': 'https://img2.baidu.com/it/u=2630466080,181
6944842&fm=26&fmt=auto', 'path': 'full/dce5e3c7a86521cc5161
50f1f9995a648a7e6dfb.jpg', 'checksum': 'bb8fd7fec96f140627b
d2c76d30125c4', 'status': 'downloaded'})]
"""
from .settings import IMAGES_STOREclass BdImagePipeline(ImagesPipeline): image_num=0 def get_media_requests(self, item, info): for x in item["image_urls"]: yield scrapy.Request(x) # urls = ItemAdapter(item).get(self..