我對字典之間的匹配方式有疑??問
我向 youtube api 發送 3 個請求
- 首先搜索
- 僅次于視頻
- 第三頻道
我嘗試創建的是具有
- 視頻標題
- 視頻縮略圖
- 以及制作視頻的頻道的個人資料。
在這里你可以看到我發送的代碼和請求:
def get(self,request):
search_url = "https://www.googleapis.com/youtube/v3/search"
video_url = "https://www.googleapis.com/youtube/v3/videos"
channel_url = "https://www.googleapis.com/youtube/v3/channels?part=snippet&id=' commaSeperatedList '&fields=items(id,snippet/thumbnails)&key={}".format(settings.YOUTUBE_DATA_API_KEY)
para_search = {
'part': 'snippet',
'q': 'Learn Python' ,
'key': settings.YOUTUBE_DATA_API_KEY,
'maxResults': 3,
'type': 'video'
}
search_response = requests.get(search_url,params=para_search)
print(search_response.text)
results = search_response.json()['items']
ids =[]
for result in results:
ids.append(result['id']['videoId'])
para_videos = {
'part': 'snippet',
'key': settings.YOUTUBE_DATA_API_KEY,
'id':','.join(ids),
}
video_response = requests.get(video_url, params=para_videos)
print(video_response.text)
results = video_response.json()['items']
dict_youtube = {}
list_youtube = []
channelIdList = []
for result in results:
dict_youtube = {
'title': result['snippet']['title'],
'thumbnails': result['snippet']['thumbnails']['high']['url'],
'channelId': result['snippet']["channelId"],
}
channelIdList.append(result['snippet']["channelId"])
list_youtube.append(dict_youtube)
param_channel = {
'part':'snippet,contentDetails,statistics',
'key':settings.YOUTUBE_DATA_API_KEY,
'id':','.join(channelIdList)
}
channel_response = requests.get(channel_url,params=param_channel)
print(channel_response.text)
results = channel_response.json()['items']
profile = []
profile_dic = {}
for result in results:
profile_dic = {
'channelId': result['id'],
'profile': result['snippet']['thumbnails']['default']['url'],
}
profile.append(profile_dic)
print(profile)
print(list_youtube)
輸入:
profile = [{'channelId': 'UC8butISFwT-*******', 'profile': 'https://yt3.ggpht.com/ytc/A*******ifQn-nYNfkgLvVPkw=s88-********-no-rj'}, {'channelId': 'UCWv7*******mDpPBA', 'profile': 'https://yt3.ggpht.com/tBEPr-zTNXEeae7VZK******2PXSwzMBKVR7W0MI7gyND8=s88-c-k-c0x00ffffff-no-rj'}]
list_youtube = [{'title': 'Learn Python - Full Course for Beginners [Tutorial]', 'thumbnails': 'https://i.ytimg.com/vi/rf****bw/hqdefault.jpg', 'channelId': 'UC******wT-Wl7EV0hUK0BQ'}, {'title': 'Python for Beginners - Learn Python in 1 Hour', 'thumbnails': 'https://i.ytimg.com/vi/kqt*****8/hqd****t.jpg', 'channelId': 'UCWv7*********pPBA'}, {'title': 'Python Tutorial - Python Full Course for Beginners', 'thumbnails': 'https://i.ytimg.com/vi/_uQrJ0TkZlc/hqdefault.jpg', 'channelId': 'U********PBA'}]
如您所見,我創建了兩個串列,每個串列都有字典。
每個字典都有一個我創建的公共鍵,它是channelId
我想要做的是在 channelId 鍵中具有相同值的字典之間的聯合,第一個串列的字典比第二個串列少。
我如何結合這兩個串列和字典,以便一切都兼容最終我將有一個包含字典的串列,其中包含密鑰
- '標題':
- “縮略圖”:
- “頻道 ID”:
- '輪廓':
例如:
[{'title':.... , 'thumbnails':... , 'channelId':... , 'profile':... ,} , { ... }, ...]
uj5u.com熱心網友回復:
這聽起來像是經典的連接操作。您可以使用過濾器來識別與 id 匹配的元素,然后使用其值更新字典,如下所示。
這假設您每個組態檔最多有 1 個視頻。您可能需要根據它們的關系翻轉變數/添加邏輯
for dic in profile:
vids = filter(lambda yt: yt["channelId"] == dic["channelId"], list_youtube)
for vid in vids:
dic.update(vid)
return dic
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/462553.html
下一篇:使用api構建表與反應表
