Run python script by clicking html button

Asked

Viewed 650 times

1

I need to feed a page html that will load and display the content dynamically with ajax/fetch. The problem is that I need to take this data from other websites that also upload this content through ajax, so I can’t make the requisitions for these pages with ajax because they returned me only the html basics without the information I need.

The solution I found was to do the web scraping with python(selenium) and pass the data to my page, but I can’t find a way to execute my script python so I don’t have to use django(and change my entire project) or write files that are constantly read by python and manage access. I also saw some things related to CGI, but it seems to me not to be what I’m looking for because it shows the elements printing the tags, but I need to load dynamically, besides preferring to receive this data as json, xml ...

Is there any way to run a script python similar a script php which is called in the tag <form>?

OBS: alternative solutions can help but at first wanted something with these tools or this way.

  • There is no need to migrate everything to Django. You can use a lightweight framework like Flask.

1 answer

0

I do not know python, but to call a page in the backend without necessarily accessing it visibly, would be making an asynchronous request, through AJAX.

I didn’t quite understand your need, but let’s assume that you want a dynamic form, according to some feedback, rule, etc... in the backend, you can get this information in a very easy way by accessing this page with ajax:

var xhr = new XMLHttpRequest(); 

xhr.open("GET", 'sua_pagina'); // envia a requisição a url
xhr.send(null);

xhr.onreadystatechange = function() { // chama uma função quando o documento for carregado 
    if(xhr.readyState === 4) { // verifica se a resposta foi de sucesso
        document.querySelector('form').innerHTML = xhr.responseText // exibe a resposta no seu formulario 
    }
}

And you can have pure html coming from your backend, which will work and display in your form quietly.

It is worth remembering that this form of making ajax requisitions is very old, nowadays there are easier and modern means, if you have the freedom, take a look:

  1. Fetch

  2. jQuery

Using jQuery for example, you reduce it all to one line if you simply want to load the data:

$('form').load('sua_pagina');

I hope I’ve helped!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.