How to Itreate through Json - python

I need [0] to increase everytime and fetch the data when index change. from 0 to 13
import requests as r
import json
url = "https://services6.arcgis.com/bKYAIlQgwHslVRaK/arcgis/rest/services/CasesByRegion_ViewLayer/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json"
response = urlopen(url)
Data= json.load(response )
for index in Data:
list = Data['features'][0]['attributes']
[0]+1
print(list)

Here is another simple approach without using urllib:
import requests as r
import json
url = "https://jsonplaceholder.typicode.com/todos/1"
response = r.get(url)
data = response.json()
print(data)

requests.get().json() delivers the complete dict from the response payload:
import requests as r
response = r.get(url)
Data = response.json()
Your json.load() doesn't work as expected because response is a dictionary from the requests module, containing some HTTP stuff like status code, reason, encoding. For API calls, this is not what you want (HTTP errors should be handled with exceptions). What you want is response.json() or response.text.
Also, you imported requests but didn't use it? I don't know about urlopen(). Use requests.get().

Related

How to get JSON response in Python?

I've been trying to get JSON response out of the following code, but it's throwing an error
from requests import get
import json
url = "https://api.wheretheiss.at/v1/satellites/25544"
response = get(url)
for item in response:
print(item, response[item])
I wanna print the JSON in the following format:
https://i.stack.imgur.com/ZWMbr.png
your code works well to get the correct respone from the url. The problem is that you should parse the raw respone as the json format. Please refer to the following code:
from requests import get
import json
url = "https://api.wheretheiss.at/v1/satellites/25544"
response = get(url)
# dump response into json
result = response.content.decode('utf-8')
result = json.loads(result)
print(result) # print raw json
# print as key-value pairs
for item in result:
print("{0} : {1}".format(item, result[item]))
The output is like following:
Example output
I think you're misunderstanding what the response contains.
Your initial code is good.
from requests import get
import json
url = "https://api.wheretheiss.at/v1/satellites/25544"
response = get(url)
You now have a response. see here for more info.
In this response you have status_code, content, encoding, and the response content as text. it also contains other information about the request. eg we don't want to continue if the request failed
You can simply parse the json by calling json.loads on the decoded response via response.text
parsed_json = json.loads(response.text)
or
parsed_json = response.json()
for item in json_:
print(item, json_[item])
I hope it helps. it is also duplicate to HTTP requests and JSON parsing in Python

Check if return a JSON or not

I wrote a simple script to check if a URL return me a JSON.
I'm using request to check if return 200 and is working.
But now i need to check if the URL return me a JSON. I don't need to open it. Just check if is a JSON or not, because even if the URL returns me a 200 doesn't mean that is JSON file I need.
How can I check if result.json() is true? I tried to check len but if the site don't have JSON my script crashes.
import pandas as pd
from requests import get
lista = pd.read_csv('sites.csv', sep=',')
df = pd.DataFrame(lista, columns=['Site', 'Json'])
newdf = df.assign(Site=df['Site'].map(str) + 'Json')
for i in newdf['Site']:
result = get(i)
result.json()
An option could be to check the response headers, which means you don't need to try and parse the response at all:
'application/json' in result.headers.get('Content-Type')

How to set params in Python requests library

I have the following code using urllib in Python 2.7 and its working. I'm trying to do the same request using the requests library but I cant get it to work.
import urllib
import urllib2
import json
req = urllib2.Request(url='https://testone.limequery.com/index.php/admin/remotecontrol',\
data='{\"method\":\"get_session_key\",\"params\":[\"username\",\"password\"],\"id\":1}')
req.add_header('content-type', 'application/json')
req.add_header('connection', 'Keep-Alive')
f = urllib2.urlopen(req)
myretun = f.read()
j=json.loads(myretun)
print(j['result'])
Using requests library( Doesn't work)
import requests
import json
d= {"method":"get_session_key","params":["username","password"],"id":"1"}
headers = {'content-type' :'application/json','connection': 'Keep-Alive'}
req2 = requests.get(url='https://testone.limequery.com/index.php/admin/remotecontrol',data=d,headers=headers)
json_data = json.loads(req2.text)
print(json data['result'])
I'm getting an error JSONDecodeError: Expecting value: line 1 column 1 (char 0) How can I make the code work with the requests library?
First, you're sending the wrong type of request. You're sending a GET request, but you need to send a POST, with requests.post.
Second, passing a dict as data will form-encode the data rather than JSON-encoding it. If you want to use JSON in your request body, use the json argument, not data:
requests.post(url=..., json=d)
Reference Link: http://docs.python-requests.org/en/master/api/
You can use requests module of python like so
import requests
Req = requests.request(
method = "GET", # or "POST", "PUT", "DELETE", "PATCH" etcetera
url = "http(s)://*",
params = {"key": "value"}, # IF GET Request (Optional)
data = {"key": "value"}, # IF POST Request (Optional)
headers = {"header_name": "header_value"} # (Optional)
)
print Req.content
You can surround the code with try::catch block like below to catch any exception thrown by requests module
try:
# requests.request(** Arguments)
except requests.exceptions.RequestException as e:
print e
For full argument list, please check reference link.

Grab data from bitfinex with Python

I'm using this :
import requests
url = "https://api.bitfinex.com/v1/pubticker/btcusd"
response = requests.request("GET", url)
print (response.text)
The output is:
{"mid":"4432.95","bid":"4432.9","ask":"4433.0","last_price":"4432.9","low":"4276.9","high":"4482.0","volume":"32877.86104158","timestamp":"1506955900.864889"}
I would like to grab the last price, as a float, in order to use it for some calculations....
I cannot figure out how to do that...
Can you help me please ?
Thx
Try like this:
import requests
url = "https://api.bitfinex.com/v1/pubticker/btcusd"
response = requests.request("GET", url)
data = response.json()
last_price = float(data.get('last_price'))
print(last_price)
Response is a JSON so requests will do the job of parsing it for you, if you call the .json() method on your response. You just need to do:
import requests
resp_json = requests.get("https://api.bitfinex.com/v1/pubticker/btcusd").json()
# resp_json is a native Python dict representing the JSON response content. But since all values are strings in the response, you need to convert them to a float manually.
print(float(resp_json["last_price"]))

Python web scrape data request error

I'm trying to retrieve the response json data by a web site that I call.
The site is this:
WebSite DriveNow
On this page are shown on map some data. With browser debugger I can see the end point
end point
that sends response data json.
I have use this python to try scrape the json response data:
import requests
import json
headers = {
'Host': 'api2.drive-now.com',
'X-Api-Key': 'adf51226795afbc4e7575ccc124face7'
}
r = requests.get('https://api2.drive-now.com/cities/42756?expand=full', headers=headers)
json_obj = json.loads(r.content)
but I get this error:
hostname doesn't match either of 'activityharvester.com'
How I can retrieve this data?
Thanks
I have tried to call the endpoint that show json response using Postam, and passing into Header only Host and Api-Key. The result is the json that i want. But i i try the same call into python i recive the error hostname doesn't match either of 'activityharvester.com'
I don't understand your script, nor your question. Why two requests and three headers ? Did you mean something like this ?
import requests
import json
headers = {
'User-Agent': 'Mozilla/5.0',
'X-Api-Key':'adf51226795afbc4e7575ccc124face7',
}
res = requests.get('https://api2.drive-now.com/cities/4604?expand=full', headers=headers, allow_redirects=False)
print(res.status_code, res.reason)
json_obj = json.loads(res.content)
print(json_obj)

Categories

Resources