Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用python解析xml文件_Python_Elementtree - Fatal编程技术网

用python解析xml文件

用python解析xml文件,python,elementtree,Python,Elementtree,我试图解析与python脚本位于同一文件夹中的xml文件,但当我运行脚本时,它不会像预期的那样在终端中打印。我正在使用ElementTree这是我的代码: import xml.etree.ElementTree f = xml.etree.ElementTree.parse('atom.xml').getroot() for atype in f.findall('link'): print(atype.get('href')) 这就是我想从href的xml文件中得到的内容 <

我试图解析与python脚本位于同一文件夹中的xml文件,但当我运行脚本时,它不会像预期的那样在终端中打印。我正在使用ElementTree这是我的代码:

import xml.etree.ElementTree

f = xml.etree.ElementTree.parse('atom.xml').getroot()
for atype in f.findall('link'):
   print(atype.get('href'))
这就是我想从href的xml文件中得到的内容

<?xml version='1.0' ?>
 <feed xmlns="http://www.w3.org/2005/Atom">
 <title type="text">Gwern</title>
 <id>https://www.gwern.net/</id>
 <updated>2017-07-22T14:57:39Z</updated>
 <link href="https://www.gwern.net/atom.xml" rel="self" />
<author>
<name>gwern</name>
</author>
<author>
 <name>ujdRR</name>
</author>
 <generator uri="http://github.com/jgm/gitit"    version="HEAD">gitit</generator>
<entry>
<id>https://www.gwern.net/Mail%20delivery?   utm_source=RSS&amp;utm_medium=feed&amp;utm_campaign=1</id>
  <title type="text">Modified &quot;Mail delivery.page&quot;, Modified   &quot;Mistakes.page&quot;, Modified &quot;Nootropics.page&quot;, Modified &quot;Touhou.page&quot;, Modified &quot;Wikipedia resume.page&quot;,         &quot;Zeo.page&quot;, Modified &quot;hakyll.hs&quot;, Modified &quot;newsletter/2017/06.page&quot;, Modified &quot;the-long-stagnation.page&quot;, Modified &quot;wittgenstein-thesis.page&quot;</title>
<updated>2017-06-25T04:00:06Z</updated>
<author>
  <name>gwern</name>
</author>
<link href="https://www.gwern.net/Mail%20delivery?utm_source=RSS&amp;utm_medium=feed&amp;utm_campaign=1" rel="alternate" />
<summary type="text">record all minor pending edits</summary>

格温
https://www.gwern.net/
2017-07-22T14:57:39Z
格温
ujdRR
吉特
https://www.gwern.net/Mail%20delivery?   utm_source=RSS&;utm_介质=馈电和;utm_活动=1
修改了“邮件递送.页面”,修改了“错误.页面”,修改了“Nootropics.页面”,修改了“Touhou.页面”,修改了“维基百科简历.页面”,“Zeo.页面”,修改了“hakyll.hs”,修改了“新闻通讯/2017/06.页面”,修改了“长期停滞.页面”,修改了“维特根斯坦论文.页面”
2017-06-25T04:00:06Z
格温
记录所有未完成的编辑

问题:。。。我想从href的xml中得到什么

<?xml version='1.0' ?>
 <feed xmlns="http://www.w3.org/2005/Atom">
 <title type="text">Gwern</title>
 <id>https://www.gwern.net/</id>
 <updated>2017-07-22T14:57:39Z</updated>
 <link href="https://www.gwern.net/atom.xml" rel="self" />
<author>
<name>gwern</name>
</author>
<author>
 <name>ujdRR</name>
</author>
 <generator uri="http://github.com/jgm/gitit"    version="HEAD">gitit</generator>
<entry>
<id>https://www.gwern.net/Mail%20delivery?   utm_source=RSS&amp;utm_medium=feed&amp;utm_campaign=1</id>
  <title type="text">Modified &quot;Mail delivery.page&quot;, Modified   &quot;Mistakes.page&quot;, Modified &quot;Nootropics.page&quot;, Modified &quot;Touhou.page&quot;, Modified &quot;Wikipedia resume.page&quot;,         &quot;Zeo.page&quot;, Modified &quot;hakyll.hs&quot;, Modified &quot;newsletter/2017/06.page&quot;, Modified &quot;the-long-stagnation.page&quot;, Modified &quot;wittgenstein-thesis.page&quot;</title>
<updated>2017-06-25T04:00:06Z</updated>
<author>
  <name>gwern</name>
</author>
<link href="https://www.gwern.net/Mail%20delivery?utm_source=RSS&amp;utm_medium=feed&amp;utm_campaign=1" rel="alternate" />
<summary type="text">record all minor pending edits</summary>
您的
XML
有一个名称空间

