当 URL 不存在时 Python 请求模块中的错误处理

1cm*_*m69 5 python python-2.7 python-requests

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

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

我已经浏览了 SO 和其他各种网络资源,尝试了许多不同的方法来似乎试图实现相同的目标,但到目前为止都是空的。

我已经将代码简化为基本的代码,以简化事情。

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)
Run Code Online (Sandbox Code Playgroud)

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

正在发生的事情是第一个URL生成的代码正确的响应,因为它给出了503的响应代码,但接下来的两个网址不产生status_code在所有根据https://httpstatus.io/,但只显示ERRORCannot find URI. another-fake-website.com another-fake-website.com:80

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

输出

在 Raspberry Pi 中运行脚本时

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]>)
>>>
Run Code Online (Sandbox Code Playgroud)

如果我输入所有 4 个 URL,https://httpstatus.io/我会得到以下结果: HTTPSTATUS 屏幕抓取

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

更新

所以我想我会使用 PowerShell 在 Windows 中检查这个并遵循这个例子: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
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我不再向前了。

更新 2

Fozoro HERE进行了进一步讨论并尝试了各种选项而没有修复的迹象,我想我会尝试使用这个代码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())
Run Code Online (Sandbox Code Playgroud)

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

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)
>>> 
Run Code Online (Sandbox Code Playgroud)

这次它显示了所有200响应,非常奇特。

Foz*_*oro 2

你应该把它放在块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)
Run Code Online (Sandbox Code Playgroud)

输出:

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]>
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助!