我的问题是:我在主页上有一个列表(html - li),对于我想在另一个页面中输入的列表中的每个组件,获取一些信息,将它们放在一个项目元素中,并与其他元素交互主页列表中的元素 (html - li)。我已经完成了第一个代码,但我是 Python、Scrapy 的新手,我发现编写代码有些困难。
我得到了这个解决方案,但它为每个主列表元素生成两个项目。
class BoxSpider(scrapy.Spider):
name = "mag"
start_urls = [
"http://www.example.com/index.html"
]
def secondPage(self, response):
secondPageItem = CinemasItem()
secondPageItem['trailer'] = 'trailer'
secondPageItem['synopsis'] = 'synopsis'
yield secondPageItem
def parse(self, response):
for sel in response.xpath('//*[@id="conteudoInternas"]/ul/li'):
item = CinemasItem()
item['title'] = 'title'
item['room'] = 'room'
item['mclass'] = 'mclass'
item['minAge'] = 'minAge'
item['cover'] = 'cover'
item['sessions'] = 'sessions'
secondUrl = sel.xpath('p[1]/a/@href').extract()[0]
yield item
yield scrapy.Request(url=secondUrl, callback=self.secondPage)
有人可以帮我生成一个填充了“title”、“room”、“mclass”、“minAge”、“cover”、“sessions”、“trailer”、“synopsis”字段的项目元素吗?而不是用“title”、“room”、“mclass”、“minAge”、“cover”、“sessions”字段填充的项目和另一个用“trailer”、“synopsis”填充的项目?