JavaScript: How to call a Python script and get the results via jQuery

Let’s say you have a situation where you need to read JSON data from an API and you don’t have plug-in credentials to access the API directly, but you can see the JSON data that is output from the API on a page. In this case, you can still get the data if you “scrape” the output from the page, rather than connecting to the API directly. JavaScript is not very good at scraping web data, but Python is!

Example JSON data on a page:

Python can get this data with two lines of code, using the “requests” module. For example:

jsonData = requests.get(r'', verify=False).text.strip()
return jsonData

JavaScript can use this data returned from Python by simply calling the Python script in a jQuery AJAX get(). For example:

// The name of the Python script that returns the scraped data is
let scrapeJSON = ''

$.get(scrapeJSON, function(data) {
   // Get JSON data from Python script
   if (data){
      console.log("Data returned:", data)
   jobDataJSON = JSON.parse(data)

Notice the syntax for the jQuery AJAX call is “$.get(nameOfOutsideScript, callback function)”


For a real working example of this, see my json-jobs-widget code on github:

For more information about AJAX get(), see: