2016-02-03 22 views
0

更新:我能够得到这个移动,但它不会返回到子页面,并重复序列。 我试图提取的数据是这样的表格:Scrapy,从第一页解析项目,然后从后续链接获取附加项目

表 date_1 | source_1 |链接到article_1 | date_2 | source_2 |链接到article_2 | 等....

,我需要先收集DATE_1,source_1然后进入该链接的文章,重复...

任何帮助将不胜感激。 :)

from scrapy.spiders import BaseSpider, Rule 
from scrapy.selector import HtmlXPathSelector 
from scrapy.contrib.linkextractors import LinkExtractor 
from dirbot.items import WebsiteLoader 
from scrapy.http import Request 
from scrapy.http import HtmlResponse 



class DindexSpider(BaseSpider): 
name = "dindex" 
allowed_domains = ["newslookup.com"] 
start_urls = [ 
     "http://www.newslookup.com/Business/" 
] 

def parse_subpage(self, response): 
    self.log("Scraping: " + response.url) 
    il = response.meta['il'] 
    time = response.xpath('//div[@id="update_data"]//td[@class="stime3"]//text()').extract() 
    il.add_value('publish_date', time) 
    yield il.load_item() 


def parse(self, response): 
    self.log("Scraping: " + response.url) 
    hxs = HtmlXPathSelector(response) 
    sites = hxs.select('//td[@class="article"]') 

    for site in sites: 
     il = WebsiteLoader(response=response, selector=site) 
     il.add_xpath('name', 'a/text()') 
     il.add_xpath('url', 'a/@href') 
     yield Request("http://www.newslookup.com/Business/", meta={'il': il}, callback=self.parse_subpage) 

回答

0

那只是因为你需要使用CrawlSpider class代替BaseSpider

from scrapy.spiders import CrawlSpider 

class DindexSpider(CrawlSpider): 
    # ... 
+0

我终于蜘蛛运行,但现在这个错误“il.add_value(”停止时间',响应['时间']) TypeError:'HtmlResponse'对象没有属性'__getitem__'“ –

+0

搞笑我所做的只是改变parse_page解析并运行。我不知道为什么。 –

+0

尽管蜘蛛启动,它仍然关闭,从一个页面提取某些项目,然后跟随与这些项目相关联的链接仍然没有运行。 –

相关问题