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)

  

posted on 2021-12-30 11:40  让编程成为一种习惯  阅读(771)  评论(0编辑  收藏  举报