Python判断网站是否连通,请求是否为200
import requests filepath = 'url.txt' fileExist = 'exsit.txt' fileUnexist = 'unexsit.txt' with open(filepath) as fp: with open(fileExist,'w') as exs: with open(fileUnexist,'w') as uexs: for url in fp: print(url) url = url.strip() try: request = requests.get(url,timeout=3) if request.status_code == 200: print('Web site exists') url = url +"\n" exs.write(url) else: print('Web site does not exist') except: print('unknown') url = url +"\n" uexs.write(url)