【问题标题】:python element tree extract value not workingpython元素树提取值不起作用
【发布时间】:2017-10-23 22:23:17
【问题描述】:

我正在尝试使用 ElementTree 提取 <paste_key> 中的值,但出现以下错误。谁能帮我看看我做错了什么?

from pastebin import PastebinAPI
from xml.etree import cElementTree as ET
import time

x = self.apiobject.pastes_by_user(api_dev_key=self.DEVKEY, api_user_key=self.userkey)
print x
x = ET.fromstring(x)


for key in list(x):
  self.pastekeys.append(key.find('paste_key').text)
print self.pastekeys

错误输出: junk after document element: line 13, column 0

x 中存在的示例数据

<paste>
<paste_key>afafafaf</paste_key>
<paste_date>1508796842</paste_date>
<paste_title>1508796842</paste_title>
<paste_size>36096</paste_size>
<paste_expire_date>0</paste_expire_date>
<paste_private>2</paste_private>
<paste_format_long>None</paste_format_long>
<paste_format_short>text</paste_format_short>
<paste_url>https://pastebin.com/afafafaf</paste_url>
<paste_hits>0</paste_hits>
</paste>
<paste>
<paste_key>asdfasdf</paste_key>
<paste_date>1508796842</paste_date>
<paste_title>1508796842</paste_title>
<paste_size>36096</paste_size>
<paste_expire_date>0</paste_expire_date>
<paste_private>2</paste_private>
<paste_format_long>None</paste_format_long>
<paste_format_short>text</paste_format_short>
<paste_url>https://pastebin.com/asdfasdf</paste_url>
<paste_hits>0</paste_hits>
</paste>
...

【问题讨论】:

  • 查看您的示例数据,问题可能是您的 &lt;paste&gt; 元素不包含在单个根元素中。

标签: python xml celementtree


【解决方案1】:

如果问题是 xml 结构,那么试试 BeautifulSoup。

如果你的粘贴是一个名为 pastebin_string 的字符串,它会是这样的:

soup = BeautifulSoup(pastebin_string, "html.parser")
pastes = soup.find_all("paste").
for paste in pastes:
    key = paste.find("paste_key")
    print(key.text)

【讨论】:

    【解决方案2】:

    以下内容对我有用。感谢@john-gordon 指出这一点

            x = self.apiobject.pastes_by_user(api_dev_key=self.DEVKEY, api_user_key=self.userkey)
    
            x = x.split("</paste>")
            x = [y + "</paste>\r\n" for y in x]
    
            for key in x[:-1]:
                paste = ET.fromstring(key)
                self.pastekeys.append(paste.find('paste_key').text)
    

    【讨论】:

      猜你喜欢
      • 2013-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-22
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多