API Query for loop
The first part of this:
library(jsonlite)
library(httr)
library(lubridate)
library(tidyverse)
base_url <- "http://api.kuroganehammer.com"
res <- GET(url = base_url, path = "api/characters")
content(res, as="text", encoding="UTF-8") %>%
fromJSON(flatten=TRUE) %>%
as_tibble() -> chars
Gets you a data frame of the characters.
This:
pb <- progress_estimated(length(chars$id))
map_df(chars$id, ~{
pb$tick()$print()
Sys.sleep(sample(seq(0.5, 2.5, 0.5), 1)) # be kind to the free API
res <- GET(url = base_url, path = sprintf("api/characters/%s/detailedmoves", .x))
content(res, as="text", encoding="UTF-8") %>%
fromJSON(flatten=TRUE) %>%
as_tibble()
}, .id = "id") -> moves
Gets you a data frame of all the "moves" and adds the "id" for the character. You get a progress bar for free, too.
You can then either left_join()
as needed or group & nest the moves data into a separate list-nest column. If you want that to begin with you can use map()
vs map_df()
.
Leave in the time pause code. It's a free API and you should likely increase the pause times to avoid DoS'ing their site.
How to loop a list of queries via restful api?
Try this:
import requests
query = ["apple", "orange", "banana", "cherry", "blueberry"]
url = 'https://api.fruits.com/Search?'
data_list = []
for item in query:
parameters = {
"key": "1234567890abc",
"rt": "json",
"mode": "full",
"query": item
}
response = requests.get(url, params=parameters)
data = response.json()
data_list.append(data['response']['TotalFound'])
print(data_list)
Is it possible to loop through an API, print separate results, and then combine them into a single variable?
const URLs = [
"https://www.reddit.com/r/SEO/comments/tepprk/is_ahrefs_worth_it/",
"https://www.reddit.com/r/juststart/comments/jvs0d1/is_ahrefs_worth_it_with_these_features/",
];
Promise.all(
URLs.map(async (url) => {
const resp = await fetch(url + ".json");
return resp.json();
})
).then((res) => console.log(res));
Python for loop API request
Querying multiple APIs iterativelly will take a lot of time. Consider using theading
or AsyncIO
to do requests simultaniously and speed up the process.
In a nutshell you should do something like this for each API:
import threading
for provider in [...]: # list of APIs to query
t = threading.Thread(target=api_request_function, args=(provider, ...))
t.start()
However better read this great article first to understand whats and whys of threading approach.
for loop in API call returns same result in each loop
You are not passing in the t
from the loop. The t
in the params
definition is unrelated; if I were to run your code now I'd get a NameError
exception because t
hasn't been set at that point. The t
expression in the params
mapping is not live, it is not updated each loop iteration.
Set the 'aTokenQueryProperties'
key in the loop:
method = "get_participant_properties"
params = OrderedDict([
("sSessionKey", api.session_key),
("iSurveyID", 12345),
("aTokenQueryProperties", None),
])
attributes = []
for t in ids:
params["aTokenQueryProperties"] = t
attributes.append(api.query(method=method, params=params))
Setting "aTokenQueryProperties"
to None
in the params
OrderedDict
object at the start is optional; you'd only need to do this if reserving its exact location in the params
order is important and even then, because in your example it is the last element in the mapping, you'd end up with the same output anyway.
Related Topics
Fastest Way to Get Min from Every Column in a Matrix
Making Binned Scatter Plots for Two Variables in Ggplot2 in R
Plot Only One Side/Half of the Violin Plot
R Multiple Conditions in If Statement
Remove Zombie Processes Using Parallel Package
Split a File Path into Folder Names Vector
Creating a More Continuous Color Palette in R, Ggplot2, Lattice, or Latticeextra
Dplyr: Grouping and Summarizing/Mutating Data with Rolling Time Windows
Unexpected Behaviour with Argument Defaults
Specify Function Parameters in Do.Call
How to Hide/Toggle Legends Based on Addlayercontrol() in Leaflet for R
Subsetting R Array: Dimension Lost When Its Length Is 1
How to Run a Job Array in R Using the Rscript Command from the Command Line
No Dimensions of Non-Empty Numeric Vector in R
Major and Minor Tickmarks with Plotly