Python 查询 IP 地址
本帖最后由 qiuyouzhi 于 2020-3-31 15:27 编辑Python 查询 IP 地址
from requests import get
from bs4 import BeautifulSoup as BS
def open_url(url):
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'}
res = get(url, headers = headers)
return res
def get_ipInfo(res):
soup = BS(res.text, "html.parser")
target = soup.find_all("td", bgcolor = "#FFFFFF", style = "text-align: center")
i = 0
for each in target:
if i == 0:
print("IP: ", each.text)
i += 1
continue
print("地址:", each.text)
def main():
while True:
ip = input("请输入IP:")
if ip == 'Q!':
break
else:
for i in ip:
try:
int(i)
except:
print("输入错误!")
break
url = 'https://ip.51240.com/%s__ip/' % ip
res = open_url(url)
get_ipInfo(res)
if __name__ == "__main__":
main()
get_ipInfo写的不是特别好,但为了实现效果,就这样了 一个账号 发表于 2020-3-31 15:21
输入 "abc" 报错
让你输IP你输abc? 一个账号 发表于 2020-3-31 15:21
输入 "abc" 报错
改了 永恒的蓝色梦想 发表于 2020-3-31 15:21
让你输IP你输abc?
估计他把这个玩意当成算法题了 一个账号 发表于 2020-3-31 15:21
输入 "abc" 报错
IP好像不包含英文字符吧 _2_ 发表于 2020-3-31 16:05
IP好像不包含英文字符吧
我知道 qiuyouzhi 发表于 2020-3-31 15:29
估计他把这个玩意当成算法题了
输入格式错误的 IP "9090.1.1.1" 报错 一个账号 发表于 2020-3-31 17:18
输入格式错误的 IP "9090.1.1.1" 报错
当然会报错啊。。。。你以为这是算法题? zltzlt 发表于 2020-3-31 20:19
当然会报错啊。。。。你以为这是算法题?
不是,我觉得应该提示一下
页:
[1]