import scrapy class DoubanSpider(scrapy.Spider): name = 'douban' allowed_domains = ['douban.com'] start_urls = [f'https://movie.douban.com/top250?start=0'] def parse(self, response): title=response.xpath('//a/span[@class="title"][1]/text()').getall() start=response.xpath('//span[@class="rating_num"]/text()').getall() #使用原生的Python写法 写入txt文档 # with open('moive.txt','w',encoding='utf-8') as f: # for t,s in zip(title,start): # #print(f'{t}{s}') # f.write(f'{t},{s}\n') # #导出的另一种格式写法 # FEED_EXPORT_ENCODING = 'utf-8' 导出格式json使用 for t,s in zip(title,start): yield { 'title':t, 'star':s } #scrapy crawl douban -o wen.csv 保存csv格式的文件 也就是excel #scrapy crawl douban -o wen.json 保存json格式的文件 #scrapy crawl douban -o wen.txt 以文本方式保存 #打开设置 #打开pipelines 添加open close # ITEM_PIPELINES = { # 'scrapy01.pipelines.Scrapy01Pipeline': 300, # }