JSONDecodeError: Expecting value: line 1 column 1 (char 0) - Not working on Python 3.8

625 Views Asked by At

Hey I've just started to use Python 3.8 and I get bug for the last line. It works perfectly on python 3.6 how cam I make it work on Python 3.6?

in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

import requests
import urllib.request, urllib.parse
from bs4 import BeautifulSoup
import json
user = "travel.like.this"
url = 'https://www.instagram.com/' + user
req = urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36')
html = urllib.request.urlopen(req).read()
response = BeautifulSoup(html, 'html.parser')
jsonObject = response.select("body > script:nth-of-type(1)")[0].text.replace('window._sharedData =','').replace(';','')
data = json.loads(jsonObject)
1

There are 1 best solutions below

0
On

maybe in python 3.8, the requests lib is not handling the empty response and its coded to raise an error when its empty.

this error: JSONDecodeError: Expecting value: line 1 column 1 (char 0)

is because the json response is empty, its a void string. ----> b''

(i didnt see the json, but i assume its empty and thats why its raising error)

from my experience:

import requests

response = requests.get("https://my_static_website.com/")
print(response.status_code) # 200
print(response.json())

out

200

JSONDecodeError: Expecting value: line 1 column 1 (char 0)