16

Webページの存在をテストするスクリプトを作成しようとしています。ページ全体をダウンロードせずにチェックできると便利です。

これは私の出発点です。複数の例で同じようにhttplibを使用しているのを見てきましたが、チェックするすべてのサイトは単にfalseを返します。

import httplib
from httplib import HTTP
from urlparse import urlparse

def checkUrl(url):
    p = urlparse(url)
    h = HTTP(p[1])
    h.putrequest('HEAD', p[2])
    h.endheaders()
    return h.getreply()[0] == httplib.OK

if __name__=="__main__":
    print checkUrl("http://www.stackoverflow.com") # True
    print checkUrl("http://stackoverflow.com/notarealpage.html") # False

何か案は?

編集

誰かがこれを提案しましたが、彼らの投稿は削除されました.. urllib2はページ全体のダウンロードを回避しますか?

import urllib2

try:
    urllib2.urlopen(some_url)
    return True
except urllib2.URLError:
    return False
4

4 に答える 4

24

これはどう:

import httplib
from urlparse import urlparse

def checkUrl(url):
    p = urlparse(url)
    conn = httplib.HTTPConnection(p.netloc)
    conn.request('HEAD', p.path)
    resp = conn.getresponse()
    return resp.status < 400

if __name__ == '__main__':
    print checkUrl('http://www.stackoverflow.com') # True
    print checkUrl('http://stackoverflow.com/notarealpage.html') # False

これにより、HTTP HEADリクエストが送信され、応答ステータスコードが400未満の場合はTrueが返されます。

  • StackOverflowのルートパスが200OKではなくリダイレ​​クト(301)を返すことに注意してください。
于 2011-06-24T17:34:22.303 に答える
14

を使用するrequestsと、これは次のように簡単です。

import requests

ret = requests.head('http://www.example.com')
print(ret.status_code)

これは、Webサイトのヘッダーをロードするだけです。これが成功したかどうかをテストするには、結果を確認できますstatus_code。または、接続が成功しなかっraise_for_statusた場合にを発生させるメソッドを使用します。Exception

于 2016-04-08T17:44:28.433 に答える
5

これはどう。

import requests

def url_check(url):
    #Description

    """Boolean return - check to see if the site exists.
       This function takes a url as input and then it requests the site 
       head - not the full html and then it checks the response to see if 
       it's less than 400. If it is less than 400 it will return TRUE 
       else it will return False.
    """
    try:
            site_ping = requests.head(url)
            if site_ping.status_code < 400:
                #  To view the return status code, type this   :   **print(site.ping.status_code)** 
                return True
            else:
                return False
    except Exception:
        return False
于 2017-04-07T00:03:34.370 に答える
-2

あなたが試すことができます

import urllib2

try:
    urllib2.urlopen(url='https://someURL')
except:
    print("page not found")
于 2016-04-08T17:35:31.840 に答える