什么是使用Python解析此XML站点地图的最有效方法?

问题描述 投票:0回答:4

我有以下我试图解析的站点地图:

<?xml version="1.0" encoding="UTF-8"?> 
  <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url> 
      <loc>https://www.example.com/examplea</loc> 
      <priority>0.5</priority> 
      <lastmod>2019-03-14</lastmod> 
      <changefreq>daily</changefreq> 
   </url> 
   <url> 
     <loc>https://www.example.com/exampleb</loc> 
     <priority>0.5</priority> 
     <lastmod>2019-03-14</lastmod> 
     <changefreq>daily</changefreq> 
   </url> 
</urlset>

什么是使用Python获取loc标签内的url链接的最快方法?

我尝试使用ElementTree,但我认为它因命名空间而无效。

我需要得到“https://www.example.com/examplea”和“https://www.example.com/exampleab

python xml sitemap
4个回答
1
投票

您可以考虑使用正则表达式。

对于您的示例,您可以通过以下代码满足您的需求:

import re

string = '''
<?xml version="1.0" encoding="UTF-8"?> 
  <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url> 
      <loc>https://www.example.com/examplea</loc> 
      <priority>0.5</priority> 
      <lastmod>2019-03-14</lastmod> 
      <changefreq>daily</changefreq> 
   </url> 
   <url> 
     <loc>https://www.example.com/exampleb</loc> 
     <priority>0.5</priority> 
     <lastmod>2019-03-14</lastmod> 
     <changefreq>daily</changefreq> 
   </url> 
</urlset>
'''

pattern = '(?<=<loc>)[a-zA-z]+://[^\s]*(?=</loc>)'

re.findall(pattern,string)

结果是['https://www.example.com/examplea', 'https://www.example.com/exampleb']


1
投票
import re

str = """
<?xml version="1.0" encoding="UTF-8"?> 
  <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url> 
      <loc>https://www.example.com/examplea</loc> 
      <priority>0.5</priority> 
      <lastmod>2019-03-14</lastmod> 
      <changefreq>daily</changefreq> 
   </url> 
   <url> 
     <loc>https://www.example.com/exampleb</loc> 
     <priority>0.5</priority> 
     <lastmod>2019-03-14</lastmod> 
     <changefreq>daily</changefreq> 
   </url> 
</urlset>
"""  
url = re.findall("<loc>(.*?)</loc>", str)

0
投票

正如其他答案所说,你可以使用正则表达式。但是如果你在使用正则表达式时有点不舒服,你也可以在python中使用xmltodict模块将xml转换为字典,你可以从xml轻松获得所需的任何数据。


0
投票

使用XML但绕过命名空间

from StringIO import StringIO
import xml.etree.ElementTree as ET

xml = '''<?xml version="1.0" encoding="UTF-8"?> 
  <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url> 
      <loc>https://www.example.com/examplea</loc> 
      <priority>0.5</priority> 
      <lastmod>2019-03-14</lastmod> 
      <changefreq>daily</changefreq> 
   </url> 
   <url> 
     <loc>https://www.example.com/exampleb</loc> 
     <priority>0.5</priority> 
     <lastmod>2019-03-14</lastmod> 
     <changefreq>daily</changefreq> 
   </url> 
</urlset>'''

it = ET.iterparse(StringIO(xml))
for _, el in it:
    if '}' in el.tag:
        el.tag = el.tag.split('}', 1)[1]  # strip all namespaces
    for at in el.attrib.keys(): # strip namespaces of attributes too
        if '}' in at:
            newat = at.split('}', 1)[1]
            el.attrib[newat] = el.attrib[at]
            del el.attrib[at]
root = it.root

urls = [u.text for u in root.findall('.//loc')]
print(urls)

产量

['https://www.example.com/examplea', 'https://www.example.com/exampleb']
© www.soinside.com 2019 - 2024. All rights reserved.