我從 API 獲取資訊,并將其作為生成的 JSON 檔案獲取:
{'business_discovery': {'media': {'data': [{'media_url': 'a link',
'timestamp': '2022-01-01T01:00:00 0000',
'caption': 'Caption',
'media_type': 'type',
'media_product_type': 'product_type',
'comments_count': 1,
'like_count': 1,
'id': 'ID'},
{'media_url': 'link',
# ... and so on
# NOTE: I scrubbed the numbers with dummy data
我知道data我可以運行這個腳本來獲取所有資料data
# "a" is the json without business discovery or media, which would be this:
a = {'data': [{'media_url': 'a link',
'timestamp': '2022-01-01T01:00:00 0000',
'caption': 'Caption',
'media_type': 'type',
'media_product_type': 'product_type',
'comments_count': 1,
'like_count': 1,
'id': 'ID'},
{'media_url': 'link',
# ... and so on
media_url,timestamp,caption,media_type,media_product_type,comment_count,like_count,id_code = [],[],[],[],[],[],[],[]
for result in a['data']:
media_url.append(result[u'media_url']) #Appending all the info within their Json to a list
timestamp.append(result[u'timestamp'])
caption.append(result[u'caption'])
media_type.append(result[u'media_type'])
media_product_type.append(result[u'media_product_type'])
comment_count.append(result[u'comments_count'])
like_count.append(result[u'like_count'])
id_code.append(result[u'id']) # All info exists, even when a value is 0
df = pd.DataFrame([media_url,timestamp,caption,media_type,media_product_type,comment_count,like_count,id_code]).T
當我對來自 api 的資訊運行上述命令時,我收到錯誤訊息,提示data找不到
這目前作業正常,但試圖找出一種方法來“跳過”業務發現和媒體,data以便我可以更有效地運行它,而不是復制和粘貼我跳過業務發現和媒體的地方
uj5u.com熱心網友回復:
使用json.normalize
df = pd.json_normalize(data=data["business_discovery"]["media"], record_path="data")
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/518556.html
