python - 无法使用 Python 中的 Mechanize 连接到安全网站

标签 python urllib2 mechanize httplib

我正在尝试使用 Python 中的 Mechanize 库打开一个安全 (https) 网站。当我尝试访问该网站时,服务器关闭连接并引发异常 BadStatusLine。

我尝试使用 addheaders 修改标题属性(property),但没有回应。

import mechanize

br = mechanize.Browser()
print 'opening page ...'
resp = br.open('https://onlineservices.tin.nsdl.com/etaxnew/tdsnontds.jsp')     #this one works fine
print 'ok'

print 'opening page 2 ...'
resp = br.open('https://incometaxindiaefiling.gov.in/portal/index.do')          #exception raised 
print 'ok'

异常(exception):

Traceback (most recent call last): File pydev_imports.execfile(file, globals, locals) #execute the script File "Z:\pyTax\app_test.py", line 22, in resp=br.open('https://incometaxindiaefiling.gov.in/portal/index.do')
File "build\bdist.win32\egg\mechanize_mechanize.py", line 203, in open File "build\bdist.win32\egg\mechanize_mechanize.py", line 230, in _mech_open File "build\bdist.win32\egg\mechanize_opener.py", line 188, in open File "build\bdist.win32\egg\mechanize_http.py", line 316, in http_request File "build\bdist.win32\egg\mechanize_http.py", line 242, in read File "build\bdist.win32\egg\mechanize_mechanize.py", line 203, in open
File "build\bdist.win32\egg\mechanize_mechanize.py", line 230, in _mech_open File "build\bdist.win32\egg\mechanize_opener.py", line 193, in open File "build\bdist.win32\egg\mechanize_urllib2_fork.py", line 344, in _open File "build\bdist.win32\egg\mechanize_urllib2_fork.py", line 332, in _call_chain File "build\bdist.win32\egg\mechanize_urllib2_fork.py", line 1170, in https_open File "build\bdist.win32\egg\mechanize_urllib2_fork.py", line 1116, in do_open File "D:\Python27\lib\httplib.py", line 1031, in getresponse response.begin() File "D:\Python27\lib\httplib.py", line 407, in begin version, status, reason = self._read_status() File "D:\Python27\lib\httplib.py", line 371, in _read_status raise BadStatusLine(line) httplib.BadStatusLine: ''

最佳答案

httplib.BadStatusLineis s 是 HTTPException 的子类.如果服务器以我们不理解的 HTTP 状态代码响应,则引发。这就是导致你的问题的原因。不过,我对修复并不完全确定,因为您的代码在我的计算机上运行良好。

关于python - 无法使用 Python 中的 Mechanize 连接到安全网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12927516/

相关文章:

python - 从git手动安装后导入错误

python - 使用身份验证从 https 下载文件

python - urllib2获取乱码字符串而不是页面源码

python - libgmail login() 运行时出错

Python wkhtmltopdf 无法导入模块

python - 从 matplotlib basemap 交互式获取可读(即 lng/lat)坐标?

ruby - 如何使用 Mechanize 抓取电子邮件地址而不将 `[email protected]` 作为值

ruby - 如何解析通过 Ajax 响应加载的 HTML 数据?

python - 删除列表中其他字符串的子字符串而不更改列表的原始顺序?

python - HTTPCookieProcessor 不提供 cookie