标签: elementtree

Python - ElementTree 库 - 按属性值搜索树

我已经从这里安装了 ElementTree 库: http: //effbot.org/zone/element.htm in python 2.7。

我已经在 xml 文件中解析了:

tree_a=parse('/home/user/cookies.xml')
Run Code Online (Sandbox Code Playgroud)

现在出现的问题是,我无法从 effbot ElementTree 的文档中提取信息:

如何通过属性值调用来访问 xml 树中的节点?

就像是

tree_a.getNode(my_attribute,my_attribute_value)
Run Code Online (Sandbox Code Playgroud)

在一个例子中:

tree_a.getNode(cookie_diameter, 12)
Run Code Online (Sandbox Code Playgroud)

这样查询将从 xml 树返回节点,该节点的“cookie_diameter”属性值为 12

是否存在内置函数?

此致

丹尼尔

python xml parsing elementtree

2
推荐指数
1
解决办法
1208
查看次数

检测并控制 Python 中自关闭 xml 元素的创建?

也许我忽略了,但我没有在文档中找到这一点。

在 Python 的 ElementTree 中解析 xml 时,如何检测 element|tag|node 是否self-closing(或未配对,即以 结尾/>)?

创建 xml 文件时,如何明确声明我是使用self-closing( />) 还是explicitly closed(即</example>) xml 标记?

如果 ElementTree 没有,其他 python 解析器是否能更好地处理这个问题?

python xml elementtree

2
推荐指数
1
解决办法
2488
查看次数

在 Python 中将 html 标签添加到 XML.ElementTree 元素的文本中

我正在尝试使用 python 脚本生成一个 HTML 文档,其中包含使用该XML.etree.ElementTree模块的数据表中的文本。我想格式化一些单元格以包含 html 标签,通常是<br /><sup></sup>标签。当我生成一个字符串并将其写入文件时,我相信 XML 解析器正在将这些标签转换为单个字符。输出将标签显示为文本,而不是将它们作为标签处理。这是一个简单的例子:

import xml.etree.ElementTree as ET

root = ET.Element('html')
   #extraneous code removed
td = ET.SubElement(tr, 'td')
td.text = 'This is the first line <br /> and the second'

tree = ET.tostring(root)
out = open('test.html', 'w+')           
out.write(tree)                     
out.close()
Run Code Online (Sandbox Code Playgroud)

当您打开生成的“test.html”文件时,它显示的文本字符串与键入的完全相同:“这是第一行 <br /> 和第二行”。

HTML 文档本身显示了源代码中的问题。解析器似乎将标记中的“小于”和“大于”符号替换为这些符号的 HTML 表示:

    <!--Extraneous code removed-->
<td>This is the first line %lt;br /&gt; and the second</td>
Run Code Online (Sandbox Code Playgroud)

显然,我的意图是让文档处理标签本身,而不是将其显示为文本。我不确定是否可以通过不同的解析器选项来使其工作,或者是否应该使用不同的方法。如果可以解决问题,我愿意使用其他模块(例如 lxml)。为方便起见,我主要使用内置的 XML 模块。

我发现唯一可行的方法是re在写入文件之前使用替换修改最终字符串:

tree …
Run Code Online (Sandbox Code Playgroud)

html python xml elementtree

2
推荐指数
1
解决办法
3948
查看次数

ElementTree XML 解析和 urllib2.urlopen

我正在使用以下方法打开 URL:

response = urllib2.urlopen(url, data, timeout=_TIMEOUT)
Run Code Online (Sandbox Code Playgroud)

并使用response.read(),它提供以下输出:

<XMLlookup licenseid="X4X6X42" reason="OK" status="1" />
Run Code Online (Sandbox Code Playgroud)

但是当我想使用 ElementTree 解析它时,如下所示:

print response.read()
t = ET.parse(response)
r = t.getroot()
print r.attrib.get('status')
Run Code Online (Sandbox Code Playgroud)

给我以下错误消息:

File "<string>", line 62, in parse
File "<string>", line 38, in parse
cElementTree.ParseError: no element found: line 1, column 0
Run Code Online (Sandbox Code Playgroud)

但是当我删除该行时response.read(),代码工作正常。我究竟做错了什么?

python xml urllib2 elementtree celementtree

2
推荐指数
1
解决办法
4557
查看次数

ElementTree 删除元素

这里是 Python 菜鸟。想知道什么是对的干净,最好的办法删除所有“ profile”用标签updated的属性值true

我已经尝试了以下代码,但它正在抛出:SyntaxError("cannot use absolute path on element")

 root.remove(root.findall("//Profile[@updated='true']"))
