【问题标题】:Parsing XML with lxml.objectify.Getting error使用 lxml.objectify.Getting 错误解析 XML
【发布时间】:2018-10-19 12:04:58
【问题描述】:

以下代码来自“用于数据分析的 Python”:

from io import StringIO
import io

tag = '<a href="http://www.google.com">Google</a>'
root = objectify.parse(io.StringIO(tag).getroot())

执行代码会产生以下错误:

TypeError: initial_value must be unicode or None, not str

请帮忙!

【问题讨论】:

    标签: html xml lxml lxml.objectify


    【解决方案1】:

    StringIO 需要一个 unicode 字符串,这不是 python 2 中的默认字符串类型。 替换

    tag = '<a href="http://www.google.com">Google</a>'
    

    tag = u'<a href="http://www.google.com">Google</a>'
    

    应该可以。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-20
      • 2015-02-18
      • 2017-03-11
      相关资源
      最近更新 更多