请不要将其标记为以下内容的简单重复:Python requests giving errror: IndexError: list index out of range
我在尝试将 GET 发送至以下位置时遇到上述错误:
http://astat.bugly.qq.com
我在给我请求的代码中使用的确切行是,也许我忽略了一些东西:
r = requests.get("http://"+url, stream=True, timeout=3)
其中astat.bugly.qq.com
作为url
传递
我正在从 csv 文件中读取 url,但我检查它是否读取了适当的 url。
我什至可以多次使用 python-REPL 重新创建它,我希望你也可以。 证明:
我不知道是什么原因造成的。
最佳答案
相关网址是纯文本文档,发送时不带任何 header 。
请求库似乎坚持至少有一个 header (当您收到索引错误时,它正在执行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.
如果不使用 telnet (或类似的低级别的东西),我认为您不会有太多运气获得这些“ header ”(当然不应该缩进,或者嵌入随机版权字符串)。
关于Python请求: "IndexError: list index out of range",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45905775/