2017-08-27 115 views
1

请不要将其标记为的琐碎重复:Python的请求:“IndexError:列表索引超出范围”

http://astat.bugly.qq.comPython requests giving errror: IndexError: list index out of range

我想一个GET发送到时可能会遇到上述错误

,我在给我的请求的代码中使用的确切行,也许我忽视的东西:

r = requests.get("http://"+url, stream=True, timeout=3) 

哪里astat.bugly.qq.com通过为url

我正在读取一个csv文件的URL,但我检查它是否读取了相应的url。

我甚至可以重新创建多次python-REPL,我希望你也可以。 证明: enter image description here

我不知道什么可能导致此。

+1

无法复制您的问题 - 您安装了什么版本的“requests”? – AChampion

+1

我可以用Python 2.7.13复制它,在win10上请求2.18.3。 – thebjorn

+1

..以及请求2.18.4。 – thebjorn

回答

2

有问题的网址是一个纯文本文件,发送时没有任何标题。

请求库似乎坚持至少存在一个头(当您收到索引错误时,它会执行headers[-1])。

我不知道如果你能做到在任何请求解决它,但你可以使用urllib

import urllib 
txt = urllib.urlopen('http://astat.bugly.qq.com').read() 

更新:

my goal is to read certain headers from a website, how would you suggest I do that considering the situation ?

服务器发送以下:

~$ telnet astat.bugly.qq.com 80 
Trying 103.7.30.121... 
Connected to astat-bugly-tgw4l.mig.tencent-cloud.net. 
Escape character is '^]'. 
GET/HTTP/1.1 
HTTP/1.1 200 OK 
       Date: Thu, 30 Aug 2012 12:01:27 GMT 
       Server: Apache/2.2.17 (Unix) PHP/5.3.6 
       Cache-Control: max-age=1800 
       Keep-Alive: timeout=5, max=98 
       Connection: Keep-Alive 
       Transfer-Encoding: chunked 
       Content-Type: text/plain; charset=UTF-8 
       Copyright @ 1998 - 2012 Tencent. All Rights Reserved. 
       Content-Length: 17 

lb monitor page 
Connection closed by foreign host. 

缺少使用tel净(或类似的低水平),我不认为你会有很多运气得到这些“标题”(当然不应该缩进,或嵌入随机版权字符串)。

+1

它是底层'urllib3'库 - 已知问题:https://github.com/shazow/ urllib3/issues/950 – AChampion

+1

响应也是在没有标准HTTP状态响应的情况下发送的 - 所以不是有效的HTTP。因此,尽管'urllib'可能工作,但如果您没有有效的HTTP响应,则不能依赖它。 – AChampion

+1

@AChampion我同意,如果你控制服务器,那么修复_it_显然是正确的事情。 – thebjorn

相关问题