【问题标题】:Error Handling in Python Requests Module When URL Does Not Exist当 URL 不存在时 Python 请求模块中的错误处理
【发布时间】:2019-08-28 18:26:27
【问题描述】:

我正在尝试确定 python 中请求模块的错误处理,以便在 URL 不可用时得到通知,即 HTTPError、ConnectionError、Timeout 等...

我遇到的问题是,即使在 FAKE URL 上,我似乎也收到了 200 的状态响应

我浏览了 S.O.以及其他各种网络资源,尝试了许多不同的方法,看似试图实现相同的目标,但到目前为止都一无所获。

我已将代码简化为尽可能基本的代码。

import requests

urls = ['http://fake-website.com', 
        'http://another-fake-website.com',
        'http://yet-another-fake-website.com',
        'http://google.com']

for url in urls:
    r = requests.get(url,timeout=1)
    try:
        r.raise_for_status()
    except:
        pass
    if r.status_code != 200:
        print ("Website Error: ", url, r)
    else:
        print ("Website Good: ", url, r)

我希望列表中的前 3 个 URL 归类为 'Website Error:',因为它们是我刚刚编造的 URL。 列表中的最终 URL 显然是真实的,因此应该是唯一一个被列为 'Website Good:'

发生的情况是第一个 URL 产生了对代码的正确响应,因为它给出了 503 的响应代码,但根据 https://httpstatus.io/,接下来的两个 URL 根本不产生 status_code,而只显示 ERROR Cannot find URI. another-fake-website.com another-fake-website.com:80

所以我希望列表中除了最后一个 URL 之外的所有 URL 都显示为 'Website Error:'

输出

在树莓派中运行脚本时

Python 2.7.9 (default, Sep 26 2018, 05:58:52) 
[GCC 4.9.2] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
('Website Error: ', 'http://fake-website.com', <Response [503]>)
('Website Good: ', 'http://another-fake-website.com', <Response [200]>)
('Website Good: ', 'http://yet-another-fake-website.com', <Response [200]>)
('Website Good: ', 'http://google.com', <Response [200]>)
>>>

如果我在https://httpstatus.io/ 中输入所有 4 个 URL,我会得到以下结果:

它显示一个 503、一个 200 和两个没有状态代码而只是显示错误的 URL

更新

所以我想我会在 Windows 中使用 PowerShell 进行检查并遵循以下示例: https://stackoverflow.com/a/52762602/5251044

这是下面的输出

c:\Testing>powershell -executionpolicy bypass -File .\AnyName.ps1
0 - http://fake-website.com
200 - http://another-fake-website.com
200 - http://yet-another-fake-website.com
200 - http://google.com

如你所见,我没有更进一步。

更新 2

FozoroHERE 进行了进一步讨论并尝试了各种选项但看不到修复我想我会使用urllib2 而不是requests 尝试此代码

这是修改后的代码

from urllib2 import urlopen
import socket

urls = ['http://another-fake-website.com',
        'http://fake-website.com',
        'http://yet-another-fake-website.com',
        'http://google.com',
        'dskjhkjdhskjh.com',
        'doioieowwros.com']

for url in urls:

    try:
        r  = urlopen(url, timeout = 5)
        r.getcode()
    except:
        pass
    if r.getcode() != 200:
        print ("Website Error: ", url, r.getcode())
    else:
        print ("Website Good: ", url, r.getcode())

不幸的是,结果输出仍然不正确确实与之前代码的输出略有不同,见下文:

Python 2.7.9 (default, Sep 26 2018, 05:58:52) 
[GCC 4.9.2] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
('Website Good: ', 'http://another-fake-website.com', 200)
('Website Good: ', 'http://fake-website.com', 200)
('Website Good: ', 'http://yet-another-fake-website.com', 200)
('Website Good: ', 'http://google.com', 200)
('Website Good: ', 'dskjhkjdhskjh.com', 200)
('Website Good: ', 'doioieowwros.com', 200)
>>> 

这次它显示了所有200的回复,非常奇特。

【问题讨论】:

    标签: python python-2.7 python-requests


    【解决方案1】:

    您应该将r = requests.get(url,timeout=1) 放在try: 块内。所以你的代码需要看起来像这样:

    import requests
    
    urls = ['http://fake-website.com', 
            'http://another-fake-website.com',
            'http://yet-another-fake-website.com',
            'http://google.com']
    
    for url in urls:
        try:
            r = requests.get(url,timeout=1)
            r.raise_for_status()
        except:
            pass
        if r.status_code != 200:
            print ("Website Error: ", url, r)
        else:
            print ("Website Good: ", url, r)
    

    输出:

    Website Error:  http://fake-website.com <Response [503]>
    Website Error:  http://another-fake-website.com <Response [503]>
    Website Error:  http://yet-another-fake-website.com <Response [503]>
    Website Good:  http://google.com <Response [200]>
    

    我希望这会有所帮助!

    【讨论】:

    • 感谢您的回复,但我已经尝试过了,只能得到与以前相同的输出。
    • 我刚刚添加了我用这段代码得到的输出,你得到的不是同一个吗?如果是这样,这不是你想要的吗? @1cm69
    • 奇怪的是,我没有得到那个输出。这就是让我难过一整天的原因。您发布的输出正是我所期望的,但我得到了......(请参阅我原始帖子中添加的 OUTPUT 部分)
    • 您在问题中发布的输出与我的看起来相同
    • 不,你的将前 3 个 URL 显示为 503,最后一个 URL 显示为 200。我的将第一个 URL 显示为 503,但其余的都显示为 200
    【解决方案2】:

    对我来说,原因是我的 ISP 提供的网站关于 URL 无效 - 它是 那个 网站返回 200,而不是假的。

    这可以通过用requests.get('http://fakesite').text打印返回站点的内容来验证

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-25
      • 2016-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多