WEIRD "json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)"
The problem is between the library you are using and instagram: the latter is not sending you JSON where you expect it.
If you have a look at the source for media_info2
it looks like this:
def media_info2(self, short_code):
"""
Alternative method to get media info. This method works for carousel media.
:param short_code: A media's shortcode
:param kwargs:
:return:
"""
headers = {
'User-Agent': self.user_agent,
'Accept': '*/*',
'Accept-Language': 'en-US',
'Accept-Encoding': 'gzip, deflate',
'Connection': 'close',
'Referer': 'https://www.instagram.com',
'x-requested-with': 'XMLHttpRequest',
}
info = self._make_request(
'https://www.instagram.com/p/{0!s}/'.format(short_code),
query={'__a': '1', '__b': '1'},
headers=headers)
...
Following your traceback, the error is in make_request
. So we go and look at the source and we see that the important lines are:
self.logger.debug('REQ DATA: {0!s}'.format(data))
res = self.opener.open(req, data=data, timeout=self.timeout)
...
if return_response:
return res
response_content = self._read_response(res)
self.logger.debug('RES BODY: {0!s}'.format(response_content))
return json.loads(response_content)
We now have everything we would need to do a bit of manual debugging, since the program works like this:
- set headers and pass them and url to make_request
- get data from url
- parse date as json
- return data
We could put together our own call to opener
, or even just run curl
on the url passed to _make_request
, or temporarily modify the code to print some stuff and be a bit more verbose. But in this case we have an even easier option: the code will already debug itself for us, we just have to tell it to be verbose:
import logging
logging.basic_config(level=logging.DEBUG)
Put that at the top of your failing script and run it again, and it should spew out lots of data telling you exactly what it's doing. Since it prints the entire return body, you should get whatever it's returning, and be able to figure out what's going on.
And remember.... use the source, Luke.
Alright, that was awful, but the only way to know what code is doing is to read the source. The great thing about python is its readability. Compare this to trying to figure out some c++ library by reading it...
JSONDecodeError: Expecting value: line 1 column 1 (char 0, json.loads
You didn't show link to API documentation but I assume that you have to send data as JSON
so you have to use json=payload
instead of data=payload
And when you use json=
then you don't have to add header "application/json"
because it will add it automatically.
payload = {
"run": 1,
"response" : "https://sampleserver",
}
r = requests.post("http://myserver", json=payload)
data=
sends it as form
data like run=1&response=http://myserver
BTW:
Next time when you get JSONDecodeError:
then first check what you get request.body
before you try to convert it to from JSON
because error means that data is not correct JSON
- and you should see data to see if you get JSON
data, form
data (run=1&response=..
) or some HTML
with warning or explanation.
Related Topics
Python Selenium - Element Is Not Currently Interactable and May Not Be Manipulated
Find Specific Words in Text File and Print the Line Using Python
How to Plot Pandas Dataframe With Date (Year/Month)
Check Json Data Is None in Python
Splitting a Phone Number into a List of Digits: Python
Printing Lists in Python Without Spaces
How to Remove an Item from a List in Python If That Item Contains a Word
How to Print Float to N Decimal Places Including Trailing 0S
How to Iterate Through a List of Dictionaries in Jinja Template
How to Check If Keras Is Using Gpu Version of Tensorflow
Python: How to Print Separate Lines from a List
How to Limit the User Input to Only Integers in Python
Pandas: Subtracting Two Date Columns and the Result Being an Integer
How to Display Last 2 Digits from a Number in Python
How to Constantly Run Python Script in the Background on Windows
How to Find Duration Between Two Time Difference in Python Dataframe
Efficient Way to Unnest (Explode) Multiple List Columns in a Pandas Dataframe