修订版 | 9334aeefa44010274c72baa383cd21356c296c2d (tree) |
---|---|
时间 | 2025-01-18 08:52:41 |
作者 | Lorenzo Isella <lorenzo.isella@gmai...> |
Commiter | Lorenzo Isella |
A script to retrieve a json file from the api link of data.europa.eu.
@@ -0,0 +1,21 @@ | ||
1 | +# Load necessary libraries | |
2 | +library(httr) | |
3 | + | |
4 | +# Function to download JSON from a URL with multiple tabs | |
5 | +download_json_from_url <- function(url, output_file = "results.json") { | |
6 | + # Add headers to specify the type of content we want (JSON) | |
7 | + response <- GET(url, add_headers(Accept = "application/json")) | |
8 | + | |
9 | + # Check if the response status code is successful | |
10 | + if (status_code(response) == 200) { | |
11 | + # Save the JSON content to the specified file | |
12 | + write(content(response, as = "text", encoding = "UTF-8"), file = output_file) | |
13 | + message("JSON file saved to: ", output_file) | |
14 | + } else { | |
15 | + stop("Failed to download JSON content. Status code: ", status_code(response)) | |
16 | + } | |
17 | +} | |
18 | + | |
19 | +# Example usage | |
20 | +url_example <- "https://data.europa.eu/api/hub/search/datasets/f9c9501a-9821-4fde-9c53-993fe13dde78" # Replace with your JSON tab URL | |
21 | +download_json_from_url(url_example, "../output/test.json") |