python - 如何从 GEANY (win 7) 运行脚本?

标签 python python-3.x lxml python-requests

请帮我解决这个问题。

我已经从 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/

相关文章:

python-3.x - TensorFlow - 简单前馈神经网络未训练

python - 我想使用 lxml 删除花括号和 XML 命名空间,只报告标签名称

python cx_Oracle.Connection 代理在进程之间共享 - 在共享连接对象上创建游标时出现错误

python - 类型错误:只能将列表(不是 "int")连接到列表 4

python - 将 pandas 列中的关键字与另一个元素列表匹配

python - 使用 Python 抓取 HTML,document_fromstring 为空

python - 解析请求响应时应该使用 .text 还是 .content?

python - 使用另一个函数已生成的 key 和维吉尼亚密码加密字符串

python - (VS 代码)Pylint 在导入模块时抛出语法错误,但在我运行代码时工作正常

python - 将数据框中的行返回到整数列表