2016-11-03 6 views
0

私はRSSリンクを取っていたのですが、私はウェブページを持っていました。リンクはXMLであり、XMLFeedSpiderの機能を使用して解析を簡素化したいと考えています。XMLFeedSpiderを使ってhtmlとxmlを解析する

これは可能ですか?

これは流れのようになります。

  • GETのexample.com/rss(HTMLを返す)
  • 解析htmlとRSSは、私が見つけた
  • foreachのリンク解析XML

答えて

0

リンク取得簡単に既存のexample in the documentationに基づいて、ソースコードを見てください。ここに私の解決策があります:

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 
関連する問題