2014-02-12 48 views
0

当构建蜘蛛时遇到了一件有趣的事情 - 您需要将对该函数的引用传递给它,然后在Selector中发送。尝试过所有可用的响应和请求方法Scrapy,但数据不会来。Scrapy。在选择器中发送链接

一段代码就明白了:

start_urls = [ 
     "http://www.site.ru/" 
    ] 

    def parse(self, response): 
     sel = Selector(response) 
     self.links = sel.xpath('//*[@id="col-01"]/div/div/ul/li/a/@href').extract() 

    def parse_catalog(self, response): 
     sel = Selector(response) 
     elements = sel.xpath('//*[@id="col-01"]/div[1]/ul[1]/ 
             li[4]/div[2]/strong/text()').extract()[0] 
     links_auto = sel.xpath('//div[@class="car-detail-list"]/a/@href').extract() 

     for link in links_auto: 
      self.parse_page(link) 

    def parse_page(self, link): 
     response = ??? (link) 
     self.sel = Selector(response) 
+0

你想达到什么目的? – Martol1ni

回答

0

你需要得到来自parse_catalog方法的新要求。用parse_link作为回调函数发送它,它将像常规分析方法一样工作。

start_urls = [ 
    "http://www.site.ru/" 
] 

def parse(self, response): 
    sel = Selector(response) 
    self.links = sel.xpath('//*[@id="col-01"]/div/div/ul/li/a/@href').extract() 

def parse_catalog(self, response): 
    sel = Selector(response) 
    elements = sel.xpath('//*[@id="col-01"]/div[1]/ul[1]/ 
            li[4]/div[2]/strong/text()').extract()[0] 
    links_auto = sel.xpath('//div[@class="car-detail-list"]/a/@href').extract() 

    for link in links_auto: 
     yield Request(url=link, callback=self.parse_page) # Request is sent 

def parse_page(self, response): 
    sel = Selector(response) # Response is picked up 
+0

谢谢,但我已经很熟悉了。我想使用Response-Request的一些方法来获得更灵活的定义。 – JRazor

+0

'extract()'返回一个元素数组。你想实现什么? – Martol1ni

+0

获取处理选择器的HTML – JRazor