Python对TXT文档去重和对接口返回状态码探测

今日份工作需要,记录一下。

TXT文档去重

line_seen=set()#初始化空的无序集合

in_file=open('IP.txt','r')

out_file=open('EIP.txt','w')

lines=in_file.readlines()

for line in lines:

    if line not in line_seen:

        print(line)
        out_file.write(line)

        line_seen.add(line)

in_file.close()

out_file.close()

看下效果
在这里插入图片描述

接口返回状态码探测

import requests
import random

user_agents = ['Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
                   'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
                   'Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11']
                   
f=open("url.txt","r")    
lines = f.readlines()
for url in lines:
    headers = {'User-Agent': random.choice(user_agents)}
    response = requests.get(url,headers = headers)
    response.enconding = "utf-8"

    if str(response) == '<Response [404]>':
        print(url+'请求404')
    if str(response) == '<Response [200]>':
        print(url+'请求200')

看下效果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值