日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python新奇检测_3种Python检测URL状态

發(fā)布時間:2025/3/12 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python新奇检测_3种Python检测URL状态 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

需求:Python檢測URL狀態(tài),并追加保存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*-

#Python學習交流群:778463939

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() #排序結(jié)果

#open('2.txt', 'w').write('%s' % '\n'.join(result)) #保存入結(jié)果文件

with open ('200urllib.txt','a') as f: ## r只讀,w可寫,a追加

f.write(x + '\n')

else:

print 'error'

總結(jié)

以上是生活随笔為你收集整理的python新奇检测_3种Python检测URL状态的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。