Python获取域名C段IP地址代码示例
import requests import re
class GetCSectionIP: def init(self): self.url2ip = 'http://ip.tool.chinaz.com/' self.headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
def getUrlIp(self, domain):
url = self.url2ip + domain
res = requests.get(url, headers=self.headers)
res.encoding = 'utf-8'
sHTML = res.text
if sHTML.find('没有找到相关的域名') == -1 and sHTML.find('禁止查询该域名') == -1:
ip_pattern = re.compile('<div id="J_ip_history">.*?<a href="/(.*?)/" target="_blank">', re.S)
result = ip_pattern.search(sHTML)
if result:
ip = result.group(1)
c_section = '.'.join(ip.split('.')[:2]) + '.'
print(domain + ' 的C段IP地址为:' + c_section)
return c_section
else:
print(domain + ' 未查询到相应IP地址')
原文地址: https://www.cveoy.top/t/topic/m3lj 著作权归作者所有。请勿转载和采集!