Run Code Online (Sandbox Code Playgroud)

XML:

<parent>
  <child type="First">
    <profile updated="true">
       <other> </other>
    </profile>
  </child>
  <child type="Second">
    <profile updated="true">
       <other> </other>
    </profile>
  </child>
  <child type="Third">
     <profile>
       <other> </other>
    </profile>
  </child>
</parent>
Run Code Online (Sandbox Code Playgroud)

python xml scripting elementtree python-2.7

2
推荐指数
1
解决办法
7621
查看次数

使用 ElementTree 从 XML 中删除元素

我有以下代码打印出我要删除的元素的名称:

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xml')
root = tree.getroot()

for elem in tree.iter(tag='test'):
    print elem.tag
Run Code Online (Sandbox Code Playgroud)

如何从我的 XML 中删除这个元素?我的 XML 类似于以下内容:

<foo>
   <bar>
      <level>
         <test name="1">
            <stuff>
               hello
            </stuff>
         </test>
         <test name="2">
            <stuff>
               hello
            </stuff>
         </test>
      </level>   
   </bar>
</foo>
Run Code Online (Sandbox Code Playgroud)

xml elementtree xml-parsing python-2.7

2
推荐指数
1
解决办法
5950
查看次数

Python getchildren() 不适用于有效的 XML 树

如果我在 XML 文件上运行以下 python(请参阅 Q 的底部):

import xml.etree.ElementTree as ET
tree = ET.parse('C:\\temp\\test2.xml')
print(tree.getchildren())
Run Code Online (Sandbox Code Playgroud)

我收到错误:

AttributeError: 'ElementTree' 对象没有属性 'getchildren'

我将 XML 上传到在线验证器,它说 XML 没问题。

python xml elementtree

2
推荐指数
2
解决办法
2988
查看次数

使用 BS4 "lxml" 抓取 XML 数据

试图解决与此非常相似的问题:

[用beautifulsoup抓取XML元素属性

我有以下代码:

from bs4 import BeautifulSoup
import requests
r = requests.get('https://www.usda.gov/oce/commodity/wasde/latest.xml')
data = r.text
soup = BeautifulSoup(data, "lxml")
for ce in soup.find_all("Cell"):
    print(ce["cell_value1"])
Run Code Online (Sandbox Code Playgroud)

代码运行没有错误,但不会向终端打印任何值。

我想为整个页面提取上面提到的“cell_value1”数据,所以我有这样的东西:

2468.58
3061.58
376.64
and so on...
Run Code Online (Sandbox Code Playgroud)

我的 XML 文件的格式与上述问题的解决方案中的示例相同。我确定了特定于我想要抓取的属性的适当属性标签。为什么这些值没有打印到终端?

python lxml beautifulsoup elementtree python-3.x

2
推荐指数
1
解决办法
1671
查看次数

Python:LXML - 如何将元素添加到现有元素树

我需要通过向现有元素添加子元素来修改现有 xml 文件。我使用 lxml 库。

<addressbook>
<person>
    <name>Eric Idle</name>
    <phone type='fix'>999-999-999</phone>
    <phone type='mobile'>555-555-555</phone>
    <address>
        <street>12, spam road</street>
        <city>London</city>
        <zip>H4B 1X3</zip>
    </address>
</person>
</addressbook>
Run Code Online (Sandbox Code Playgroud)

这是 XML;让我们假设我想添加第二个名字:

<addressbook>
<person>
    <name>Eric Idle</name>
    <name>TEST TEST</name>
    <phone type='fix'>999-999-999</phone>
    <phone type='mobile'>555-555-555</phone>
    <address>
        <street>12, spam road</street>
        <city>London</city>
        <zip>H4B 1X3</zip>
    </address>
</person>
</addressbook>
Run Code Online (Sandbox Code Playgroud)

我知道我可以解析文件并使用 etree.getroot() 获取根,但是我可以将 /adressbook/person 作为 etree.element 获取吗?

python xml lxml elementtree

2
推荐指数
1
解决办法
2190
查看次数

xml.etree.ElementTree.Element' 对象没有属性 'write'

我想读取 XML 字符串,对其进行编辑并将其另存为 XML 文件。

但是,当我这样做时,我在标题中遇到了提到的错误 .write()

我发现当您使用ElementTree.fromstring(string)它读取 XML 字符串时,它会创建一个ElementTree.Element而不是一个ElementTree本身。Element 没有 write 方法,但 ElementTree 有。

如何将元素写入 XML 文件?或者我如何创建一个 ElementTree 并将我的 Element 添加到其中然后使用该.write方法?

python xml elementtree

2
推荐指数
1
解决办法
5814
查看次数