Python检查批量URL是否可以正常访问
生活随笔
收集整理的這篇文章主要介紹了
Python检查批量URL是否可以正常访问
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''=================================================
@Project -> File :test_-pytest-automation -> 555
@IDE :PyCharm
@Author :Mr. MA
@Date :2021/7/30 9:37
@Desc :檢查URL是否能正常訪問(腳本)
=================================================='''
import requests
import pandas as pd# 取出excel表格數據遍歷后生成字典
def parse_excel(file_path, sheet_name, column1, column2):res = {}data = pd.read_excel(io=file_path, sheet_name=sheet_name)a = data[column1]c = data[column2]for i in range(len(a)):res[a[i]] = (str(c[i]))return res# 檢查URL狀態
def getHttpStatusCode(url):try:request = requests.get(url)httpStatusCode = request.status_codereturn httpStatusCodeexcept requests.exceptions.HTTPError as e:return eif __name__ == "__main__":real_data = parse_excel('url.xls', 'Sheet1', 'id', 'product_show_img')# 取字典里數據遍歷key,value檢驗url,不通鏈接保存至文本輸出for key, value in real_data.items():try:status = getHttpStatusCode(value.strip('\n')) # 換行符if status != 200:with open('404.txt', 'a') as f:f.write("id:{},product_show_img:{}".format(key,value) + '\n')print(key, value)else:print('sucess')except Exception as e:print(e)
總結
以上是生活随笔為你收集整理的Python检查批量URL是否可以正常访问的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu安装网易云音乐
- 下一篇: C++ 双声道PCM音频分成单声道音频