我们可以遍历URL,将它们扔到列表中,然后将它们写到文件中:
from xml.etree import ElementTree as ETtree = 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



