python检查URL是否正常访问的小技巧
作者:程序猿全敏 时间:2022-02-23 09:23:56
今天,项目经理问我一个问题,问我这里有2000个URL要检查是否能正常打开,其实我是拒绝的,我知道因为要写代码了,正好学了点Python,一想,python处理起来容易,就选了python,开始把思路想好:
1.首先2000个URL。可以放在一个txt文本内
2.通过python 把内容内的URL一条一条放进数组内
3.打开一个模拟的浏览器,进行访问。
4.如果正常访问就输出正常,错误就输出错误
直接简单粗暴甩代码。因为涉及到隐私,图片打了码
import urllib.request
import time
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/49.0.2')]
#这个是你放网址的文件名,改过来就可以了
file = open('test.txt')
lines = file.readlines()
aa=[]
for line in lines:
temp=line.replace('\n','')
aa.append(temp)
print(aa)
print('开始检查:')
for a in aa:
tempUrl = a
try :
opener.open(tempUrl)
print(tempUrl+'没问题')
except urllib.error.HTTPError:
print(tempUrl+'=访问页面出错')
time.sleep(2)
except urllib.error.URLError:
print(tempUrl+'=访问页面出错')
time.sleep(2)
time.sleep(0.1)
效果图:
来源:http://blog.csdn.net/u010798367/article/details/53008439
标签:python,URL
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PyQt5实现界面(页面)跳转的示例代码
2023-06-21 18:48:43
![](https://img.aspxhome.com/file/2023/5/75225_0s.jpg)
django框架自定义模板标签(template tag)操作示例
2023-11-06 16:05:59
浅谈python中频繁的print到底能浪费多长时间
2022-01-31 06:24:34
django中使用原生sql语句的方法步骤
2023-06-29 16:36:07
pygame游戏之旅 计算游戏中躲过的障碍数量
2022-02-13 19:09:16
![](https://img.aspxhome.com/file/2023/5/87755_0s.png)
asp中文件与文件夹常用处理函数(文件后缀、创建文件等)
2011-02-20 11:00:00
SQL server高级应用 收藏版
2012-10-07 11:04:06
pytest解读fixtures之Teardown处理yield和addfinalizer方案
2023-06-18 22:13:01
Python中利用pyqt5制作指针钟表显示实时时间(指针时钟)
2023-04-30 22:11:13
![](https://img.aspxhome.com/file/2023/6/82746_0s.jpg)
跟老齐学Python之Import 模块
2022-02-02 21:13:34
![](https://img.aspxhome.com/file/2023/7/77857_0s.png)
python基础教程之csv文件的写入与读取
2021-06-18 21:21:00
![](https://img.aspxhome.com/file/2023/4/65524_0s.jpg)
SQL Server 2008中的新日期数据类型
2009-03-16 15:05:00
![](https://img.aspxhome.com/file/UploadPic/20093/2009317182644835.jpg)
Python Pandas删除替换并提取其中的缺失值NaN(dropna,fillna,isnull)
2023-08-11 01:11:34
深入了解Golang中Slice切片的使用
2023-09-25 08:59:08
![](https://img.aspxhome.com/file/2023/9/87609_0s.png)
JS实现仿新浪微博发布内容为空时提示功能代码
2023-08-22 21:52:13
![](https://img.aspxhome.com/file/2023/4/56164_0s.jpg)
python读取文本中数据并转化为DataFrame的实例
2021-10-24 13:08:28
![](https://img.aspxhome.com/file/2023/5/77785_0s.jpg)
python生成九宫格图片
2022-09-09 04:46:34
![](https://img.aspxhome.com/file/2023/5/81475_0s.png)
Python机器学习性能度量利用鸢尾花数据绘制P-R曲线
2023-01-27 20:55:48
![](https://img.aspxhome.com/file/2023/5/82905_0s.png)
python设置Pyplot的动态rc参数、绘图的填充
2023-12-15 22:52:29
![](https://img.aspxhome.com/file/2023/9/92169_0s.png)
Python基于Logistic回归建模计算某银行在降低贷款拖欠率的数据示例
2023-11-27 21:26:53
![](https://img.aspxhome.com/file/2023/8/70208_0s.png)