scrapy 2.3 SitemapSpider

2021-06-09 10:07 更新
classscrapy.spiders.SitemapSpider

SiteMapSpider允许您通过使用 Sitemaps .

它支持嵌套的站点地图和从中发现站点地图URL robots.txt .

sitemap_urls

指向要爬网其URL的网站地图的URL列表。

您也可以指向 robots.txt 它将被解析为从中提取站点地图URL。

sitemap_rules

元组列表 (regex, callback) 在哪里?

  • regex 是一个正则表达式,用于匹配从站点地图中提取的URL。 regex 可以是str或已编译的regex对象。

  • 回调是用于处理与正则表达式匹配的URL的回调。 callback 可以是字符串(指示spider方法的名称)或可调用的。

例如::

sitemap_rules = [('/product/', 'parse_product')]

规则按顺序应用,只使用第一个匹配的规则。

如果省略此属性,则在站点地图中找到的所有URL都将使用 parse 回调。

sitemap_follow

应遵循的站点地图正则表达式列表。这只适用于使用 Sitemap index files 指向其他站点 Mapfile 。

默认情况下,将遵循所有站点地图。

sitemap_alternate_links

指定是否为一个 url 应该遵循。这些是同一网站的链接,使用同一网站内传递的另一种语言 url 块。

例如::

<url>
    <loc>http://example.com/</loc>
    <xhtml:link rel="alternate" hreflang="de" href="http://example.com/de" rel="external nofollow" target="_blank" />
</url>

用 sitemap_alternate_links 设置,这将检索两个URL。用 sitemap_alternate_links 只有残疾人 http://example.com/ 将被取回。

sitemap_alternate_links 残疾人。

sitemap_filter(entries)

这是一个过滤器函数,可以重写该函数以根据其属性选择站点地图条目。

例如::

<url>
    <loc>http://example.com/</loc>
    <lastmod>2005-01-01</lastmod>
</url>

我们可以定义一个 sitemap_filter 要筛选的函数 entries 日期:

from datetime import datetime
from scrapy.spiders import SitemapSpider

class FilteredSitemapSpider(SitemapSpider):
    name = 'filtered_sitemap_spider'
    allowed_domains = ['example.com']
    sitemap_urls = ['http://example.com/sitemap.xml']

    def sitemap_filter(self, entries):
        for entry in entries:
            date_time = datetime.strptime(entry['lastmod'], '%Y-%m-%d')
            if date_time.year >= 2005:
                yield entry

这只能找回 entries 2005年及以后年份修改。

条目是从站点地图文档中提取的dict对象。通常,键是标记名,值是其中的文本。

重要的是要注意:

  • 由于loc属性是必需的,因此不带此标记的条目将被丢弃。

  • 备用链接用键存储在列表中 alternate (见 sitemap_alternate_links )

  • 名称空间被删除,因此名为 {{namespace}}tagname 成为唯一 tagname

如果省略此方法,则将处理站点地图中找到的所有条目,同时观察其他属性及其设置。

SiteMapSpider示例

最简单的示例:使用 parse 回叫:

from scrapy.spiders import SitemapSpider

class MySpider(SitemapSpider):
    sitemap_urls = ['http://www.example.com/sitemap.xml']

    def parse(self, response):
        pass # ... scrape item here ...

使用特定回调处理某些URL,使用其他回调处理其他URL::

from scrapy.spiders import SitemapSpider

class MySpider(SitemapSpider):
    sitemap_urls = ['http://www.example.com/sitemap.xml']
    sitemap_rules = [
        ('/product/', 'parse_product'),
        ('/category/', 'parse_category'),
    ]

    def parse_product(self, response):
        pass # ... scrape product ...

    def parse_category(self, response):
        pass # ... scrape category ...

遵循中定义的站点地图 robots.txt 文件,仅跟踪其URL包含 /sitemap_shop ::

from scrapy.spiders import SitemapSpider

class MySpider(SitemapSpider):
    sitemap_urls = ['http://www.example.com/robots.txt']
    sitemap_rules = [
        ('/shop/', 'parse_shop'),
    ]
    sitemap_follow = ['/sitemap_shops']

    def parse_shop(self, response):
        pass # ... scrape shop here ...

将SiteMapSpider与其他URL源合并::

from scrapy.spiders import SitemapSpider

class MySpider(SitemapSpider):
    sitemap_urls = ['http://www.example.com/robots.txt']
    sitemap_rules = [
        ('/shop/', 'parse_shop'),
    ]

    other_urls = ['http://www.example.com/about']

    def start_requests(self):
        requests = list(super(MySpider, self).start_requests())
        requests += [scrapy.Request(x, self.parse_other) for x in self.other_urls]
        return requests

    def parse_shop(self, response):
        pass # ... scrape shop here ...

    def parse_other(self, response):
        pass # ... scrape other here ...
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号