python批量访问网页保存结果_Python检测批量URL状态,并将返回正常的URL保存文件...
-- coding: UTF-8 --
#author == huangyishan
import os
import sys
import urllib2
urls = sys.argv[1] #從程序外部調(diào)用參數(shù),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)) #保存入結(jié)果文件
except urllib2.HTTPError, e:
print e.code
with open(‘url_notok.txt‘, ‘w‘) as f: #保存入結(jié)果文件
f.write(line + ‘ : ‘ + str(e.code) + ‘\n‘)
except:
print "error"
with open(‘url_notok.txt‘, ‘a(chǎn)‘) as f: #保存入結(jié)果文件
f.write(line + ‘ : ‘ + ‘error‘ + ‘\n‘)
if name == ‘main‘:
check_url_status()
原文地址:http://blog.51cto.com/linuxpython/2105821
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的python批量访问网页保存结果_Python检测批量URL状态,并将返回正常的URL保存文件...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 删除win7多余的系统还原点_【Win7
- 下一篇: python 数据分析 实际案例_pyt