【问题标题】:Parsing text from XML node in Python在 Python 中从 XML 节点解析文本
【发布时间】:2018-10-17 04:34:03
【问题描述】:

我正在尝试从这样的站点地图中提取 URL:https://www.bestbuy.com/sitemap_c_0.xml.gz

我已将 .xml.gz 文件解压缩并保存为 .xml 文件。结构如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
    <loc>https://www.bestbuy.com/</loc>
    <priority>0.0</priority>
</url>
<url>
    <loc>https://www.bestbuy.com/site/3d-printers/3d-printer-filament/pcmcat335400050008.c?id=pcmcat335400050008</loc>
    <priority>0.0</priority>
</url>
<url>
    <loc>https://www.bestbuy.com/site/3d-printers/3d-printing-accessories/pcmcat748300527647.c?id=pcmcat748300527647</loc>
    <priority>0.0</priority>
</url>

我正在尝试使用 ElementTree 来提取整个文件中 loc 节点中的所有 URL,但很难让它正常工作。

根据文档,我正在尝试这样的事情:

import xml.etree.ElementTree as ET
tree = ET.parse('my_local_filepath')
root = tree.getroot()

value = root.findall(".//loc")

但是,没有任何东西被加载到值中。我的目标是提取 loc 节点之间的所有 URL 并将其打印到一个新的平面文件中。我哪里错了?

【问题讨论】:

标签: python xml python-3.x elementtree


【解决方案1】:

您的尝试很接近,但就像 mzjn 在评论中所说,您没有考虑默认命名空间 (xmlns="http://www.sitemaps.org/schemas/sitemap/0.9")。

这是一个如何解释命名空间的示例:

import xml.etree.ElementTree as ET
tree = ET.parse('my_local_filepath')

ns = {"sm": "http://www.sitemaps.org/schemas/sitemap/0.9"}

for elem in tree.findall(".//sm:loc", ns):
    print(elem.text)

输出:

https://www.bestbuy.com/
https://www.bestbuy.com/site/3d-printers/3d-printer-filament/pcmcat335400050008.c?id=pcmcat335400050008
https://www.bestbuy.com/site/3d-printers/3d-printing-accessories/pcmcat748300527647.c?id=pcmcat748300527647

请注意,我使用了命名空间前缀 sm,但您可以使用任何 NCName

See here 了解有关在 ElementTree 中使用命名空间解析 XML 的更多信息。

【讨论】:

    【解决方案2】:

    我们可以遍历 URL,把它们扔进一个列表,然后把它们写到一个文件中:

    from xml.etree import ElementTree as ET
    
    tree = ET.parse('test.xml')
    root = tree.getroot()
    
    name_space = '{http://www.sitemaps.org/schemas/sitemap/0.9}'
    
    urls = []
    for child in root.iter():
        for block in child.findall('{}url'.format(name_space)):
            for url in block.findall('{}loc'.format(name_space)):
                urls.append('{}\n'.format(url.text))
    
    with open('sample_urls.txt', 'w+') as f:
        f.writelines(urls)
    
    • 请注意,我们需要从打开的 urlset 定义中附加名称空间以正确解析 xml

    【讨论】:

    • 不行,我的urls数组还是空的。不确定我尝试打开的实际 XML 文件是否存在格式问题?我正在抓取 .xml.gz 文件,就像我链接到的文件一样,并使用 GzipFile 解压缩它。
    • 是的,我想我在测试文件中删除了一些重要信息,并将其附加到解析中应该会有所帮助。我更新了答案。
    【解决方案3】:

    我知道这有点像僵尸回复,但实际上我只是在 github 上发布了一个工具,它完全可以满足您的需求。在 Python 中!因此,请随意从源代码中获取您需要的内容(或按原样使用)。我想我会对此发表评论,以便遇到此线程的其他人会拥有它。

    这里是:https://github.com/tcaldron/xmlscrape

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      • 2011-09-25
      相关资源
      最近更新 更多