python - 使用 Python 检查响应时间、长度和 URL 状态(获取代码 200,404 等)

标签 python python-2.7 url web web-crawler

我有多个 URL,我想检查 URL 状态等状态(返回代码如 200,404 等)。下面是获取代码的代码。我想检查同一代码的响应时间和长度。谁能帮我修改下面的代码来查找多个网址的响应时间、长度和 getcode。

import socket
from urllib2 import urlopen, URLError, HTTPError

import urllib
socket.setdefaulttimeout( 23 )  # timeout in seconds
#print "---------URL----------", " ---Status Code---"
url='https://www.google.com'

    try :
      response = urlopen( url )
    except HTTPError, e:
        print 'The server couldn\'t fulfill the request. Reason:', str(e.code)
        #Want to get code for that but its not showing

    except URLError, e:
        print 'We failed to reach a server. Reason:', str(e.reasonse)
        #Want to get code for that but its not showing


    else :

        code=urllib.urlopen(url).getcode()
        **#here getcode is working
        print url,"-------->", code
        #print 'got response!' 

最佳答案

我认为您的用例更适合请求库。使用 pip install requests 安装它并查看 documentation 。您应该立即启动并运行!

关于python - 使用 Python 检查响应时间、长度和 URL 状态(获取代码 200,404 等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36170105/

相关文章:

Python 元类和导入 *

python-2.7 - 将 Pandas 系列中的字符串转换为小写时遇到问题

php - 如何禁止直接进入html?

macos - 自从在Mac OS X Yosemite上安装python 3以来,ipython Notebook无法启动

python - 如何用 Python 编写一个简单的电子邮件监听器

ios - 在 iOS 上获取 url 的缩影

python - 在 Python 中从 URL 中提取域名

python - 如何计算某个字符串中某事发生的次数?

python - 具有 Python 相关信息的前五名表

python - IO错误 : [Errno 28] No space left on device while installing TensorFlow