/ / Use XMLFeedSpider para analizar html y xml - html, xml, scrapy, rastreador web

Utilice XMLFeedSpider para analizar html y xml: html, xml, scrapy, rastreador web

Tengo una página web desde donde tomo los enlaces RSS. Los enlaces son XML y me gustaría usar la funcionalidad XMLFeedSpider para simplificar el análisis.

¿Es eso posible?

Este sería el flujo:

  • GET example.com/rss (devuelve HTML)
  • Analizar html y obtener enlaces RSS
  • foreach link parse XML

Respuestas

0 para la respuesta № 1

He encontrado una forma sencilla basada en la existente ejemplo en la documentación y mirando el código fuente. Aquí está mi solución:

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