吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 230|回复: 5
收起左侧

[学习记录] python获取ip138接口ip地址并替换文本内旧ip地址

[复制链接]
iloong 发表于 2024-7-25 09:28
本帖最后由 iloong 于 2024-7-25 11:38 编辑

[Python] 纯文本查看 复制代码
import requests
import ipaddress
import os
import re
def get_ip138_ip(url,head):
    response = requests.get(url=url,headers=head)
    con_1 = response.text
    #print(re)
    obj = re.compile(r'<title>您的IP地址是:(?P<ipname>.*?)</title>')
    #result = obj.finditer(con_1)
    result = obj.finditer(con_1)
    for it in result:
        get_ip=it.group("ipname")
    return get_ip
def replace_ips_in_file(file_path, old_ip, new_ip):
    # 定义IP正则表达式
    ip_pattern = r'\b(?:[0-9{1,3}\.){3}[0-9{1,3}\b'
    # 读取文件内容
    with open(file_path, 'r') as file:
        content = file.read()
   
    # 查找所有IP并替换
    ip_regex = re.compile(ip_pattern)
    new_content = ip_regex.sub(new_ip, content)
   
    # 将新内容写回文件
    with open(file_path, 'w') as file:
        file.write(new_content)
url = 'https://2024.ip138.com/'
head = {
    "User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0"
}
get_ip = get_ip138_ip(url,head)  #获取到ip138接口ip地址
file_path = './baidu.txt'  # 替换为你的文本文件路径
ips = None   
if ips == get_ip:
    ips = get_ip
    #replace_ips_in_file(file_path,ips,get_ip)
elif ips != get_ip:
    replace_ips_in_file(file_path,ips,get_ip)
else:
    ips = 'xx.xx.xx.xx' #文本内旧ip地址
    replace_ips_in_file(file_path,ips,get_ip)
print(get_ip)

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Hmily 发表于 2024-7-25 11:16
从哪复制的,五颜六色,用一下代码框处理一下。
 楼主| iloong 发表于 2024-7-25 11:35
Hmily 发表于 2024-7-25 11:16
从哪复制的,五颜六色,用一下代码框处理一下。

vs code编辑器里面的,现在没权限发代码

点评

没权限是什么意思?  详情 回复 发表于 2024-7-25 14:21
feng_129 发表于 2024-7-25 13:50
这东西访问一次后缓存下来还行,多访问几次就被限制了。
Hmily 发表于 2024-7-25 14:21
iloong 发表于 2024-7-25 11:35
vs code编辑器里面的,现在没权限发代码

没权限是什么意思?
 楼主| iloong 发表于 2024-7-26 10:41
feng_129 发表于 2024-7-25 13:50
这东西访问一次后缓存下来还行,多访问几次就被限制了。

动态ip不重启设备的情况下,更新周期7周
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-9-8 09:56

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表