Python check if website exists

后端 未结 8 1940
甜味超标
甜味超标 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:10

    You can simply use stream method to not download the full file. As in latest Python3 you won't get urllib2. It's best to use proven request method. This simple function will solve your problem.

    def uri_exists(uri):
        r = requests.get(url, stream=True)
        if r.status_code == 200:
            return True
        else:
            return False
    

提交回复
热议问题