Python check if website exists

后端 未结 8 1914
甜味超标
甜味超标 2020-11-27 12:51

I wanted to check if a certain website exists, this is what I\'m doing:

user_agent = \'Mozilla/20.0.1 (compatible; MSIE 5.5; Windows NT)\'
headers = { \'User         


        
8条回答
  •  [愿得一人]
    2020-11-27 13:11

    There is an excellent answer provided by @Adem Öztaş, for use with httplib and urllib2. For requests, if the question is strictly about resource existence, then the answer can be improved upon in the case of large resource existence.

    The previous answer for requests suggested something like the following:

    def uri_exists_get(uri: str) -> bool:
        try:
            response = requests.get(uri)
            try:
                response.raise_for_status()
                return True
            except requests.exceptions.HTTPError:
                return False
        except requests.exceptions.ConnectionError:
            return False
    

    requests.get attempts to pull the entire resource at once, so for large media files, the above snippet would attempt to pull the entire media into memory. To solve this, we can stream the response.

    def uri_exists_stream(uri: str) -> bool:
        try:
            with requests.get(uri, stream=True) as response:
                try:
                    response.raise_for_status()
                    return True
                except requests.exceptions.HTTPError:
                    return False
        except requests.exceptions.ConnectionError:
            return False
    

    I ran the above snippets with timers attached against two web resources:

    1) http://bbb3d.renderfarming.net/download.html, a very light html page

    2) http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_30fps_normal.mp4, a decently sized video file

    Timing results below:

    uri_exists_get("http://bbb3d.renderfarming.net/download.html")
    # Completed in: 0:00:00.611239
    
    uri_exists_stream("http://bbb3d.renderfarming.net/download.html")
    # Completed in: 0:00:00.000007
    
    uri_exists_get("http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_30fps_normal.mp4")
    # Completed in: 0:01:12.813224
    
    uri_exists_stream("http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_30fps_normal.mp4")
    # Completed in: 0:00:00.000007
    

    As a last note: this function also works in the case that the resource host doesn't exist. For example "http://abcdefghblahblah.com/test.mp4" will return False.

提交回复
热议问题