请帮我解决这个问题。
我已经从 IDLE (win7) 运行了以下脚本:
import requests
import lxml.html
def get_doc(url):
try:
req = requests.get(url)
except requests.exceptions.ConnectionError as exc:
print('A Connection error occurred. _____ ', exc)
else:
return req
if __name__ == "__main__":
baseUrl = 'http://en.modagram.com/'
#baseUrl = 'http://forum.saransk.ru/'
req = get_doc(baseUrl)
doc_html = req.text
print(doc_html)
doc_obj = lxml.html.document_fromstring(doc_html)
结果,脚本编写了一个 html 页面。
我已经从同一个 GEANY (win7) 运行了这个脚本。结果得到以下 error message
Traceback (most recent call last): File "index.py", line 19, in print(doc_html) File "C:\Python33\lib\encodings\cp866.py", l return codecs.charmap_encode(input,self.er UnicodeEncodeError: 'charmap' codec can't enco 92: character maps to
请告诉我如何从 GEANY 成功运行脚本?
最佳答案
如错误所示,当您打印 doc_html
时,Python 会尝试将其从 charmap
编码进行转换,但失败了。
试试这个:
print(doc_html.decode('charmap'))
如果它不起作用(我认为会是这种情况),试试这个:
print(doc_html.decode('charmap', errors='ignore'))
关于python - 如何从 GEANY (win 7) 运行脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22294272/