Python 代碼實現 iP地址歸屬地查詢


主要使用站長工具爬取,僅用於學術研究,勿用於商業場景

import re
import requests
import time

url = "http://ip.tool.chinaz.com/"
rg=r'<em>[-*\u4E00-\u9FA5A-Za-z0-9_& ]*?<\/em>'
burn = [r'<em>', r'</em>']

def getIP(ipstr):
    while True:
        res = requests.get(url+ipstr)
        for i in re.findall(rg, res.text):
            for j in burn:
                i = i.replace(j, '')
            return i
        time.sleep(1)

利用Flask服務器日志查看后台訪問IP的地址及其次數

# 運行參考
# PS F:\Output\IPco> python .\fresh.py .\1.txt

import re
import pprint
import sys

name=sys.argv[1]

rg = r'\n(?:[0-9]{1,3}\.){3}[0-9]{1,3} \- \- \['
burn = ['\n', ' - - [']

allIP = []
with open(name, 'r', encoding='utf8') as f:
    for i in re.findall(rg, f.read()):
        for j in burn:
            i = i.replace(j, '')
        allIP.append(i)


import collections
# allIP = list(set(allIP))
allIP = dict(collections.Counter(allIP))
allIP = list(allIP.items())
allIP.sort(key=lambda x: -x[1])


import requests
import time

url = "http://ip.tool.chinaz.com/"
rg=r'<em>[-*\u4E00-\u9FA5A-Za-z0-9_& ]*?<\/em>'
burn = [r'<em>', r'</em>']

def getIP(ipstr):
    while True:
        res = requests.get(url+ipstr)
        for i in re.findall(rg, res.text):
            for j in burn:
                i = i.replace(j, '')
            return i
        time.sleep(1)

# print(getIP("119.90.42.91"))

for ipS, cou in allIP:
    print(f"{cou}\t{ipS}\t{getIP(ipS)}")


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM