为Python生成网站地图,支持直接爬取ASGI网络应用。
项目描述
sitemaps
Sitemaps是一个Python命令行工具和库,通过爬取网络服务器或ASGI应用来生成网站地图。Sitemaps由HTTPX和anyio提供支持。
注意:这是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 查看哈希值)
关闭
sitemaps-0.1.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 9291ab4c98e7acfe8d7aa36b223ba48dcc887fe87f2012181c0183e800315dee |
|
MD5 | 3f6a8a615304a4ff2b157900e9cab081 |
|
BLAKE2b-256 | ed44f637d83d0f0ab4e7784978050bab8cd1b84f54dcbcf2769e3aaf3754bb12 |
关闭
sitemaps-0.1.0-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | f7c0801a209fd4b20fb3251d70f5275714023ad5d44560762a8bc07a904939f5 |
|
MD5 | 2f08548e4337e21c60a35957e054a1bd |
|
BLAKE2b-256 | 6450f495e68843243141c67e0bb7f07680850a98f48293c59cdd97041f6c250e |