无法使用包含标签

时间:2016-05-31 20:13:30

标签: python xml elementtree

现在我有一些代码使用Biopython和NCBI" Entrez"用于从Pubmed Central获取XML字符串的API。我试图用ElementTree解析XML以获得页面中的文本。虽然我有一个BeautifulSoup代码,当我从网站本身抓取lxml数据时,我正在切换到NCBI API,因为刮刀显然是禁止的。但是现在使用来自NCBI API的XML,我发现ElementTree非常不直观,并且可以真正使用一些帮助使其工作。当然,我已经查看了其他帖子,但其中大部分都处理名称空间,在我的例子中,我只想使用XML标记来获取信息。即使是ElementTree文档也没有进入(从我能说的)。任何人都可以帮我找出在某些标签内而不是在某些名称空间内获取信息的语法吗?

这是一个例子。注意:我使用Python 3.4

XML的小谣言:

      <sec sec-type="materials|methods" id="s5">
      <title>Materials and Methods</title>
      <sec id="s5a">
        <title>Overgo design</title>
        <p>In order to screen the saltwater crocodile genomic BAC library described below, four overgo pairs (forward and reverse) were designed (<xref ref-type="table" rid="pone-0114631-t002">Table 2</xref>) using saltwater crocodile sequences of MHC class I and II from previous studies <xref rid="pone.0114631-Jaratlerdsiri1" ref-type="bibr">[40]</xref>, <xref rid="pone.0114631-Jaratlerdsiri3" ref-type="bibr">[42]</xref>. The overgos were designed using OligoSpawn software, with a GC content of 50&#x2013;60% and 36 bp in length (8-bp overlapping) <xref rid="pone.0114631-Zheng1" ref-type="bibr">[77]</xref>. The specificity of the overgos was checked against vertebrate sequences using the basic local alignment search tool (BLAST; <ext-link ext-link-type="uri" xlink:href="http://www.ncbi.nlm.nih.gov/">http://www.ncbi.nlm.nih.gov/</ext-link>).</p>
    <table-wrap id="pone-0114631-t002" orientation="portrait" position="float">
      <object-id pub-id-type="doi">10.1371/journal.pone.0114631.t002</object-id>
      <label>Table 2</label>
      <caption>
        <title>Four pairs of forward and reverse overgos used for BAC library screening of MHC-associated BACs.</title>
      </caption>
      <alternatives>
        <graphic id="pone-0114631-t002-2" xlink:href="pone.0114631.t002"/>
        <table frame="hsides" rules="groups">
          <colgroup span="1">
            <col align="left" span="1"/>
            <col align="center" span="1"/>
          </colgroup>

对于我的项目,我想要&#34; p&#34;中的所有文字。 tag(不只是针对XML的这个snippit,而是针对整个XML字符串)。

现在,我已经知道我可以将整个XML字符串转换为ElementTree对象

>>> import xml.etree.ElementTree as ET
>>> tree = ET.ElementTree(ET.fromstring(xml_string))
>>> root = ET.fromstring(xml_string)

现在,如果我尝试使用这样的标签来获取文本:

 >>> text = root.find('p')
 >>> print("".join(text.itertext()))

 >>> text = root.get('p').text

我无法提取我想要的文字。根据我的阅读,这是因为我使用了标签&#34; p&#34;作为参数而不是命名空间。

虽然我觉得在#34; p&#34;中获取所有文字应该很简单。 XML文件中的标签,我目前无法做到。请让我知道我错过了什么以及如何解决这个问题。谢谢!

---编辑---

所以现在我知道我应该使用这段代码来获取&#39; p&#39;标记:

>>> text = root.find('.//p')
>>> print("".join(text.itertext()))

尽管我使用了itertext(),但它只返回第一个&#34; p&#34;标记而不是查看任何其他内容。 itertext()只在标签内迭代吗?文档似乎也建议它遍历所有标签,所以我不确定为什么它只返回一行而不是所有的&#34; p&#34;标签。

----最终编辑 -

我发现itertext()只能在一个标签内工作,而find()只返回第一个项目。为了得到我想要的enitre文本,我必须使用findall()

>>> all_text = root.findall('.//p')
>>> for texts in all_text:
    print("".join(texts.itertext()))

1 个答案:

答案 0 :(得分:1)

root.get()是错误的方法,因为它将检索根标记的属性而不是子标记。 root.find()是正确的,因为它会找到第一个匹配的子标签(或者,对于所有匹配的子标签,可以使用root.findall())。

如果您不仅要查找直接子标签而且还要查找间接子标签(如示例所示),root.find / root.findall中的表达式必须是XPath的子集(请参阅https://docs.python.org/2/library/xml.etree.elementtree.html#xpath-support)。在你的情况下它是'.// p':

  text = root.find('.//p')
  print("".join(text.itertext()))