NEWStandard createTask and in.php APIs. Change the address and you are connected.View API docs
GATESOLVER

Web scraping

How do I read JSON from a response in Python?

Call .json() on the response object from requests or httpx, after confirming the status code and content type, and you get Python dicts and lists back.

The basic call

With the requests library, response.json() parses the body as JSON and returns ordinary Python objects: objects become dicts, arrays become lists. httpx works the same way. The catch is that .json() raises an error if the body is not JSON, which happens more often than you would think when scraping, so check the response before you parse it.

import requests

reply = requests.get("https://api.example.net/v2/listings?page=1", timeout=15)
reply.raise_for_status()
if "application/json" not in reply.headers.get("Content-Type", ""):
    raise ValueError(f"expected JSON, got {reply.headers.get('Content-Type')}")
payload = reply.json()
for item in payload.get("results", []):
    print(item["id"], item.get("price"))

Why .json() fails

  • An HTML error page. A block page, a login redirect or a challenge page arrives with status 200 or 403 and an HTML body. Parsing raises JSONDecodeError at character 0.
  • An empty body. A 204 or a dropped connection gives nothing to parse.
  • JSONP or a prefix. Some endpoints wrap JSON in a function call, or start with a guard such as )]}'. Strip it before parsing with json.loads.
  • Wrong encoding. Rare with modern APIs, but forcing reply.encoding = "utf-8" fixes odd characters.

Logging the first 200 characters of reply.text on failure usually tells you which one you hit.

JSON hidden inside HTML

Many sites ship their data inside the page as a script block, which is often easier to read than the visible markup. Look for <script type="application/json">, application/ld+json for structured data, or framework blobs such as __NEXT_DATA__.

import json, bs4

soup = bs4.BeautifulSoup(html_text, "html.parser")
blob = soup.find("script", id="__NEXT_DATA__")
state = json.loads(blob.string) if blob else {}
product = state.get("props", {}).get("pageProps", {}).get("product", {})

Chained .get() calls with defaults keep the script running when one level is missing, which is common across page types.

Finding the JSON endpoint in the first place

Open the browser's network panel, filter by Fetch/XHR, and reload or scroll. Requests that return JSON often carry exactly the data the page renders. Calling them directly is faster and steadier than parsing HTML, an approach described under hidden API scraping. Copy the headers the browser sent, because some endpoints check Referer, cookies or a custom header.

When the JSON endpoint is protected

Some JSON endpoints only answer after a CAPTCHA, returning an error object or an HTML challenge until a token or cookie is present. Solve the challenge on the page that guards the endpoint, attach the token or clearance cookie the way the browser does, and repeat the call. A JSON reply from GateSolver itself is also read with .json(); check its errorId before reading solution.

Large and nested responses

For very large files, stream them and use a streaming parser such as ijson instead of loading everything into memory. For deeply nested data, write small helper functions that walk known paths, and flatten only the fields you need before saving. The question on converting JSON to CSV shows how to flatten records cleanly.

Validating what you parsed

A response that parses is not necessarily the response you wanted. APIs return error objects with status 200, change field names between versions, and send null where you expected a number. Check for the keys you depend on right after parsing and fail loudly when they are missing, rather than writing half empty rows. For anything long lived, a small schema check with a library such as pydantic, or a handful of plain assertions, turns silent data rot into an error you see the same day.

Common questions

What is the difference between .json() and json.loads()?

.json() parses the response body for you; json.loads() parses any string you pass, such as a script tag's contents.

Why do I get JSONDecodeError at char 0?

The body is not JSON, usually an HTML error, block or challenge page. Print the start of the text to see it.

Does httpx parse JSON the same way?

Yes. httpx responses also have a .json() method with the same behavior.

How do I keep numbers as decimals for prices?

Pass parse_float=decimal.Decimal to json.loads, which avoids float rounding in money values.

Should I prefer a JSON endpoint over HTML parsing?

When the endpoint is public and stable, yes; it is lighter and less likely to break on layout changes.

Keep reading