无法将 JSON 从 youtube APIv3 转换为我可以在 python 中使用的东西
Having trouble converting JSON from youtube APIv3 to something I can work with in python
现在我正试图通过 python 打印出 JSON。最终,我将使用所请求的 JSON 中的信息,但我坚持只是将其转化为我可以使用的东西。
import urllib
import urllib.request
import dateutil
import json
API_KEY = open("/Users/Sean/Documents/Yer.txt", "r")
API_KEY = API_KEY.read()
url = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=UUiufyZv8iRPTafTw0D4CvnQ&key='+API_KEY
response = urllib.request.urlopen(url)
videos = json.load(response)
print(videos)
这是我开始的地方。我收到错误
the JSON object must be str, not 'bytes'
在 "videos = json.load(response)" 行。
四处寻找并最终尝试
videos = json.load(response.decode())
我得到这个错误
Traceback (most recent call last):
File "C:\Users\Sean\Documents\NeebsBot\NeebsBot\NeebsBot\NeebsBot.py", line 13, in
videos = json.load(response.decode())
AttributeError: 'HTTPResponse' object has no attribute 'decode'
Press any key to continue . . .
再次搜索并尝试了这个。
response = urllib.request.urlopen(url)
content = response.read()
videos = json.loads(content.decode('utf8'))
print(videos)
当我 运行
时我得到了这个
'charmap' codec can't encode character '\xa9' in position 1573: character maps to
我在网上找到的所有解决方案总是让我回到这些错误。
您很可能收到 gzip 压缩的内容,作为对 Youtube API 的示例请求返回了以下 headers:
cache-control: private, max-age=0, must-revalidate, no-transform
content-encoding: gzip
content-length: 1706
content-type: application/json; charset=UTF-8
你有两个选择,你可以add the snippet from this question to decompress the content, or use the popular requests
library它会为你处理这一切。
在请求中,您的代码将是:
import requests
r = requests.get(your_url_goes_here)
results = r.json()
print(results)
现在我正试图通过 python 打印出 JSON。最终,我将使用所请求的 JSON 中的信息,但我坚持只是将其转化为我可以使用的东西。
import urllib
import urllib.request
import dateutil
import json
API_KEY = open("/Users/Sean/Documents/Yer.txt", "r")
API_KEY = API_KEY.read()
url = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=UUiufyZv8iRPTafTw0D4CvnQ&key='+API_KEY
response = urllib.request.urlopen(url)
videos = json.load(response)
print(videos)
这是我开始的地方。我收到错误
the JSON object must be str, not 'bytes'
在 "videos = json.load(response)" 行。
四处寻找并最终尝试
videos = json.load(response.decode())
我得到这个错误
Traceback (most recent call last): File "C:\Users\Sean\Documents\NeebsBot\NeebsBot\NeebsBot\NeebsBot.py", line 13, in videos = json.load(response.decode()) AttributeError: 'HTTPResponse' object has no attribute 'decode' Press any key to continue . . .
再次搜索并尝试了这个。
response = urllib.request.urlopen(url)
content = response.read()
videos = json.loads(content.decode('utf8'))
print(videos)
当我 运行
时我得到了这个'charmap' codec can't encode character '\xa9' in position 1573: character maps to
我在网上找到的所有解决方案总是让我回到这些错误。
您很可能收到 gzip 压缩的内容,作为对 Youtube API 的示例请求返回了以下 headers:
cache-control: private, max-age=0, must-revalidate, no-transform
content-encoding: gzip
content-length: 1706
content-type: application/json; charset=UTF-8
你有两个选择,你可以add the snippet from this question to decompress the content, or use the popular requests
library它会为你处理这一切。
在请求中,您的代码将是:
import requests
r = requests.get(your_url_goes_here)
results = r.json()
print(results)