2016-11-03 4 views
0

J'ai une page Web où je prends les liens RSS. Les liens sont XML et je voudrais utiliser la fonctionnalité XMLFeedSpider pour simplifier l'analyse.Utilisez XMLFeedSpider pour analyser html et xml

Est-ce possible?

Ce serait le flux:

  • GET example.com/rss (retour HTML)
  • Parse html et recevez RSS Liens
  • lien foreach parse XML

Répondre

0

J'ai trouvé un moyen simple basé sur le example in the documentation existant et en regardant le code source. Voici ma solution:

from scrapy.spiders import XMLFeedSpider 
from myproject.items import TestItem 

class MySpider(XMLFeedSpider): 
    name = 'example.com' 
    allowed_domains = ['example.com'] 
    start_urls = ['http://www.example.com/feed.xml'] 
    iterator = 'iternodes' # This is actually unnecessary, since it's the default value 
    itertag = 'item' 

    def start_request(self): 
     urls = ['http://www.example.com/get-feed-links'] 
     for url in urls: 
      yield scrapy.Request(url=url, callback=self.parse_main) 

    def parse_main(self, response): 
     for el in response.css("li.feed-links"): 
      yield scrapy.Request(el.css("a::attr(href)").extract_first(), 
           callback=self.parse) 

    def parse_node(self, response, node): 
     self.logger.info('Hi, this is a <%s> node!: %s', self.itertag,  ''.join(node.extract())) 

     item = TestItem() 
     item['id'] = node.xpath('@id').extract() 
     item['name'] = node.xpath('name').extract() 
     item['description'] = node.xpath('description').extract() 
     return item