如何编写python scrapy代码来提取url';在站点的站点地图中存在

如何编写python scrapy代码来提取url';在站点的站点地图中存在,python,scrapy,web-crawler,sitemap,Python,Scrapy,Web Crawler,Sitemap,我正在尝试使用此代码获取站点地图中的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

我正在尝试使用此代码获取站点地图中的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

这个爬行器将从站点地图获取所有URL,并将它们保存到列表中。您可以轻松地将其更改为输出到文件或控制台

# -*- coding: utf-8 -*-
import scrapy
from scrapy.spiders import SitemapSpider
from scrapy.spiders import Spider
from scrapy.http import Request, XmlResponse
from scrapy.utils.sitemap import Sitemap, sitemap_urls_from_robots
from scrapy.utils.gz import gunzip, is_gzipped
import re
import requests

class GetpagesfromsitemapSpider(SitemapSpider):
    name = "test"
    handle_httpstatus_list = [404]

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

    def _parse_sitemap(self, response):
        if response.url.endswith('/robots.txt'):
            for url in sitemap_urls_from_robots(response.body):
                yield Request(url, callback=self._parse_sitemap)
        else:
            body = self._get_sitemap_body(response)
            if body is None:
                self.logger.info('Ignoring invalid sitemap: %s', response.url)
                return

            s = Sitemap(body)
            sites = []
            if s.type == 'sitemapindex':
                for loc in iterloc(s, self.sitemap_alternate_links):
                    if any(x.search(loc) for x in self._follow):
                        yield Request(loc, callback=self._parse_sitemap)
            elif s.type == 'urlset':
                for loc in iterloc(s):
                    for r, c in self._cbs:
                        if r.search(loc):
                            sites.append(loc)
                            break
            print sites

    def __init__(self, spider=None, *a, **kw):
            super(GetpagesfromsitemapSpider, self).__init__(*a, **kw)
            self.spider = spider
            l = []
            url = "https://channelstore.roku.com"
            resp = requests.head(url + "/sitemap.xml")
            if (resp.status_code != 404):
                l.append(resp.url)
            else:
                resp = requests.head(url + "/robots.txt")
                if (resp.status_code == 200):
                    l.append(resp.url)
            self.sitemap_urls = l
            print self.sitemap_urls

def iterloc(it, alt=False):
    for d in it:
        yield d['loc']

        # Also consider alternate URLs (xhtml:link rel="alternate")
        if alt and 'alternate' in d:
            for l in d['alternate']:
                yield l

这仅仅是因为您的代码实际上除了调用parse_sitemap_url()函数之外什么都不做,而parse_sitemap_url()函数什么都不做。另外,您的类MySpider的格式不好,并且有未使用的类变量。你从哪里得到的代码?实际上指的是这个链接。。你能帮我解决这个问题吗?或者告诉我怎么做。这对我现在的业余时间来说有点太复杂了,因为那将是一份真正的工作。通常我喜欢帮助指出代码中的错误或给出建议,但实际上上面的代码还没有完成。它是一个类,您可以在自己的代码中使用,但没有main()-没有起点-什么都没有,只是一个简单的类设计原型代码:(非常感谢你的帮助。但是我想把这些url添加到元组或列表变量中。你能在这方面扩展你的帮助吗。老兄..很抱歉再次打扰你..但是当我运行这个程序时,我在“self.spider=spider”一行得到缩进错误。所以我在上面的“super”一行中又增加了一个缩进(GetpagesfromsitemapSpider,self)。\uuuuuuuuuuuu初始值(*a,**kw),然后它没有抛出任何错误。但是在屏幕中我没有看到任何结果。我尝试将结果重定向到txt文件。仍然是空的。您能告诉我可能的原因吗。我在linux中运行这个程序。我使用python 2.7@Dataisk。我现在已经用测试版本替换了上面的代码!:)都德..我想你已经完成了,但是有一些小的逻辑错误..当我调试时,我添加了调用类行,然后运行..我看到了作为输入的输出..当我再次检查代码时,我没有看到parse和_parse_sitemap.之类的函数的调用语句..可以重新检查并告诉我完美的代码..提前谢谢..@dataisbeautiful你需要吗o将此爬行器添加到现有的scrapy项目中,然后使用scrapy crawl test调用它。上面的代码可以工作并返回roku.com网站地图上的所有URL