You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
983 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import urllib.request
class ZhongtuwangPipeline:
# 打开文件
def open_spider(self, spider):
self.fp = open('book.json', 'w', encoding='utf-8')
# 写入内容
def process_item(self, item, spider):
self.fp.write(str(item))
return item
# 关闭文件
def close_spider(self,spider):
self.fp.close()
class zhongwangDownloadPipeline:
def process_item(self, item, spider):
url =item.get('src')
filename = 'images/' + item.get('name')[0].replace(':','').replace('/','-').replace('?','') + '.jpg'
urllib.request.urlretrieve(url=url, filename=filename)
print(filename)
return item