I am trying to call the marvel API from here: https://developer.marvel.com
The code I wrote for it:
import requests, json
from pprint import pprint
# Call an API
url = "http(s)://gateway.marvel.com/"
response = requests.get(url)
response.raise_for_status() # check for errors
# Load JSON data into a Python variable.
jsonData = json.loads(response.text)
pprint(jsonData)
When I run it, I receive the error :
requests.exceptions.HTTPError: 409 Client Error: Conflict for url:
It is from rest api
import requests
import json
from pprint import pprint
# Call an API
response = requests.get('https://jsonplaceholder.typicode.com/users')
response.raise_for_status() # check for errors
print(response.status_code)
# Load JSON data into a Python variable.
jsonData = json.loads(response.text)
pprint(jsonData)
I'm assuming the url you posted is just a placeholder? The correct format should be something like https://gateway.marvel.com:443/v1/public/characters?apikey=yourpublickey.
You also need the to specify an endpoint, in my example I used the /characters endpoint.
Try removing the response.raise_for_status(). It should continue and give you an error message with what is wrong (according to the docs, almost all errors will give you a status code of 409).
import requests, json
from pprint import pprint
# Call an API
url = "https://gateway.marvel.com:443/v1/public/characters?apikey=yourpublickey."
response = requests.get(url)
# response.raise_for_status() # check for errors
# Load JSON data into a Python variable.
jsonData = json.loads(response.text)
pprint(jsonData)
Related
disclaimer- noob here!
i keep running into the issue that says I imported requests but am not using it. not sure how to proceed. I looked in my package library and it is there under site-packages, however I do not see the 'get' function in it. I am using PyCharm, and have also checked this Spyder. Same problem
import json
import requests
from requests import get as geturl
url = 'http://api.open-notify.org/iss-now.json'
response = geturl(url)
print(response)
result = json.loads(response.text)
print(result('iss_position'))
You are not using the name requests. You can safely remove the import requests line. Your from requests import get as geturl line suffices.
As a side note, you can load JSON responses directly without having to import json:
result = response.json()
so all you need is
from requests import get as geturl
url = 'http://api.open-notify.org/iss-now.json'
response = geturl(url)
result = response.json()
print(result['iss_position'])
>>> from requests import get as geturl
>>> url = 'http://api.open-notify.org/iss-now.json'
>>> response = geturl(url)
>>> result = response.json()
>>> print(result)
{'message': 'success', 'iss_position': {'latitude': '-40.8208', 'longitude': '103.2043'}, 'timestamp': 1509893252}
You can either use
import requests
requests.get(url, params=None, **kwargs)
Or
from requests import get as geturl
geturl(url, params=None, **kwargs)
You have used both, which is redundent and the requests is not being used as requests.get() that's was the reason why you were getting that message.
I am trying to fetch data from quandl using urllib2.Please check code below.
import json
from pymongo import MongoClient
import urllib2
import requests
import ssl
#import quandl
codes = [100526];
for id in codes:
url = 'https://www.quandl.com.com//api/v3/datasets/AMFI/"+str(id)+".json?api_key=XXXXXXXX&start_date=2013-08-30'
req = urllib2.Request(url)
response = urllib2.urlopen(req)
data = response.read()
print data
OR
for id in codes:
url = "https://www.quandl.com.com//api/v3/datasets/AMFI/"+str(id)+".json?api_key=XXXXXXXX&start_date=2013-08-30"
request = requests.get(url,verify=False)
print request
I am getting HTTPERROR exception 404 in 1st case. and when I use request module I get SSL error even after using verify=false. I am looking through previous posts but most of them are related to HTTP request.
Thanks for help.
J
This is working for me, but you get a warning about the SSL certificate but you don't need to care about it.
import requests
codes = [100526];
for id in codes:
url = "https://www.quandl.com.com//api/v3/datasets/AMFI/"+str(id)+".json?api_key=XXXXXXXX&start_date=2013-08-30"
request = requests.get(url, verify=False)
print request.text
request.text has your response data.
You seem to be using a wrong URL (.com.com instead of .com) as well as a combination of different quotes in the first version of your code. Use the following instead and it should work:
import urllib2
import requests
codes = [100526]
for id in codes:
url = "https://www.quandl.com//api/v3/datasets/AMFI/"+str(id)+".json?start_date=2013-08-30"
req = urllib2.Request(url)
response = urllib2.urlopen(req)
print response.read()
for id in codes:
url = "https://www.quandl.com//api/v3/datasets/AMFI/"+str(id)+".json?start_date=2013-08-30"
response = requests.get(url,verify=False)
print response.text
To disable the warning about the SSL certificate, use the following code before making the request using requests:
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
I want to use the Python 3 module urllib to access an Elasticsearch database at localhost:9200. My script gets a valid request (generated by Kibana) piped to STDIN in JSON format.
Here is what I did:
import json
import sys
import urllib.parse
import urllib.request
er = json.load(sys.stdin)
data = urllib.parse.urlencode(er)
data = data.encode('ascii')
uri = urllib.request.Request('http://localhost:9200/_search', data)
with urllib.request.urlopen(uri) as repsonse:
response.read()
(I understand that my repsonse.read() doesn't make much sense by itself but I just wanted to keep it simple.)
When I execute the script, I get an
HTTP Error 400: Bad request
I am very sure that the JSON data I'm piping to the script is correct, since I had it printed and fed it via curl to Elasticsearch, and got back the documents I expected to get back.
Any ideas where I went wrong? Am I using urllib correctly? Do I maybe mess up the JSON data in the urlencode line? Am I querying Elasticsearch correctly?
Thanks for your help.
With requests you can do one of two things
1) Either you create the string representation of the json object yourself and send it off like so:
payload = {'param': 'value'}
response = requests.post(url, data=json.dumps(payload))
2) Or you have requests do it for you like so:
payload = {'param': 'value'}
response = requests.post(url, json = payload)
So depending on what actually comes out of the sys.stdin call (probably - as Kibana would be sending that if the target was ElasticSearch - a string representation of a json object == equivalent of doing json.dumps on a dictionary), but you might have to adjust a bit depending on the output of sys.stdin.
My guess is that your code could work by just doing so:
import sys
import requests
payload = sys.stdin
response = requests.post('http://localhost:9200/_search', data=payload)
And if you then want to do some work with it in Python, requests has a built in support for this too. You just call this:
json_response = response.json()
Hope this helps you on the right track. For further reading om json.dumps/loads - this answer has some good stuff on it.
For anyone who doesn't want to use requests (for example if you're using IronPython where its not supported):
import urllib2
import json
req = urllib2.Request(url, json.dumps(data), headers={'Content-Type': 'application/json'})
response = urllib2.urlopen(req)
Where 'url' can be something like this (example below is search in index):
http://<elasticsearch-ip>:9200/<index-name>/_search/
When I call the socialcast api from python 2.7 using requests, I get a response "reason" but I don't see that text in my actual API. Here's my code:
import requests
parameters = {"username" = "myUsername", "password" = "myPassword"}
response = requests.get("https://hub.sas.com/api/groups/808/messages.json", parameters)
response.json()
The beginning of the JSON that I'm passing through is this:
{"messages":[{"id":126433,"user":{"id":4468,"name":
So I would expect something else to come back, but what it returns is:
{u'reason': u''}
Is this an error or is there something I'm not understanding?
I solved my problem by using this code:
import requests
from requests.auth import HTTPBasicAuth
r = requests.get('https://hub.sas.com/api/groups/808/messages.json', auth=HTTPBasicAuth('username', 'password'))
data = r.json()
for message in data['messages']:
print(message['user']['name'])
I'm not sure that the from requests.auth import HTTPBasicAuth or the data = r.json() were necessary but it ended up working for me so I left them in there.
I am learning Python and am working with the API for MediaFire and trying to just automate some simple processes. But I am running into a bit of confusion after I pass the POST to get the Session Token. The problem is that I am not sure how to extract the token from the response. Here is the call to get the session token:
import hashlib
import time
import urllib.request
token = hashlib.sha1()
token.update("calvinrock0406#gmail.comheaven3610255k592k2di4u9uok3e8u9pkfepjfoc809kfutv5z".encode('utf-8'))
token_dig = token.hexdigest()
with urllib.request.urlopen("https://www.mediafire.com/api/user/get_session_token.php?application_id=36102&signature=" + token_dig + "&email=calvinrock0406#gmail.com&password=heaven&token_version=2&response_format=json") as response:
html = response.read()
And here is the responce I get back from the call:
b'{"response":{"action":"user\\/get_session_token","session_token":"618679cd5046c48fface93dee366a1a07eacfefc5bce88173d5118bb3f128f539602dc54f6d667825a376bc2f86b41a5b1cbe178cd45dfcb4ddfc8e9652f7c529db233181655ec42","secret_key":"774329384","time":"1454700043.4884","ekey":"a84adbba31f9ae8ef717486616a97c63","pkey":"b4dfdc307d","result":"Success","current_api_version":"1.5"}}'
Any help for the noob would be greatly appreciated
I would recommend using requests instead of urllib, and use json() to work with the response:
import json
import hashlib
import requests
token = hashlib.sha1()
token.update("calvinrock0406#gmail.comheaven3610255k592k2di4u9uok3e8u9pkfepjfoc809kfutv5z".encode('utf-8'))
token_dig = token.hexdigest()
response = requests.get("https://www.mediafire.com/api/user/get_session_token.php?application_id=36102&signature=" + token_dig + "&email=calvinrock0406#gmail.com&password=heaven&token_version=2&response_format=json")
json_response = response.json()
print json_response["response"]["session_token"]