首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Ip地址检查器不输出IP地址

Ip地址检查器不输出IP地址
EN

Stack Overflow用户
提问于 2020-05-13 15:09:35
回答 1查看 148关注 0票数 0

按照这里的代码,我得到了一个IP地址检查器。但是,它没有输出IP地址,而是输出[]。代码:

代码语言:javascript
运行
复制
import urllib.request
import re

print("we will try to open this url, in order to get IP Address")

url = "http://checkip.dyndns.org"

print(url)

request = urllib.request.urlopen(url).read()

theIP = re.findall(r"d{1,3}.d{1,3}.d{1,3}.d{1,3}", request.decode('utf-8'))


print("your IP Address is: ",  theIP)

预期产出:

代码语言:javascript
运行
复制
we will try to open this url, in order to get IP Address
http://checkip.dyndns.org
your IP Address is: 40.74.89.185

那里的IP地址不是我的,它来自 这里

实际产出:

代码语言:javascript
运行
复制
we will try to open this url, in order to get IP Address
http://checkip.dyndns.org
your IP Address is:  []

我刚刚从网站复制,然后修复错误。我做错了什么。救命啊..。

我的python版本是空闲的3.8。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-13 15:18:17

原来您的regex哪里出错了:我已经更新了代码并使用了请求get:

findall将返回一个元素列表,因为您只有一个ip返回--只需使用

代码语言:javascript
运行
复制
from requests import get
import re
iphtml = get('http://checkip.dyndns.org').text
theIP = re.findall( r'[0-9]+(?:\.[0-9]+){3}', iphtml)
print(f"Your IP is: {theIP[0]}")

您的代码已更新:

代码语言:javascript
运行
复制
import urllib.request
import re

print("we will try to open this url, in order to get IP Address")

url = "http://checkip.dyndns.org"

print(url)

request = urllib.request.urlopen(url).read()

theIP = re.findall(r'[0-9]+(?:\.[0-9]+){3}', request.decode('utf-8'))


print("your IP Address is: ",  theIP[0])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61778166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档