我陷入了困境,问题是:
from lxml.html import parse
from urllib2 import urlopen
import codecs
parsed = parse(urlopen('http://lakgsa.org/?page_id=18'))
doc = parsed.getroot()
links = doc.findall('.//div/a')
print(links[15:20])
lnk=links[3]
lnk.get('href')
print(lnk.get('href'))
print(lnk.text_content())
with codecs.open('hey.json', 'wb', encoding='utf-8') as file:
file.write(lnk.text_content())
在上面运行我的ubuntu终端嘿,json'显示以下内容。你知道吗
“[招聘]2016年”
字体是break。我知道这是编码问题。但无论我尝试其他的解决方案,都失败了。你知道吗
问题是,您正在进行双重编码-来自远程源的内容已经是UTF-8,然后当您编写时,它将再次被编码。你知道吗
处理这个问题的最快方法是从输出文件
open()
中删除encoding=utf-8
。你知道吗正确的处理方法是根据远程服务器的字符集定义将输入流转换为Unicode。最简单的方法是使用python请求及其
response.text
字段。你知道吗您可能需要考虑BeautifulSoup,它具有非常好的Unicode支持。你知道吗
相关问题 更多 >
编程相关推荐