我正在尝试使用mechanize
模块打开网站,并且遇到了产生错误的文本字符串:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 14859: ordinal not in range(128)
我使用的代码是:
import mechanize
login_post_url = "http://www.naturalgasintel.com/user/login"
internal_url = "https://naturalgasintel.com/ext/resources/Data-Feed/Daily-GPI/2018/12/20181221td.txt"
browser = mechanize.Browser()
browser.open(login_post_url)
browser.select_form(nr = 1)
browser.form['user[email]'] = username
browser.form['user[password]'] = password
browser.submit()
response = browser.open(internal_url)
print response.read()
错误出现在打印语句上:
File "<ipython-input-5-408cf01a27c9>", line 14, in <module>
print response.read()
我似乎找不到有关如何机械化读取编解码器的文档。其命令的链接在这里:
Mechanize Documentation and Commands
如何通过读取unicode或utf-8来绕过此错误?