在python中合并来自2个不同api get调用的数据帧

2024-04-20 02:38:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python的初学者,我有两个代码,分别运行时可以完美地工作。但是当我把它们放在一个程序中的函数中时就不起作用了。你知道吗

1)在一个特定的网站中,我得到了一个json响应,其中包含100个doment的详细信息

2)在第二段代码中,我获得了每个文档作者的详细信息,其中authord是从第一段代码中检索到的。你知道吗

因此,我必须从第一个代码中检索authord,并将其传递给第二个代码中api调用的url端点。然后将main函数中的2个数据帧与common authord合并。你知道吗

url=“https://api jj.com/ugt0/xyz/v1/people/%authorID“当 给出如下url=“https://api-jj.com/ugt0/xyz/v1/people/314684““

import requests
import pandas as pd
import numpy as np
from pandas.io.json import json_normalize
import sys
import json
import csv

global headers
headers = {
    'ConnectAuthorization': "Basic z",
    'Authorization': "Bearer Q",
    'Cache-Control': "no-cache",
    'Postman-Token': "1"
    }
global payload
payload=''

r=requests.get(url,data=payload,headers=headers,verify=False)
json_data=r.json()


def get_contendetails():
    try:


        url="https://api/ugt0/xyz/v1/contents?count=100"
        r=requests.get(url,data=payload,headers=headers,verify=False)
        json_data=r.json()
        if json_data =='' or None :
            return [], 'Got empty response'
        else:
            for i in json_data.get('list',[]):
                dfDICT = {'Content_id':[i.get('contentID',None)],
                          'subject':[i.get('subject',None)],
                          'published':[i.get('published',None)],
                          'updated':[i.get('updated',None)],
                          'likeCount':i.get('likeCount',None),
                          'replyCount':i.get('replyCount',None),
                          'viewCount':i.get('viewCount',None)
                          }

                if i.get('author',None):  # Test if your key exists
                    dfDICT['Author'] = i.get('author').get('displayName',None)

                else:
                    dfDICT['Author'] = None
                if i.get('author',None):
                    dfDict['authorID']=i.get('author').get('id',None)
                else:
                    dfDICT['authorID']=None

            df1=df1.append(pd.DataFrame(dfDICT,index=[0]),ignore_index=True)
            return df1





def get_authordetails(authorID):
    try:
        url="https://api-jj.com/ugt0/xyz/v1/people/%authorID"
        r=requests.get(url,data=payload,headers=headers,verify=False)
        json_data=r.json()
        if json_data =='' or None :
            return [], 'Got empty response'
        else:
            for j in [json_data]:
                profile = j['jive']['profile']
                headers = [e['jive_label'] for e in profile]
                values  = [e['value'] for e in profile]

            df2 = pd.DataFrame(columns=headers, data=[values]).drop('BUFUGU','Preferred Language'axis[0])

            return df2
def main():
   s=get_contendetails()
   v=get_contendetails(authorID)
   f=pd.merge(s,v)

包含内容详细信息的第一部分代码的数据帧: enter image description here

第二部分代码的数据帧,其中包含一位作者的详细信息:

enter image description here


Tags: 代码httpsimportnoneapijsonurldata