这段代码有啥问题嘛,为啥出来的item不对,有大佬解答下嘛。
import scrapy
from testscrapy1.items import *
class Testcrapy1Spider(scrapy.Spider):
name = "testcrapy1"
#allowed_domains = ["ssr1.scrape.center"]
start_urls = ["https://ssr1.scrape.center/"]
item = Testscrapy1Item()
number = 1
def parse(self, response):
i_list = response.css('.el-card__body')
#j_list = response.xpath('//div[@class="el-card__body"]/div/div[1]/a/@href')
for i in range(0,10) :
self.item['name'] = i_list[i].css('.m-b-sm ::text').extract_first()
self.item['caro'] = '/'.join(i_list[i].css('.categories span ::text').extract())
self.item['info'] = '/'.join(i_list[i].css('.m-v-sm span ::text').extract())
yield scrapy.Request(f'https://ssr1.scrape.center/detail/{self.number}', self.parse_detail)
self.number += 1
nextpage = response.xpath('//a[@class="next"]/@href').extract_first()
if nextpage:
yield scrapy.Request(f'https://ssr1.scrape.center{nextpage}', self.parse)
def parse_detail(self, response):
item = self.item
item['story'] = ''.join(response.css('.drama p ::text').extract()).strip()
item['actor'] = ''
for i in response.xpath('//div[@class="actor el-col el-col-4"]'):
item['actor'] += ''.join(i.css('p ::text').extract())+'||'
image_list = response.xpath('//div[@class="el-image"]/img/@src').extract()
if image_list.__len__() < 10:
item['image_urls'] = image_list
else:
item['image_urls'] = image_list[:11]
yield item
解决了,是item必须要deepcopy的问题。