ChatgptPro 2023-03-09 12:33 采纳率: 100%
浏览 10
已结题

Python'解析xml文件,每个dom放一个元素里


<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>https://www.u9seo.com/356584.html</loc>
        <lastmod>2021-05-14T08:54:33+08:00</lastmod>
    </url>
    <url>
        <loc>https://www.u9seo.com/356582.html</loc>
        <lastmod>2021-05-14T08:53:56+08:00</lastmod>
    </url>
    <url>
        <loc>https://www.u9seo.com/356580.html</loc>
        <lastmod>2021-05-14T08:53:37+08:00</lastmod>
    </url>
    <url>
        <loc>https://www.u9seo.com/356578.html</loc>
        <lastmod>2021-05-14T08:53:23+08:00</lastmod>
    </url>
    <url>
        <loc>https://www.u9seo.com/356576.html</loc>
        <lastmod>2021-05-14T08:52:52+08:00</lastmod>
    </url>
    <url>
        <loc>https://www.u9seo.com/356574.html</loc>
        <lastmod>2021-05-14T08:52:36+08:00</lastmod>
    </url>

一个url就是一个dom,怎么写呢

  • 写回答

2条回答 默认 最新

  • 没掉发的程序员 山东创站网络科技有限公司官方账号 2023-03-09 13:35
    关注

    可以使用Python中的ElementTree库来解析XML。

    首先,我们需要将字符串解析为XML元素对象,可以使用ElementTree库中的fromstring()方法实现。

    然后通过ElementTree库中的findall()方法,查找sitemap元素,再通过元素的子元素loc和lastmod获取对应的值。

    以下是具体代码实现:

    import xml.etree.ElementTree as ET
    
    content = '''<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
        <sitemap>
            <loc>https://www.u9seo.com/post-sitemap1.xml</loc>
            <lastmod>2022-10-22T22:04:43+08:00</lastmod>
        </sitemap>
    </sitemapindex>'''
    
    # 解析XML
    root = ET.fromstring(content)
    
    # 判断是否为sitemapindex类型
    if root.tag == '{http://www.sitemaps.org/schemas/sitemap/0.9}sitemapindex':
        # 获取所有子元素sitemap
        sitemaps = root.findall('{http://www.sitemaps.org/schemas/sitemap/0.9}sitemap')
        
        # 遍历获取loc和lastmod值
        for sitemap in sitemaps:
            loc = sitemap.find('{http://www.sitemaps.org/schemas/sitemap/0.9}loc').text
            lastmod = sitemap.find('{http://www.sitemaps.org/schemas/sitemap/0.9}lastmod').text
            
            # 打印测试
            print('loc:', loc)
            print('lastmod:', lastmod)
    else:
        print('不是sitemapindex类型')
    

    输出的结果为:

    loc: https://www.u9seo.com/post-sitemap1.xml
    lastmod: 2022-10-22T22:04:43+08:00
    

    可以看到成功解析初其中的loc和lastmod参数。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月17日
  • 已采纳回答 3月9日
  • 创建了问题 3月9日

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