Python urllib开放问题
发布时间:2020-12-20 13:31:12 所属栏目:Python 来源:网络整理
导读:我正在尝试从http://book.libertorrent.com/获取数据,但目前我的失败很严重,因为响应中存在一些额外的数据(标题).我的代码非常简单: response = urllib.urlopen('http://book.libertorrent.com/login.php')f = open('someFile.html','w')f.write(response.r
我正在尝试从http://book.libertorrent.com/获取数据,但目前我的失败很严重,因为响应中存在一些额外的数据(标题).我的代码非常简单:
response = urllib.urlopen('http://book.libertorrent.com/login.php') f = open('someFile.html','w') f.write(response.read()) read()返回: Date: Fri,09 Nov 2012 07:36:54 GMT Content-Type: text/html; charset=utf-8 Transfer-Encoding: chunked Connection: close Cache-Control: no-cache,pre-check=0,post-check=0 Expires: 0 Pragma: no-cache Set-Cookie: bb_test=973132321; path=/; domain=book.libertorrent.com Content-Language: ru 1ec0 ...Html... 0 而response.info()是空的. 有没有办法纠正反应? 解决方法
我们试试这个:
$echo -ne "GET /index.php HTTP/1.1rnHost: book.libertorrent.comrnrn" | nc book.libertorrent.com 80 | head -n 10 HTTP/1.1 200 OK WWW Date: Sat,10 Nov 2012 17:41:57 GMT Content-Type: text/html; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Content-Language: ru 1f57 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html dir="ltr"> 在第二行看到“WWW”?这不是有效的HTTP标头,我猜这是什么在这里抛出响应解析器. 顺便说一句,python2和python3在这里表现不同: > python2似乎立即将此无效标题后的任何内容解释为内容 所以最后问题是服务器发送的响应无效,应该在服务器端修复. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |