Google Trends API is not good json when reading in python

Using HTTP requests in Python, I was able to get the answer, however, neither json nor simplejson modules can unpack it. Both claim input is not good json.

import requests
import json

html_base = u"http://www.google.com/trends/fetchComponent?q="
q = u"asdf,qwerty"
query_type = u"&cid=TIMESERIES_GRAPH_0&export=3"
full_query = html_base + q + query_type

response = requests.get(full_query)
data = json.loads(response.text)

Error:

C:\Anaconda\lib\json\decoder.pyc in raw_decode(self, s, idx)
    382             obj, end = self.scan_once(s, idx)
    383         except StopIteration:
--> 384             raise ValueError("No JSON object could be decoded")
    385         return obj, end

ValueError: No JSON object could be decoded
+4
source share
2 answers

Please excuse my necromancy, but, here is a clean job for those who should stumble in the future.

import ast

nice_dict = ast.literal_eval(response.text.split('setResponse(')[1].rstrip()[:-2].replace('new Date', ''))

ast.literal_eval () takes a string and returns a dict. Everything on the inside clears json to make it returnable. Note that your dates are now tuples.

+2
source

, JSON. URL- , JavaScript. , - JSON .

+2

All Articles