【发布时间】:2020-03-19 15:48:26
【问题描述】:
大家好,感谢您的宝贵建议。我通常会找到一些帮助,所以这是我的第一个问题。经过很长时间后,我设法从这段代码中得到了可用的响应。很多文章不适用于python3。
import re
import json
import urllib.request, urllib.error, urllib.parse
import hashlib
import uuid
#! /usr/bin/env python
try:
# For Python 3.0 and later
from urllib.request import urlopen
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import urlopen
html = urlopen("http://ipinfo.io/json")
#result = html(buf.decode('utf-8'))
print(html.read())
回复以这种形式出现。
b'{\n "ip": "84.0.23.49",\n "hostname": "cpc127884-ldry4-2-0-
cust47.know.cable.virginm.net",\n "city": "London",\n "region":
"UK",\n "country": "GB",\n "loc": "54.9971,-7.3073",\n "org":
"AS5089 Virgin Media Limited",\n "postal": "BT34",\n "timezone":
"Europe/London",\n "readme": "https://ipinfo.io/missingauth"\n}'
我不知道这是我的位置或互联网服务,还是 RPi,或者这可能是新格式。顺便说一句,我更改了一些位置详细信息。但是似乎没有一篇文章描述如何以这种形式获取信息。它通常看起来不同。我尝试时遇到很多错误 - 例如告诉我数据是二进制而不是 str 我如何从这个“字典”中获取我的位置或城市?
【问题讨论】:
标签: json python-3.x raspberry-pi urllib urlopen