使用 Python 检查 URL 状态代码的脚本

Script to check status code of URLs using Python

我想编写通过列表或文本文件接受多个 URL 的脚本,并在每个 URL 的末尾附加一些字符串并检查 https 状态代码(200、401 和 403)每个 URL 并保存在单独的文件中。

到目前为止,这是我的代码:

lst = {'back.sql',
       'backup.sql',
       'accounts.sql',
       'backups.sql',
       'clients.sql',
       'customers.sql',
       'data.sql',
       'database.sql',
       'database.sqlite',
       'users.sql',
       'db.sql',
       'db.sqlite',
       'db_backup.sql',
       'dbase.sql',
       'dbdump.sql',
       'setup.sql',
       'sqldump.sql',
       'dump.sql',
       'mysql.sql',
       'sql.sql',
       'temp.sql'
       
       }
url_test = 'http://www.Holiday.com/%s/' #This can be modified to accept multiple URLs
for i in lst:
     url = url_test %i
     print(url) #This can be modified to save results for each http status code

如果需要,检查状态代码你必须逐页请求

from requests import get

lst = {'back.sql',
           'backup.sql',
           'accounts.sql',
           'backups.sql',
           'clients.sql',
           'customers.sql',
           'data.sql',
           'database.sql',
           'database.sqlite',
           'users.sql',
           'db.sql',
           'db.sqlite',
           'db_backup.sql',
           'dbase.sql',
           'dbdump.sql',
           'setup.sql',
           'sqldump.sql',
           'dump.sql',
           'mysql.sql',
           'sql.sql',
           'temp.sql'
           
           }
url_test = ['http://www.Holiday.com/%s/'] #Create list of url
result_dict = dict()
for i in lst:
  for url_from_list in url_test:
     url = url_from_list %i
     # request and get status code from each page one by one
     result_dict[url] = get(url).status_code

result_dict 将是一个字典,其中包含 url 作为键和响应代码作为值

然后保存到文件

with open('filename.txt', 'w') as file:
  for url, status_code in result_dict.items():
    line = url+" "+str(status_code)+"\n"
    file.write(line)