I am new to python, and I am currently repeating all the solved examples from the book "python for data analysis"
in one example, I need to access the APIs from twitter search. Here is the code:
import requests
url='https://twitter.com/search?q=python%20pandas&src=typd'
resp=requests.get(url)
everything works okay up to here. problems comes when I use the json
import json
data=json.loads(resp.text)
then I received the error message ValueError: No JSON object could be decoded
I tried to switch to another url instead of the twitter, but I still receive the same error message.
does anyone have some ideas? thanks
You need a response with JSON content. To search Twitter this requires use of api.twitter.com, but you need to get an OAuth key. It's more complicated than the 5 lines of code you have. See http://nbviewer.ipython.org/github/chdoig/Mining-the-Social-Web-2nd-Edition/blob/master/ipynb/Chapter%201%20-%20Mining%20Twitter.ipynb
Related
The jist of my problem is that there is an open API that contains road info that I'm trying to pull very specific information from. The JSON request I'm trying to make looks like:
data1 = """
<REQUEST>
<LOGIN authenticationkey=""/>
<QUERY objecttype="RoadData" schemaversion="1.0">
<INCLUDE>SpeedLimit</INCLUDE>
<INCLUDE>RoadMainNumber</INCLUDE>
<INCLUDE>RoadSubNumber</INCLUDE>
</QUERY>
<QUERY objecttype="RoadGeometry" schemaversion="1.0">
<INCLUDE>Geometry.WGS843D</INCLUDE>
<INCLUDE>RoadMainNumber</INCLUDE>
<INCLUDE>RoadSubNumber</INCLUDE>
</QUERY>
</REQUEST>
"""
and I'm simply sending a request to the correct URL with this as my json code, i.e:
respones = requests.post(url, data=data1) but I'm only getting response 400 which leads me to believe my JSON request is wrong, however they supply an in-house console for testing your requests directly on their website and there it's working. Am I simply stupid here and I'm sending it incorrectly?
Tried looking at similar problems on stackoverflow and online but every single issue uses a dictionary to map their requests but that's not possible here? At least I don't know how to get the keyword in a dictionary as a condition since the sice of the dataset requires this.
I'm new to Python and API's. I'm trying to work on a project and need some help.
I just want to request some information from Blogger to get back blog post details (title, body, etc) and return it.
I'm wanting to use these: https://developers.google.com/resources/api-libraries/documentation/blogger/v3/python/latest/blogger_v3.blogs.html
I can use requests.get(url/key) and I get a server status[200], but everytime I try to find a way to use one of the requests from the link I get keyword errors.
For example: "TypeError: request() got an unexpected keyword argument 'blogId'"
My code is requests.get('url/key' blogId='BLOG ID HERE', x__xgafv=None, maxPosts=None, view=None)
I don't feel comfortable posting my exact code, since it has my API in it.
What am I doing wrong?
request.get() method doesn't have blogID parameter. For more info use this link
I am not sure, but oyu can use params like that:
page = get('https://google.com', params={'blogId': '12345'})
It's better to look up information in the docs: https://requests.readthedocs.io/en/master/
I found my solution after digging a bit.
I was using get requests on the API to get a response, which is how it should be used.
Yet, I was also trying to use a method of returning data that also used the get keyword, but was meant for use with the Google API Library.
I had to install the Google API Client library and import build, and requests so that I could use the API methods instead. This allows me to return results that are far more specific (hence the keyword arguments), and can be programmed to do a number of things.
Thanks everyone!
First thing, I'm not very used to HTTP request, so bear with me if I make some stupid mistakes or assumption that are completely wrong.
I'm trying to send an image using a POST request using Flask, the code that I'm using is the one that can be found on this link:
https://gist.github.com/kylehounslow/767fb72fde2ebdd010a0bf4242371594
Basically it encodes the image using cv2, than sends it over in the POST request, I could get that to work but I wanted to send some more information, therefore I rewrote the request as:
payload = {'img':img_encoded.tostring(), 'name':'foo'}
response = requests.post(test_url, data=json.dumps(payload), headers=headers)
this gives me an error, more specifically:
TypeError: Object of type 'bytes' is not JSON serializable
This is due to the fact that I'm encoding the image, so I tried to just send the dictionary, without dumping it to a JSON, the request goes through, but now I dont know how to decode the data field in the request.
If I try to access to request.data I get this:
b'img=very_long_first_field_of_binary_info&name=foo'
How do I revert that to a dictionary?
Since I can't use json, I don't know what to do.
Thanks in advance for your help,
Mattia
I read the documentation of Facebook graph and trying to get the results from the oauth endpoint. The url I'm requesting is:
https://graph.facebook.com/oauth/access_token?client_id=<MY_CLIENT_ID>&client_secret=<MY_CLIENT_SECRET>&redirect_uri=<MY_REDIRECT_URL>&code=<CODE_RECEIVED_FROM_FB>
but I get a response like this:
access_token=CAAICzAsj0CgBAAQ7Go1k4NuG89mabLg6ZCpGoBoZCelRsLdQlcq1yvUbFZAKZBskTwMVmkTeZBZC9Thd4keYq0d3er3tGNTZCzR3TMnfEZABVfOpBqkOZBvZANZCny3XrDPDv7bTZB4ZAYjcPfvvMA4gRTcPrRJhht6XjIehV5gLtXW4YRvWaL4KuhYWB&expires=5169000
This happens from python requests library and also when I use curl from terminal or open in browser. Looks like it's returning response in form of get parameters. Why is it not returning JSON as documented? What's going on?
Ahh, my mistake, this is related to FB Graph API version. When I added the version as defined in the api, it worked fine.
https://graph.facebook.com/v2.3/oauth/access_token...
Reference:
https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow
I have an URL in the form of
http://site.com/source.json?s=
And I wish to use Python to create a class that will allow me to parse in my "s" query, send it to that site, and extract out the JSON results.
I've tried importing json/setting up the class, but nothing ever really works and I'm trying to learn good practices at the same time. Can anyone help me out?
Ideally, you should (especially when starting out), use the requests library. This would enable your code to be:
import requests
r = requests.get('http://site.com/source.json', params={'s': 'somevalue/or other here'})
json_result = r.json()
This automatically escapes the parameters, and automatically converts your JSON result into a Python dict....