因此,必须将名称空间参数与
findall
一起使用
其次,
XML
有两个
标记,一个在
标记内

findall(self、path、namespace=None)
查找与ElementPath表达式匹配的所有元素。与getroot()相同。findall(路径)。
可选名称空间参数接受前缀到名称空间的映射,该映射允许在路径表达式中使用XPath前缀

root=tree.getroot()
名称空间={
“xmlns”:”http://www.w3.org/2005/Atom"
}
#第一个出去
link=root.findall('./xmlns:link',名称空间)[0]
打印('link:{}{}'。格式(link,link.get('href'))
#找到里面的一切
对于root.findall('./xmlns:entry/xmlns:link',名称空间)中的链接:
打印(link.get('href'))
输出

link:<Element {http://www.w3.org/2005/Atom}link at 0xf6a6d8ac> https://www.gwern.net/atom.xml
https://www.gwern.net/Mail%20delivery?utm_source=RSS&utm_medium=feed&utm_campaign=1
链接:https://www.gwern.net/atom.xml
https://www.gwern.net/Mail%20delivery?utm_source=RSS&utm_medium=feed&utm_campaign=1
使用Python:3.4.2测试

问题:。。。我想从href的xml中得到什么

<?xml version='1.0' ?>
 <feed xmlns="http://www.w3.org/2005/Atom">
 <title type="text">Gwern</title>
 <id>https://www.gwern.net/</id>
 <updated>2017-07-22T14:57:39Z</updated>
 <link href="https://www.gwern.net/atom.xml" rel="self" />
<author>
<name>gwern</name>
</author>
<author>
 <name>ujdRR</name>
</author>
 <generator uri="http://github.com/jgm/gitit"    version="HEAD">gitit</generator>
<entry>
<id>https://www.gwern.net/Mail%20delivery?   utm_source=RSS&amp;utm_medium=feed&amp;utm_campaign=1</id>
  <title type="text">Modified &quot;Mail delivery.page&quot;, Modified   &quot;Mistakes.page&quot;, Modified &quot;Nootropics.page&quot;, Modified &quot;Touhou.page&quot;, Modified &quot;Wikipedia resume.page&quot;,         &quot;Zeo.page&quot;, Modified &quot;hakyll.hs&quot;, Modified &quot;newsletter/2017/06.page&quot;, Modified &quot;the-long-stagnation.page&quot;, Modified &quot;wittgenstein-thesis.page&quot;</title>
<updated>2017-06-25T04:00:06Z</updated>
<author>
  <name>gwern</name>
</author>
<link href="https://www.gwern.net/Mail%20delivery?utm_source=RSS&amp;utm_medium=feed&amp;utm_campaign=1" rel="alternate" />
<summary type="text">record all minor pending edits</summary>
您的
XML
有一个名称空间

因此,必须将名称空间参数与
findall
一起使用
其次,
XML
有两个
标记,一个在
标记内

findall(self、path、namespace=None)
查找与ElementPath表达式匹配的所有元素。与getroot()相同。findall(路径)。
可选名称空间参数接受前缀到名称空间的映射,该映射允许在路径表达式中使用XPath前缀

root=tree.getroot()
名称空间={
“xmlns”:”http://www.w3.org/2005/Atom"
}
#第一个出去
link=root.findall('./xmlns:link',名称空间)[0]
打印('link:{}{}'。格式(link,link.get('href'))
#找到里面的一切
对于root.findall('./xmlns:entry/xmlns:link',名称空间)中的链接:
打印(link.get('href'))
输出

link:<Element {http://www.w3.org/2005/Atom}link at 0xf6a6d8ac> https://www.gwern.net/atom.xml
https://www.gwern.net/Mail%20delivery?utm_source=RSS&utm_medium=feed&utm_campaign=1
链接:https://www.gwern.net/atom.xml
https://www.gwern.net/Mail%20delivery?utm_source=RSS&utm_medium=feed&utm_campaign=1

用Python测试:3.4.2

1。您确定它是XML文件而不是HTML吗?2.如果
findall
未找到任何内容,则不会打印任何内容…运行此代码时会发生什么情况?不会发生任何情况它只返回一个空值terminal@DeepSpace . 我已经添加了想要从xml中获取的文本file@mots我的两个观点仍然有效。您确定它是XML文件而不是HTML吗?2.如果
findall
未找到任何内容,则不会打印任何内容…运行此代码时会发生什么情况?不会发生任何情况它只返回一个空值terminal@DeepSpace . 我已经添加了想要从xml中获取的文本file@mots我的两个观点仍然有效。。。