Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am a novice python programmer and I am having troubles finding a tool to help me get a form from a javascript. I have written a small script in python and also have a simple interface done in javascript. The user needs to select a few items in the browser and the javascript then returns a sendForm(). I would like to then to recover the form with my python script. I know I could generate an xml file with javascript and tell my python script to wait until its creation and then catch it (with a os.path.exist(..)) but i would like to avoid this. I have seen that libraries such as cgi, mechanize, pyjs,(selenium?) exist to interface python and html,javascript but I can't find which one to use or if there would be another tool that would handle recovering the form easily.

More info: the python script generates an xml which is read by javascript. The user selects items in the javascript (with checkboxes) which are then tagged in the xml by javascript. The javascript then outputs the modified xml in a hidden field and it is this modified xml that I wish to retrieve with my python script after it is created.

Thank you all a lot for your help

share|improve this question
5  
Welcome to Stack Overflow! I'm sorry, but I find it very hard to figure out what you are asking here. It would help if you included some code to show what you have tried, it'll make it much easier for us to help you. Perhaps you could also take a look at whathaveyoutried.com for a great article on how to ask good questions? –  Martijn Pieters Jul 8 '12 at 14:16
1  
I am having trouble understanding whether you are trying to scrape an existing site over which you have no control, or working on a website you actually own. Which is it? Mechanize does sound like what you want. Read the example and see if it suits your needs. If you have problems, come back and post the code you used and the problem (including a traceback) you encountered. –  ChrisP Jul 8 '12 at 14:24
 
@ChrisP: Can mechanize interact with javascript? Although OP's question is not clear enough but I believe he is trying to do somethinh which envolves javascript. –  RanRag Jul 8 '12 at 14:27
 
Sorry for the confusion. I am working on a website I own. The python script creates it when it is launched. What I am having troubles with is for my python script to understand when the user has clicked the submit button on the form and catch the content of it. –  Alexei Bassinski Jul 8 '12 at 14:27
1  
@RanRag: Actually, I don't think mechanize handles JavaScript. So something like Selenium would be more appropriate for scraping. I guess Alexei isn't scraping, however. I've added an answer below. –  ChrisP Jul 9 '12 at 14:16
add comment

2 Answers

Thanks for the clarification in the comments. Since you are building a website, you'll want to use AJAX to call the python web application and return something (probably XML in your case, although I recommend reading about JSON). It sounds to me like you don't have the best solution to your problem yet, but that is another issue. I don't understand why you would go to the trouble of generating xml with python, reading it with JavaScript, updating it with JavaScript, and then reading it again with Python on form submission. For starters, think about what happens if your users disable JavaScript.

Anyways, I suggest reading about Flask and jQuery to get started. Your application might then look something like this in python:

from flask import Flask
app = Flask(__name__)

@app.route('/', methods=['POST', 'GET'])
def foo():
    if request.method == 'GET':
        # Generate the XML
        return myXML
    if request.method == 'POST':
        # Read that hidden field
        xml = request.form['the_hidden_xml']
        # Do what you will
        return render_template('homepage.html')

if __name__ == "__main__":
    app.run()

The JavaScipt probably does this:

function buildTheForm() {
    $.get('/', function(data) {
        // handle the xml in data
        alert('Load was performed.');
    });
}

The form probably does this:

<form action="/" method="post">
share|improve this answer
add comment

Is your system a web application, If so your javascript can post to python back-end using ajax. Then you can encrypt a form to json string and send to back-end, in back en you can parse that string into python variable... Javascript it self does not have access to your local file except you run it local (but it's really limitted)

I suggest you should try a web frame work like Django. It's easy to learn in one day.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.