我在请求我的 IP 时收到 json.decoder.JSONDecodeError
I got a json.decoder.JSONDecodeError while requesting my IP
import requests
headers = {
"Host" : "iplocation.com"
}
res= requests.get("https://iplocation.com/", headers=headers).json()
print(res)
错误
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
我该怎么办?
来自 https://iplocation.com/ 的响应是 HTML 响应,而不是 JSON。此行将 return HTML 文本响应:
res = requests.get("https://iplocation.com/", headers=headers).text
您可能正在寻找一个 JSON 端点来获取您的 public IP 地址。像下面这样的东西应该可以工作:
import requests
res = requests.get("https://api.ipify.org?format=json").json()
print(res['ip'])
import requests
headers = {
"Host" : "iplocation.com"
}
res= requests.get("https://iplocation.com/", headers=headers).json()
print(res)
错误
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
我该怎么办?
来自 https://iplocation.com/ 的响应是 HTML 响应,而不是 JSON。此行将 return HTML 文本响应:
res = requests.get("https://iplocation.com/", headers=headers).text
您可能正在寻找一个 JSON 端点来获取您的 public IP 地址。像下面这样的东西应该可以工作:
import requests
res = requests.get("https://api.ipify.org?format=json").json()
print(res['ip'])