Python请求: "IndexError: list index out of range"

标签 python get python-requests

请不要将其标记为以下内容的简单重复: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 重新创建它,我希望你也可以。 证明: enter image description here

我不知道是什么原因造成的。

最佳答案

相关网址是纯文本文档,发送时不带任何 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/

相关文章:

python - 我有一个 python 命名元组。除了通过索引或解包之外,还有其他方法可以访问单个成员吗?

php - 使用 htaccess 重写/重定向,使单个 PHP 文件可以根据 GET/POST 变量显示数据

java - 检测 View 是否添加到布局中

python - 使用不同的参数调用 API 每次都会返回相同的数据

Python请求抛出错误IOError : [Errno 22] invalid mode ('wb' ) or filename:

Python - 使用 lxml 将 urlib2 替换为 Requests

返回可变数量输出的python函数

python - 如何记录python函数参数类型?

python - 如何在 Pandas 数据框的列名上方添加数字?

php、MySQL、fpdf 和 $_Get?