按照这里的代码,我得到了一个IP地址检查器。但是,它没有输出IP地址,而是输出[]
。代码:
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)
预期产出:
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地址不是我的,它来自 这里。
实际产出:
we will try to open this url, in order to get IP Address
http://checkip.dyndns.org
your IP Address is: []
我刚刚从网站复制,然后修复错误。我做错了什么。救命啊..。
我的python版本是空闲的3.8。
发布于 2020-05-13 15:18:17
原来您的regex哪里出错了:我已经更新了代码并使用了请求get:
findall
将返回一个元素列表,因为您只有一个ip返回--只需使用
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]}")
您的代码已更新:
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])
https://stackoverflow.com/questions/61778166
复制相似问题