【发布时间】:2016-02-25 16:43:23
【问题描述】:
我有一个议程作为起始页。此页面包含活动的开始时间和标题以及指向每个活动详细信息页面的链接。
我的蜘蛛在每个事件的详细信息页面上提取所有事件详细信息(描述、位置等),除了我必须在我的开始页面上提取的开始时间。
如何从开始页面和每个详细信息页面上的其他数据中提取开始时间? 什么是斗志昂扬的路要走?使用元 ['item'] ?我不明白... 这是我现在的蜘蛛。非常感谢任何帮助!
class LuSpider(scrapy.Spider):
name = "lu"
allowed_domains = ["example.com"]
start_urls = ["http://www.example.com/agenda"]
def parse(self, response):
for href in response.css("div.toggle_container_show > div > a::attr('href')"):
url = response.urljoin(href.extract())
yield scrapy.Request(url, callback=self.parse_agenda_contents)
def parse_agenda_contents(self, response):
for sel in response.xpath('//div[@class="container"]'):
item = LuItem()
item['EventTitle'] = sel.xpath('div[@class="content"]/div/div[@class="sliderContent"]/h1[@id]/text()').extract()
item['Description'] = sel.xpath('div[@class="content"]/div/div[@class="sliderContent"]//p').extract()
yield item
编辑:
我尝试使用request.meta['item'] 从开始页面提取开始时间,并在开始页面中为每个事件获取所有开始时间列表。如何获取每个事件的开始时间?
有人可以告诉我正确的方向吗?
class LuSpider(scrapy.Spider):
name = "lu"
allowed_domains = ["example.com"]
start_urls = ["http://www.example.com/agenda"]
def parse(self, response):
item = LuItem()
item['StartTime'] = response.xpath('//div[contains(., "H")]/span/text()').extract()
for href in response.css("div.toggle_container_show > div > a::attr('href')"):
url = response.urljoin(href.extract())
request = scrapy.Request(url, callback=self.parse_agenda_contents)
request.meta['item'] = item
yield request
def parse_agenda_contents(self, response):
for sel in response.xpath('//div[@class="container"]'):
item = response.meta['item']
item['EventTitle'] = sel.xpath('div[@class="content"]/div/div[@class="sliderContent"]/h1[@id]/text()').extract()
item['Description'] = sel.xpath('div[@class="content"]/div/div[@class="sliderContent"]//p').extract()
yield item
【问题讨论】:
-
编辑使用
request.meta['item'] = item -
终于找到了解决办法。请参阅下面的答案。
标签: scrapy scrapy-spider