Overwrite response from api with python - python

I would like to get data from api, but api is presented in pages. So I have to iterate through all of them and save wanted data in variable.
I was trying to attach new page in loop and add data to my response, but only I got was error: "TypeError: must be str, not Response". I wanted to do it in this way:
response = "https://api.dane.gov.pl/resources/17201/data?page=1"
for i in range(2,32):
url = "https://api.dane.gov.pl/resources/17201/data?page="+str(i)
response += requests.get(url)
data = response.text
When I get the data I want to extract and operate on them.

requests.get(url) returns a Response object. At the moment, you are trying to add the Response object to a string.
Try something like this:
response = []
for i in range(2,32):
url = "https://api.dane.gov.pl/resources/17201/data?page="+str(i)
response.append(requests.get(url).text)
When that finishes running, response will be a list full of the response text instead of response objects.

Related

405 Error when I try to access data from an api link

I am trying to get data from an api, but when I try to access it with the following code I get the message (405 Error The method is not allowed for the requested URL.)
response = requests.get(url)
data = response.text
print(data)
You can try converting the json to dictionary by rewriting the code as
response = requests.get(url)
dict=response.json()
data = dict.text
print(data)

Python requests response - cant unnest the data object

I am querying an API and receiving back a response of type text
url = "https://tripadvisor1.p.rapidapi.com/reviews/list"
querystring = {"limit":"20","currency":"USD","lang":"en_US","location_id":"2269364"}
headers = {
'x-rapidapi-host': "xxx",
'x-rapidapi-key': "xxx"
}
response = requests.request("GET", url, headers=headers, params=querystring)
print(response.text)
I then want to retrieve a dataframe object. Various online sources suggest this approach:
d = json.loads(response.text)
e = json_normalize(d)
However, what I get back is this:
I would like to obtain a dataframe object from the 'data' column. Please advise if I can make this question clearer.
First off requests already has a json() method on the response object to decode JSON responses. Second, the top level object contains three things: data, paging_results, and paging_total_results which is why you see what you do.
If you want to extract the inner data contents, just pull that out before passing it to normalize:
response = ...
df = json_normalize(response.json()['data'])

Obtain both headers and content from single GET request with the Python requests library

I am trying to, with a single get request, obtain both headers and content.
At the moment, I am able to obtain them individually:
Headers=requests.get('https://coinmarketcap.com', verify=False).headers
and
ParseLM=requests.get('https://coinmarketcap.com', verify=False).content
However, this makes two separate GET requests while I am trying to parse both headers and content from the same request, although separately.
Call requests.get() once, saving the entire result:
response = requests.get('https://coinmarketcap.com', verify=False)
Then you can access individual pieces of the result:
headers = response.headers
content = response.content

Python Parse JSON Response from URL

I'm am wanting to get information about my Hue lights using a python program. I am ok with sorting the information once I get it, but I am struggling to load in the JSON info. It is sent as a JSON response. My code is as follows:
import requests
import json
response= requests.get('http://192.168.1.102/api/F5La7UpN6XueJZUts1QdyBBbIU8dEvaT1EZs1Ut0/lights')
data = json.load(response)
print(data)
When this is run, all I get is the error:
in load return loads(fp.read(),
Response' object has no attribute 'read'
The problem is you are passing in the actual response which consists of more than just the content. You need to pull the content out of the response:
import requests
r = requests.get('https://github.com/timeline.json')
print r.text
# The Requests library also comes with a built-in JSON decoder,
# just in case you have to deal with JSON data
import requests
r = requests.get('https://github.com/timeline.json')
print r.json
http://www.pythonforbeginners.com/requests/using-requests-in-python
Looks like it will parse the JSON for you already...
Use response.content to access response content and json.loads method instead of json.load:
data = json.loads(response.content)
print data

Making a GET request JSON with parameters using Python

I was wondering how do I make a GET request to a specific url with two query parameters? These query parameters contain two id numbers
So far I have:
import json, requests
url = 'http://'
requests.post(url)
But they gave me query paramters first_id=### and last_id=###. I don't know how to include these parameters?
To make a GET request you need the get() method, for parameters use params argument:
response = requests.get(url, params={'first_id': 1, 'last_id': 2})
If the response is of a JSON content type, you can use the json() shortcut method to get it loaded into a Python object for you:
data = response.json()
print(data)

Categories