Python 抓取所有站点地图链接

Python 抓取所有站点地图链接,python,scrapy,web-crawler,sitemap,Python,Scrapy,Web Crawler,Sitemap,我想抓取固定站点的sitemap.xml中存在的所有链接。我偶然发现了Scrapy的网站地图蜘蛛。到目前为止,我已经提取了网站地图中的所有URL。现在我想爬过网站地图的每个链接。任何帮助都是非常有用的。到目前为止,守则是: class MySpider(SitemapSpider): name = "xyz" allowed_domains = ["xyz.nl"] sitemap_urls = ["http://www.xyz.nl/sitemap.xml"]

我想抓取固定站点的sitemap.xml中存在的所有链接。我偶然发现了Scrapy的网站地图蜘蛛。到目前为止,我已经提取了网站地图中的所有URL。现在我想爬过网站地图的每个链接。任何帮助都是非常有用的。到目前为止,守则是:

class MySpider(SitemapSpider):
    name = "xyz"
    allowed_domains = ["xyz.nl"]
    sitemap_urls = ["http://www.xyz.nl/sitemap.xml"] 

    def parse(self, response):
        print response.url

基本上,您可以创建新的请求对象来抓取SitemapSpider创建的URL,并使用新回调解析响应:

class MySpider(SitemapSpider):
    name = "xyz"
    allowed_domains = ["xyz.nl"]
    sitemap_urls = ["http://www.xyz.nl/sitemap.xml"] 

    def parse(self, response):
        print response.url
        return Request(response.url, callback=self.parse_sitemap_url)

    def parse_sitemap_url(self, response):
        # do stuff with your sitemap links

您需要添加sitemap_规则来处理已爬网URL中的数据,并且可以创建任意数量的URL。 例如,假设您有一个名为的页面,您想创建一个规则:

class MySpider(SitemapSpider):
    name = 'xyz'
    sitemap_urls = 'http://www.xyz.nl/sitemap.xml'
    # list with tuples - this example contains one page 
    sitemap_rules = [('/x/', parse_x)]

    def parse_x(self, response):
        sel = Selector(response)
        paragraph = sel.xpath('//p').extract()

        return paragraph

如果您可以发布域的有效url,那么检查代码就更容易了如果没有指定规则,默认情况下会调用
parse
方法;我相信原来的帖子在这方面是正确的