我正在尝试使用HTML scraper,就像提供的那样here。就他们提供的例子来说,这很好。但是,当我尝试在我的webpage中使用它时,我会收到这个错误-Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration.
我试过用谷歌搜索,但找不到解决办法。我非常感谢你的帮助。我想知道是否有办法用Python把它复制成HTML。
编辑:
from lxml import html
import requests
page = requests.get('http://cancer.sanger.ac.uk/cosmic/gene/analysis?ln=PTEN&ln1=PTEN&start=130&end=140&coords=bp%3AAA&sn=&ss=&hn=&sh=&id=15#')
tree = html.fromstring(page.text)
谢谢你。
简而言之:使用
page.content
,而不是page.text
。来自http://lxml.de/parsing.html#python-unicode-strings:
来自http://docs.python-requests.org/en/latest/user/quickstart/#response-content:
所以你看,
requests.text
和lxml.etree
都想把utf-8解码成unicode。但是,如果让requests.text
进行解码,那么xml文件中的编码语句就变成了谎言。所以,让
requests.content
不要解码。这样lxml
将收到一个一致的未编码文件。相关问题 更多 >
编程相关推荐