首页 > 编程语言 > 详细

Python检测批量URL状态,并将返回正常的URL保存文件

时间:2018-04-20 16:49:22      阅读:212      评论:0      收藏:0      [点我收藏+]
#!/usr/bin/python

-- coding: UTF-8 --

#author == huangyishan
import os
import sys
import urllib2
urls = sys.argv[1] #从程序外部调用参数,0即程序本身
result = list()
def check_url_status():
f = open(urls,‘r‘) #以读方式打开文件
for line in f.readlines(): #依次读取每行
line = line.strip() #去掉每行头尾空白
if len(line) !=0:
if line[0:7]==‘http://‘ or line[0:8]==‘https://‘:
pass
else:
line=‘http://‘+line
print line
try:
#response = urllib2.urlopen(line,timeout=4)
status = urllib2.urlopen(line,timeout=4).code
#print response
print status
result.append(line)
open(‘url_ok.txt‘, ‘w‘).write(‘%s‘ % ‘\n‘.join(result)) #保存入结果文件
except urllib2.HTTPError, e:
print e.code
with open(‘url_notok.txt‘, ‘w‘) as f: #保存入结果文件
f.write(line + ‘ : ‘ + str(e.code) + ‘\n‘)
except:
print "error"
with open(‘url_notok.txt‘, ‘a‘) as f: #保存入结果文件
f.write(line + ‘ : ‘ + ‘error‘ + ‘\n‘)

if name == ‘main‘:
check_url_status()

Python检测批量URL状态,并将返回正常的URL保存文件

原文:http://blog.51cto.com/linuxpython/2105821

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!