马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 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写的不是特别好,但为了实现效果,就这样了 |