my question is what is really happens when you hit the submit button on a html servey like this one?
<INPUT TYPE="radio" NAME="bev" VALUE="no" CHECKED>No beverage<BR>
<INPUT TYPE="radio" NAME="bev" VALUE="tea">Tea<BR>
<INPUT TYPE="radio" NAME="bev" VALUE="cof">Coffee<BR>
<INPUT TYPE="radio" NAME="bev" VALUE="lem">Lemonade<BR>
To be more specific, I mean how does the browser sending the data of my choie to the server, because I want to make a Python code that will vote for me in a HTML survey like this
If the form method attribute is post(which I think is) , then the browser sends a post request.If you're using requests library, this is the code
data = {'bev': 'tea'}
#Define a dict with parameter with keys as name attribute values and value as the content you want to send
r = requests.get("http://awebsite.com/", params=data)
print r.content
Requests docs POST requests
And if you aren't using requests, then God help you write the code.
Related
I have a python script that gets 5 inputs, does some work, and then prints output for 10 seconds until it finishes. I would like that input to be taken from a web app, sent to the server, executed in python and then the result, sent back to the web app to be shown to the client.
My question is this done? I was thinking in having a HTML+CSS+VUE front end, but I don't know how to link the inputs from the html, to the python script and then back to the html async ( because I want the page to show the real time results ). What backend should I use and how to link those inputs to and results to the web page?
Thank you!
You can use a basic API.
You will create a form like this:
<form method="POST action="/your/path">
<input type="text" name="input1">
<input type="text" name="input2">
<input type="text" name="input3">
<input type="text" name="input4">
<input type="text" name="input5">
<button type="submit" value="Submit">
</form>
I don't know which framework you use but I suppose that you are using Flask:
#app.route("/your/path", methods=["POST"])
def executeTheInfos():
#Execute your code and render your HTML page to return
return yourRenderedPage
When you submit the data to HTML form, it will POST the info to /your/path in JSON format like {"input1":"value1", "input2":"value2"...} and at the back-end you will render the response page and return it to user.
I'm looping through zip codes and retrieving information from this site http://www.airnow.gov/index.cfm?action=school_flag_program.sfp_createwidget
Here's the form and input elements:
<form name="groovyform">
<input type="text" name="Title" id="Title" size="20" maxlength="20" />
<input type="text" name="Zipcode" id="Zipcode" size="10" maxlength="10" />
My question is how do I make a post request if there are no attributes in the form element (such as action or method)?
My code (I've tried request.get with the params argument, and request.post with the data argument):
url = 'http://www.airnow.gov/index.cfm?action=school_flag_program.sfp_createwidget'
data_to_send = {'zipcode ':'37217',
'Title': 'ph'}
response = requests.get(url, params=data_to_send)
contents = response.text
print contents
just returns the HTML of the url but I want the HTML of the page I get when I post the data. In other words, I don't think request.get is submitting my data and I think it has something to do with there not being an action or method attribute.
Enlighten me!
Thanks!
That form isn't intended to be submitted anywhere. It's just there for the benefit of the Copy button:
<input type="button" value="Copy" onclick="copy(document.groovyform.simba.value)" />
There are also a number of references to document.groovyform in the buildCall Javascript function, which is run when you click on Build your widget.
This is an old style of Javascript programming. These days, most would assign IDs to these elements, and use document.getElementById() to access them, so there would be no need to wrap them in a form. But before that approach was developed, the way to access DOM elements depended on the fact that forms are automatically added as properties of document, and input elements are properties of the containing form.
Reading Comprehension, I could learn it.
Ok, so like Barmar stated, the <form> I posted isn't supposed to be submitted. The form I was supposed to be filling out (top of the page) contained the following:
<form name="frmZipSearch" method="get" style="width:178px; float:left;">
Zip Code:
<input name="zipcode" type="text" size="5" maxlength="5" height="20">
Now my code works.
url = 'http://www.airnow.gov/index.cfm?action=airnow.local_city&zipcode=37217&submit=Go'
data_to_send = {'zipcode':'37217'}
response = requests.get(url, data=data_to_send)
contents = response.text
print contents
Thanks, Barmar, for directing me to the right path.
I am trying to build a webscraper to extract my stats data from MWO Mercs. To do so it is necessary to login to the page and then go through the 6 different stats pages to get the data (this will go into a data base later but that is not my question).
The login form is given below (from https://mwomercs.com/login?return=/profile/stats?type=mech)- from what I see there are two fields that need data EMAIL and PASSWORD and need to be posted. It should then open http://mwomercs.com/profile/stats?type=mech . After that I need have a session to cycle through the various stats pages.
I have tried using urllib, mechanize and requests but I have been totally unable to find the right answer - I would prefer to use requests.
I do realise that similar questions have been asked in stackoverflow but I have searched for a very long time with no success.
Thank you for any help that could be provided
<div id="stubPage">
<div class="container">
<h1 id="stubPageTitle">LOGIN</h1>
<div id="loginForm">
<form action="/do/login" method="post">
<legend>MechWarrior Online REGISTER</legend>
<label>Email Address:</label>
<div class="input-prepend"><span class="add-on textColorBlack textPlain">#</span><input id="email" name="email" class="span4" size="16" type="text" placeholder="user#example.org"></div>
<label>Password:</label>
<div class="input-prepend"><span class="add-on"><span class="icon-lock"></span></span><input id="password" name="password" class="span4" size="16" type="password"></div>
<br>
<button type="submit" class="btn btn-large btn-block btn-primary">LOGIN</button>
<br>
<span class="pull-right">[ Forgot Your Password? ]</span>
<br>
<input type="hidden" name="return" value="/profile/stats?type=mech">
</form>
</div>
</div>
</div>
The Requests documentation is very simple and easy to follow when it comes to submitting form data. Please give this a read-through: More Complicated POST requests
Logins usually come down to saving the cookie and sending it with future requests.
After you POST to the login page with requests.post(), use the request object to retieve the cookies. This is one way to do it:
post_headers = {'content-type': 'application/x-www-form-urlencoded'}
payload = {'username':username, 'password':password}
login_request = requests.post(login_url, data=payload, headers=post_headers)
cookie_dict = login_request.cookies.get_dict()
stats_reqest = requests.get(stats_url, cookies=cookie_dict)
If you still have problems, check the return code from the request with login_request.status_code or the page content for an error with login_request.text
Edit:
Some sites will redirect you several times when you make a request. Make sure to check the request.history object to see what happened and why you got bounced out. For example, I get redirects like this all of the time:
>>> some_request.history
(<Response [302]>, <Response [302]>)
Each item in the history tuple is another request. You can inspect them like normal requests objects, such as request.history[0].url and you can disable the redirects by putting allow_redirects=False in your request parameters:
login_request = requests.post(login_url, data=payload, headers=post_headers, allow_redirects=False)
In some cases, I've had to disallow redirects and add new cookies before progressing to the proper page. Try using something like this to keep your existing cookies and add the new cookies to it:
cookie_dict = dict(cookie_dict.items() + new_request.cookies.get_dict().items())
Doing this after each request will keep your cookies up-to-date for your next request, similar to how your browser would.
I am scraping a page for some data, however I need to insert text into a text box, submit the form and scrape the result page. I looked at the page source, but I'm not sure how to activate the button or pass down the argument for it.
Website is http://archive.org/web/web.php
Trying to look at some historicals, and no idea what to use for this. Open to any solution
First you should know that click on that button usually does a POST to some urls, passes the data in that form, here is:
<form id="wwmform" name="wwmform" method="get" action="http://web.archive.org/form-submit.jsp" onsubmit="document.location.href='http://web.archive.org/web/*/'+document.getElementById('wwmurl').value;return false;" style="display:inline;">
<input id="wwmurl" type="text" name="url" size="50" value="http://">
<button type="submit" name="type" value="urlquery" class="roundbox5">Take Me Back</button>
</form>
you see the action attribute? That's where the data goes to.
So in python, you may need urllib and urllib2 to encode the data and post it to the target url and then fetch the outcome.
ps: watch out the onsubmit
i use this code in python to post something to a form in a web page.
but i dont just want to send some text as an input textbox, i also want to decide if a checkbox in the form is checked or not.
what value do i have to give to the 'checkbox' parameter in the "web_form"?
web_form = [('textbox', text),('checkbox', ?????)]
form_data = urllib.urlencode(web_form)
o = urllib2.build_opener(url)
res = o.open(url, form_data).read()
this is the html of the form:
<form action="?" method="POST" enctype="multipart/form-data">
<textarea name="textbox"></textarea> Checkbox <input type='checkbox' name='cb' > <input type="submit" value="submit" /></div>
</form>
You question is pretty broad since you did not mention with web framework you are using.
Application code could check either for the existence of the checkbox key in the request or check for a particular value...unless you provide further informations: try setting it to something like '1' and see what's happening.