Home > Enterprise >  Read and Write JSON online
Read and Write JSON online

Time:01-06

I'm trying to learning how to read and write json online storage like npoint.io or JSONBIN.io. But I confuse how to read and write it, i looking from the internet the example code, but always failed when i tried it alone.

import json
from urllib.request import urlopen
url = "just say it abcdefg"
response = urlopen(url)
data_json = json.loads(response.read())

For write and read i know how to do it, but how to import it from internet how to do it?

CodePudding user response:

What you are attempting is an HTTP GET for the url given by the sample websites you said like npoint.io.

When you use urlopen(url) , the HTTP request is sent without proper / required headers most websites expect. For eg, a header like User-Agent. Many websites block such access for security purposes.

You can access the URLs in different ways. Using requests library [Install like pip install requests if you dont have it.]

url = "https://api.npoint.io/6cb9bb8b8fe2856e5977"
response = requests.get(url).json()

Will give you

{'why': ['quick setup', 'easy editing', 'schema validation'], 'what': 'a simple JSON data store'}

And if you want to use the urllib itself, better frame your get request as a formal one like below

import urllib, json
# Mock a browser
user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
headers = {'User-Agent': user_agent, }
request = urllib.request.Request(url, None, headers)
response = urllib.request.urlopen(request)
response = json.loads(response.read())

Gives

{'why': ['quick setup', 'easy editing', 'schema validation'], 'what': 'a simple JSON data store'}

CodePudding user response:

Are you attempting to obtain localStorage cache in the browser? I don't know if that is possible using python if you just access a random API since the localStorage is browser specific. See more here

How could I access localstorage under Python requests

Maybe try using selenium and open a new browser. By using the selenium library, you can open a new browser from within python and you may be able to have more direct control over your browser.

However, using https://randomuser.me/api as an example, your code works and the JSON value is fetched and stored in the data_json variable.

  •  Tags:  
  • Related