跳转到主要内容

为Python生成网站地图,支持直接爬取ASGI网络应用。

项目描述

sitemaps

Build Status Coverage Python versions Package version

Sitemaps是一个Python命令行工具和库,通过爬取网络服务器或ASGI应用来生成网站地图。Sitemaps由HTTPXanyio提供支持。

注意:这是alpha软件。请确保将依赖关系固定到最新的次要版本。

快速入门

实时服务器

python -m sitemaps https://example.org

示例输出

$ cat sitemap.xml
<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    <url><loc>https://example.org/</loc><changefreq>daily</changefreq></url>
</urlset>

ASGI应用

直接向ASGI应用发出HTTP请求。目标URL仅用作构建网站地图条目的基础URL。

python -m sitemaps --asgi '<module>:<attribute>' http://testserver

检查模式

用于验证网站地图是否同步(例如,作为CI检查的一部分)

python -m sitemaps --check [...]

功能

  • 支持爬取任何实时网络服务器。
  • 支持直接爬取ASGI应用(即无需启动服务器)。
  • --check模式。
  • 从命令行调用,或使用程序化的异步API(支持asyncio和trio)。
  • 完全类型注解。
  • 100%测试覆盖率。

安装

使用pip安装

$ pip install sitemaps

Sitemaps需要Python 3.7+。

命令行参考

$ python -m sitemaps --help
usage: __main__.py [-h] [-o OUTPUT] [-I IGNORE_PATH_PREFIX] [--asgi ASGI]
                   [--max-concurrency MAX_CONCURRENCY] [--check]
                   target

positional arguments:
  target                The base URL used to crawl the website and build
                        sitemap URL tags.

optional arguments:
  -h, --help            show this help message and exit
  -o OUTPUT, --output OUTPUT
                        Output file path.
  -I IGNORE_PATH_PREFIX, --ignore-path-prefix IGNORE_PATH_PREFIX
                        Ignore URLs for this path prefix. Can be used multiple
                        times.
  --asgi ASGI           Path to an ASGI app, formatted as
                        '<module>:<attribute>'.
  --max-concurrency MAX_CONCURRENCY
                        Maximum number of URLs to process concurrently.
  --check               Compare existing output and fail if computed XML
                        differs.

程序化API

实时服务器

import sitemaps

async def main():
    urls = await sitemaps.crawl("https://example.org")
    with open("sitemap.xml", "w") as f:
        f.write(sitemaps.make_xml(urls))

ASGI应用

import httpx
import sitemaps

from .app import app

async def main():
    async with httpx.AsyncClient(app=app) as client:
        urls = await sitemaps.crawl("http://testserver", client=client)

    with open("sitemap.xml", "w") as f:
        f.write(sitemaps.make_xml(urls))

自定义URL标签

默认情况下,.make_xml()生成具有daily更改频率的<url>标签。您可以通过传递自定义的urltag可调用来自定义URL标签的生成。

from urllib.parse import urlsplit

def urltag(url):
    path = urlsplit(url).path
    changefreq = "monthly" if path.startswith("/reports") else "daily"
    return f"<url><loc>{url}</loc><changefreq>{changefreq}</changefreq></url>"

async def main():
    urls = await sitemaps.crawl(...)
    with open("sitemap.xml", "w") as f:
      f.write(sitemaps.make_xml(urls, urltag=urltag))

许可证

MIT

变更日志

本项目所有显著更改都将记录在此文件中。

格式基于Keep a Changelog

0.1.0 - 2020-05-31

新增

  • 初始实现:CLI和程序化异步API。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定该选择哪一个,请了解有关安装包的更多信息。

源代码分发

sitemaps-0.1.0.tar.gz (13.7 kB 查看哈希值)

上传时间 源代码

构建分发

sitemaps-0.1.0-py3-none-any.whl (15.0 kB 查看哈希值)

上传时间 Python 3

支持

AWSAWS云计算和安全赞助商DatadogDatadog监控FastlyFastlyCDNGoogleGoogle下载分析MicrosoftMicrosoftPSF赞助商PingdomPingdom监控SentrySentry错误记录StatusPageStatusPage状态页面