跳到内容
🛠️ToolsShed

网站地图XML生成器

生成包含URL、优先级和更改频率的sitemap.xml文件。

关于此工具

网站地图是一份文件,用于告知搜索引擎你的网站上存在哪些页面以及它们更新的频率。Sitemap XML 生成器可以创建格式正确的 sitemap.xml 文件,帮助 Google、必应和其他搜索引擎更高效地抓取和索引你的内容。对于拥有数百或数千个页面的大型网站,这一点特别有价值,因为没有网站地图,搜索引擎机器人可能需要数个月才能发现所有页面。

使用此工具,你只需逐行输入网站的网址,然后为每个网址分配优先级(0.0 到 1.0,其中 1.0 最高)并指定其更新频率(每日、每周、每月或每年)。该工具会立即生成符合官方网站地图协议的有效 XML 网站地图。然后你可以下载该文件,将其上传到你的网络服务器根目录,并将其提交给 Google Search Console 和必应网管工具以加快索引速度。

网站地图在网站结构复杂或页面缺乏充分内部链接时特别有用。虽然网站地图不能替代良好的网站架构,但它可以加快搜索引擎发现新内容或更新内容所需的时间,因此对搜索引擎优化至关重要。大多数现代网站——博客、电子商务商店、新闻网站和文档门户——都可以从向搜索引擎注册网站地图中受益。

常见问题

代码实现

from datetime import date
from xml.etree.ElementTree import Element, SubElement, tostring
from xml.dom import minidom

def generate_sitemap(urls: list[dict]) -> str:
    """
    Generate sitemap.xml string.
    Each url dict: { loc, lastmod?, changefreq?, priority? }
    """
    root = Element("urlset")
    root.set("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")
    for entry in urls:
        url_el = SubElement(root, "url")
        SubElement(url_el, "loc").text = entry["loc"]
        if "lastmod" in entry:
            SubElement(url_el, "lastmod").text = entry["lastmod"]
        if "changefreq" in entry:
            SubElement(url_el, "changefreq").text = entry["changefreq"]
        if "priority" in entry:
            SubElement(url_el, "priority").text = str(entry["priority"])
    raw = tostring(root, encoding="unicode")
    dom = minidom.parseString(raw)
    return dom.toprettyxml(indent="  ")

urls = [
    {"loc": "https://example.com/",        "lastmod": str(date.today()), "changefreq": "daily",   "priority": 1.0},
    {"loc": "https://example.com/about",   "lastmod": str(date.today()), "changefreq": "monthly", "priority": 0.8},
    {"loc": "https://example.com/contact", "lastmod": str(date.today()), "changefreq": "monthly", "priority": 0.5},
]
print(generate_sitemap(urls))

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.