问题描述
这可能是一个非常菜鸟的问题,但我真的在努力创建高效的 Python 代码。 背景:我正在尝试通过 API 获取数据并将该数据提取到 CSV 中。我面临的挑战是我从中提取 API 的公司允许您每页最多处理 20 个产品(因此,每个请求)。正如您从我的 df2 变量中看到的,我可以将所有产品合并为一个变量(尽管这是一种非常低效的方式)。我正在寻找可扩展的东西,因为我目前有大约 5,000 种产品(或 250 页)。任何帮助将不胜感激!
count = 1
allPages = []
for x in range(2):
url = "https://COMPANYNAME.repairshopr.com/api/v1/products?page="+str(count)
headers = CaseInsensitiveDict()
headers["accept"] = "application/json"
headers["Authorization"] = "Bearer APIVALUE"
resp = requests.get(url,headers=headers)
allPages.append(json.loads(resp.content.decode('utf-8')))
count += 1
# Determine how to get the list into a dataframe
df = DataFrame (allPages)
#Very inefficient way. Need to determine way to loop
df2 = DataFrame(df.iat[0,0] + df.iat[1,0] + df.iat[2,0] + df.iat[3,0] + df.iat[4,0] + df.iat[5,0] + df.iat[6,0] + df.iat[7,0] + df.iat[8,0] + df.iat[9,0] + df.iat[10,0] + df.iat[11,0] + df.iat[12,0] + df.iat[13,0] + df.iat[14,0] + df.iat[15,0] + df.iat[16,0] + df.iat[17,0] + df.iat[18,0] + df.iat[19,0])
# this is how you write to a CSV file
df2.to_csv(r'C:\Users\lukes\Dropbox\Luke,Dad & Jilly\Grand Tourer\Reports\File Name.csv',index = False)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)