You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从sitemap.xml中筛选含指定特征字符串的目标URL并返回?

解析站点地图XML并筛选指定URL

操作步骤

  1. 解析XML文件
    使用Python自带的xml.etree.ElementTree库读取并解析sitemap.xml文件,需处理站点地图的标准命名空间:

    import xml.etree.ElementTree as ET
    
    # 加载本地的sitemap.xml文件
    tree = ET.parse('sitemap.xml')
    root = tree.getroot()
    
    # 定义站点地图的命名空间
    ns = {'sm': 'http://www.sitemaps.org/schemas/sitemap/0.9'}
    
  2. 筛选目标URL
    遍历XML中所有存储URL的<loc>标签,筛选同时包含Average-Weather-in-(对应语义“某地平均天气-”)和-Year-Round(对应语义“-全年”)的URL:

    target_urls = []
    # 遍历所有URL节点
    for loc in root.findall('.//sm:loc', ns):
        url = loc.text
        # 检查URL是否符合双重特征条件
        if 'Average-Weather-in-' in url and '-Year-Round' in url:
            target_urls.append(url)
    
    # 输出筛选后的结果
    for valid_url in target_urls:
        print(valid_url)
    

示例筛选结果

符合条件的URL示例:
https://weatherspark.com/y/8004/Average-Weather-in-Austin-Texas-United-States-Year-Round

内容的提问来源于stack exchange,提问作者WQureshi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 03:57:06