URLLib2.URL错误:读取服务器响应代码(Python)

2024-10-01 15:33:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个网址列表。如果我想找出每一个服务器的响应。我可以读取服务器错误(500)和断开的链接(404)好的,但是一旦读取非网站,代码就会中断(例如“notawebsite”)_断网"). 我到处找都没找到答案。。。我希望你能帮忙。在

代码如下:

import urllib2

#List of URLs. The third URL is not a website
urls = ["http://www.google.com","http://www.ebay.com/broken-link",
"http://notawebsite_broken"]

#Empty list to store the output
response_codes = []

# Run "for" loop: get server response code and save results to response_codes
for url in urls:
    try:
        connection = urllib2.urlopen(url)
        response_codes.append(connection.getcode())
        connection.close()
        print url, ' - ', connection.getcode()
    except urllib2.HTTPError, e:
        response_codes.append(e.getcode())
        print url, ' - ', e.getcode()

print response_codes

这会产生。。。在

^{pr2}$

有人知道解决这个问题的方法吗?或者有人能给我指出正确的方向吗?在


Tags: 代码服务器comhttpurlresponsewwwurllib2
3条回答

当urllib2.urlopen()无法连接到服务器,或无法解析主机的IP时,它将引发一个URLError而不是HTTPError。除了urllib2.HTTPError之外,还需要捕获urllib2.URLError来处理这些情况。在

urllib2库的API是个噩梦。在

包括我在内的许多人强烈建议使用requests软件包:

关于requests的一个好处是,任何请求问题都从基异常类继承。当您使用urllib2“raw”时,除了socket模块和其他一些模块之外,urllib2可以引发许多异常(我不记得了,但它很混乱)

tldr只需使用requests库。在

您可以使用请求:

import requests

urls = ["http://www.google.com","http://www.ebay.com/broken-link",
"http://notawebsite_broken"]

for u in urls:
    try:
        r = requests.get(u)
        print "{} {}".format(u,r.status_code)
    except Exception,e:
        print "{} {}".format(u,e)

http://www.google.com 200
http://www.ebay.com/broken-link 404
http://notawebsite_broken HTTPConnectionPool(host='notawebsite_broken', port=80): Max retries exceeded with url: /

相关问题 更多 >

    热门问题