1
votes

I am getting error Expecting value: line 1 column 1 (char 0) on the line response.json().

WHAT I TRIED

As opposed to other answers I do not get an error at json.loads() but at response.json().I have also tried tweaking response data body but no help.Here is the code.

import requests
import json
from bs4 import BeautifulSoup as Soup


def get_links(search_name):
   search_name = search_name.replace(' ', '+')
   response = requests.get("http://www.google.com/search", 
          params={'q': search_name, 'first': 0}, 
          headers={'User-Agent': user_agent})
   dataform = response.json()
   page = json.loads(dataform)
   new_soup = Soup(page[1][1], 'lxml')
   images = new_soup.find_all('img')
   links = [image['src'] for image in images]
   return links

traceback:

<ipython-input-27-8d351939888e> in get_links(search_name)
 17               params={'q': search_name, 'first': 0},
 18               headers={'User-Agent': user_agent})
 ---> 19     dataform = response.json()
 20     page = json.loads(dataform)
 21     new_soup = Soup(page[1][1], 'lxml')

 /usr/local/lib/python3.6/dist-packages/requests/models.py in json(self, **kwargs)
890                     # used.
891                     pass
--> 892         return complexjson.loads(self.text, **kwargs)
893 
894     @property

/usr/lib/python3.6/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
352             parse_int is None and parse_float is None and
353             parse_constant is None and object_pairs_hook is None and not kw):
--> 354         return _default_decoder.decode(s)
355     if cls is None:
356         cls = JSONDecoder

/usr/lib/python3.6/json/decoder.py in decode(self, s, _w)
337 
338         """
--> 339         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
340         end = _w(s, end).end()
341         if end != len(s):

/usr/lib/python3.6/json/decoder.py in raw_decode(self, s, idx)
355             obj, end = self.scan_once(s, idx)
356         except StopIteration as err:
--> 357             raise JSONDecodeError("Expecting value", s, err.value) from None
358         return obj, end

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

1 Answers

2
votes

The function response.json() attempts to decode whatever JSON that was sent as the response of the request. But obviously, https://www.google.com does not return json; it responds with a html instead.

This is evident from response.text, which shows the returned html as a string.

If you use a url which responds with actual JSON, example https://jsonplaceholder.typicode.com/todos/1, then you will not get any error.