{"id":2433,"date":"2018-11-07T17:11:21","date_gmt":"2018-11-07T22:11:21","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=2433"},"modified":"2018-11-07T17:11:21","modified_gmt":"2018-11-07T22:11:21","slug":"javascript-how-to-call-a-python-script-and-get-the-results-via-jquery","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/11\/07\/javascript-how-to-call-a-python-script-and-get-the-results-via-jquery\/","title":{"rendered":"JavaScript: How to call a Python script and get the results via jQuery"},"content":{"rendered":"<p>Let&#8217;s say you have a situation where you need to read JSON data from an API and you don&#8217;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 &#8220;scrape&#8221; the output from the page, rather than connecting to the API directly. JavaScript is not very good at scraping web data, but Python is!<\/p>\n<p>Example JSON data on a page:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2504 \" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/11\/JSON-data-on-page.png\" alt=\"\" width=\"693\" height=\"240\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/11\/JSON-data-on-page.png 989w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/11\/JSON-data-on-page-300x104.png 300w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/11\/JSON-data-on-page-768x266.png 768w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/11\/JSON-data-on-page-676x234.png 676w\" sizes=\"auto, (max-width: 693px) 100vw, 693px\" \/><\/p>\n<p>Python can get this data with two lines of code, using the &#8220;requests&#8221; module. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">jsonData = requests.get(r'https:\/\/api.stagingjobshq.com\/madgexWidgetJobs.php?callback=displayJobs&amp;city=Moorhead&amp;state=Minnesota', verify=False).text.strip()\r\nreturn jsonData<\/pre>\n<p>JavaScript can use this data returned from Python by simply calling the Python script in a jQuery AJAX get(). For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\/\/ The name of the Python script that returns the scraped data is scrapeJSON.py\r\nlet scrapeJSON = 'http:\/\/bluegalaxy.info\/cgi\/scrapeJSON.py'\r\n\r\n$.get(scrapeJSON, function(data) {\r\n   \/\/ Get JSON data from Python script\r\n   if (data){\r\n      console.log(\"Data returned:\", data)\r\n   }\r\n   jobDataJSON = JSON.parse(data)\r\n})<\/pre>\n<p>Notice the syntax for the jQuery AJAX call is &#8220;$.get(nameOfOutsideScript, callback function)&#8221;<\/p>\n<p>&nbsp;<\/p>\n<p>For a real working example of this, see my json-jobs-widget code on github:<br \/>\n<a href=\"https:\/\/github.com\/chris-relaxing\/json-jobs-widget\">https:\/\/github.com\/chris-relaxing\/json-jobs-widget<\/a><\/p>\n<p>For more information about AJAX get(), see:<br \/>\n<a href=\"https:\/\/api.jquery.com\/jQuery.get\/\">https:\/\/api.jquery.com\/jQuery.get\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s say you have a situation where you need to read JSON data from an API and you don&#8217;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 &#8220;scrape&#8221; &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/11\/07\/javascript-how-to-call-a-python-script-and-get-the-results-via-jquery\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">JavaScript: How to call a Python script and get the results via jQuery<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44,128],"tags":[65,45,123,4],"class_list":["post-2433","post","type-post","status-publish","format-standard","hentry","category-javascript-language","category-jquery","tag-ajax","tag-javascript","tag-jquery","tag-python"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=2433"}],"version-history":[{"count":8,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2433\/revisions"}],"predecessor-version":[{"id":2511,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2433\/revisions\/2511"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=2433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=2433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=2433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}