利用Python检测URL状态
作者:白天的影子 时间:2022-12-29 23:03:52
需求:Python检测URL状态,并追加保存200的URL
代码一:
#! /usr/bin/env python
#coding=utf-8
import sys
import requests
def getHttpStatusCode(url):
try:
request = requests.get(url)
httpStatusCode = request.status_code
return httpStatusCode
except requests.exceptions.HTTPError as e:
return e
if __name__ == "__main__":
with open('1.txt', 'r') as f:
for line in f:
try:
status = getHttpStatusCode(line.strip('\n'))#换行符
if status == 200:
with open('200.txt','a') as f:
f.write(line + '\n')
print line
else:
print 'no 200 code'
except Exception as e:
print e
代码二:
#! /usr/bin/env python
# -*--coding:utf-8*-
import requests
def request_status(line):
conn = requests.get(line)
if conn.status_code == 200:
with open('url_200.txt', 'a') as f:
f.write(line + '\n')
return line13 else:
return None
if __name__ == '__main__':
with open('/1.txt', 'rb') as f:
for line in f:
try:
purge_url = request_status(line.strip('\n'))
except Exception as e:
pass
代码三:
#! /usr/bin/env python
#coding:utf-8
import os,urllib,linecache
import sys
result = list()
for x in linecache.updatecache(r'1.txt'):
try:
a = urllib.urlopen(x.replace('/n','')).getcode()
#print x,a
except Exception,e:
print e
if a == 200:
#result.append(x) #保存
#result.sort() #排序结果
#open('2.txt', 'w').write('%s' % '\n'.join(result)) #保存入结果文件
with open ('200urllib.txt','a') as f: ## r只读,w可写,a追加
f.write(x + '\n')
else:
print 'error'
总结
以上所述是小编给大家介绍的python 检测url 状态,网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
来源:https://www.cnblogs.com/x00479/archive/2019/07/31/11275208.html
标签:python,检测,url,状态
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决jupyter notebook import error但是命令提示符import正常的问题
2022-08-19 22:10:30
python实现不同数据库间数据同步功能
2024-01-18 15:58:52
python 如何求N的阶乘
2023-11-01 15:24:46
使用Python实现从各个子文件夹中复制指定文件的方法
2023-11-09 12:04:05
Python数据类型之String字符串实例详解
2022-01-08 11:38:50
JS关于 replace 取值、替换第几个匹配项问题小结
2024-04-25 13:11:05
Python WSGI的深入理解
2021-04-20 21:48:25
Sql Server查询性能优化之不可小觑的书签查找介绍
2024-01-18 22:32:48
![](https://img.aspxhome.com/file/2023/5/115305_0s.png)
pycharm激活码2020最新分享适用pycharm2020最新版亲测可用
2021-09-21 11:11:33
![](https://img.aspxhome.com/file/2023/1/90681_0s.png)
一篇文章带你了解Python之Selenium自动化爬虫
2023-12-15 14:30:53
![](https://img.aspxhome.com/file/2023/2/98802_0s.png)
使用Perl创建指定编码格式(如utf-8)文件的实现代码
2023-07-28 08:11:11
mysql报错:MySQL server version for the right syntax to use near type=InnoDB的解决方法
2024-01-20 19:17:17
在Python上基于Markov链生成伪随机文本的教程
2023-07-02 09:24:03
重命名批处理python脚本
2021-04-27 22:56:38
将字符实体引用转换成 Unicode 字符
2011-07-01 12:31:51
Python 如何引入同级包和模块
2022-12-01 02:47:37
![](https://img.aspxhome.com/file/2023/7/128177_0s.png)
Vue使用vux-ui自定义表单验证遇到的问题及解决方法
2024-05-10 14:18:07
![](https://img.aspxhome.com/file/2023/9/126459_0s.png)
pandas中df.groupby()方法深入讲解
2024-01-01 15:30:24
在Python的Bottle框架中使用微信API的示例
2022-06-02 00:12:47
![](https://img.aspxhome.com/file/2023/8/105808_0s.jpg)
React+TypeScript+webpack4多入口配置详解
2024-02-24 03:23:31