python - 如何修复 "Unicode strings with encoding declaration are not supported."

标签 python parsing lxml

ValueError: Unicode strings with encoding declaration are not supported. 
Please use bytes input or XML fragments without declaration.

当我尝试解析此网站时不起作用。

当我尝试序列化此页面文本时,出现错误 类型错误:类型“str”无法序列化

from lxml import html

source = 'http://games.chruker.dk/eve_online/item.php?type_id=814'
path = '//*[@id="top"]/table[1]/tbody/tr[1]/td[3]/table'

page = requests.get(source)
pagetext = page.text

parser = html.fromstring(pagetext)

result = parser.xpath(path)
print(result)


我希望有一个表格要求,例如网站中的要求: http://games.chruker.dk/eve_online/item.php?type_id=814

最佳答案

试试这个:

parser = html.fromstring(bytes(pagetext, encoding='utf8'))

关于python - 如何修复 "Unicode strings with encoding declaration are not supported.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57833080/

相关文章:

python - 没有名为 ‘picamera' 的模块

Python MySQLdb : Query parameters as a named dictionary

python - 2D 插值导致 OverflowError : Too many data points to interpolate

c - 如何从字符串中解析公式?

解析 AMF3 对象

python - BeautifulSoup 排除特定标签内的内容

python - 比较两个数据框并根据掩码值向数据框添加新列

java - 我们如何以编程方式获取 IntelliJ 中方法的调用层次结构

python - 关闭 lxml 中没有文本的标签

python - 如何在 virtualenv 中使用 pypy 安装 lxml