Log in to website behind CloudFlare using Python Requests - python

Trying to post a login to a website but cant seem to get it working.
Here is the returned error:
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>cloudflare-nginx</center>
</body>
Here is form that I'm trying to post the requests to login to:
<form name="loginform" id="loginform" method="post" action="login.php" onsubmit="return formVal();">
<table cellpadding="2" cellspacing="1" border="0" align="center">
<tr valign="top">
<td align="right">Username </td>
<td align="left"><input type="text" name="username" id="username" class="inputtext" autofocus="autofocus" /></td>
</tr>
<tr valign="top">
<td align="right">Password </td>
<td align="left"><input type="password" name="password" id="password" class="inputtext" /></td>
</tr>
<tr valign="top">
<td colspan="2" align="right"><input type="checkbox" name="keeplogged" value="1" />Keep me logged in</td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" name="login" value="Log In!" class="submit" /></td>
</tr>
</table>
<br /><br />
Lost your password? Recover it here!
Pls help!

Try getting the page first and then post, like this -
with cfscrape.create_scraper() as s:
p = s.get(login_url)
p = s.post(login_url, data=payload)
print p.content

Related

I am unable to get round toggle in my django template

I have a table which contains for loop and if tag using jinja this table shows some data but in last column I want round toggle button but I only get a checkbox, I am unable to find the error please help me.
<tbody>
{%for student in students%}
{%if user.staff.class_coordinator_of == student.division and user.staff.teacher_of_year == student.year%}
<tr>
<td style="color:white;">{{student.user.first_name}}</td>
<td style="color:white;">{{student.user.last_name}}</td>
<td style="color:white;">{{student.year}}</td>
<td style="color:white;">{{student.division}}</td>
<td style="color:white;">{{student.batch}}</td>
<td>
<label class="switch ">
<input type="checkbox" id="" value="" checked>
<span class="slider round"></span>
</label>
</td>
</tr>
{% endif %}
{%endfor%}
</tbody>
OUTPUT
Output Image
You should use radio button instead of checkbox so, it should be type="radio".
<input type="radio" id="" value="" checked>

How to count rows from a html table web2py

actually i have a table in the html and i want to know how i can get like tabe.rows.count on python in web2py
here is my code:
<table id="TablaMateriales" name="TablaMateriales" class=" table table-responsive order-list">
<thead>
<tr class="table-light">
<td>Item</td>
<td>Descripcion</td>
<td>Unidad</td>
<td>Cantidad</td>
<td>Proveedor</td>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="number" name="Item0" class="form-control" style="width:45px" value="1" disabled />
</td>
<td>
<input type="text" name="Unidad0" style="width:100px" class="form-control"/>
</td>
<td>
<input type="number" min="0" id="Cantidad0" name="Cantidad0" style="width:75px" value="0" class="form-control" onFocus="this.select()" onsubmit="if(this == ''){$this.val('0');}"/>
</td>
<td>
<input type="text" name="Proveedor0" style="width:250px" class="form-control"/>
</td>
</tr>
</tbody>
</table>
and code behind:
def crearCotizacion():
materialesT = request.vars.get('TablaMateriales')
rowsCount = materialesT.rows.count
return dict(rowsCount = rowsCount)
Thank you!
It seems like you are trying to access a client side element ('TablaMateriales') on the server side. You can only do that if you explicitly pass the element back to the server.
And then you would need to manipulate it using something like http://web2py.com/books/default/chapter/29/05/the-views#Server-side-DOM-and-parsing
You could just count the number of rows using JavaScript from inside the view: JavaScript to get rows count of a HTML table

Python mechanize to submit form data

My form HTML source is below, I am trying to "check one of the checkboxes" and hit the "update" or submit button using mechanize. How would I go about this? Does the variable for linear_entitlements make it not possible?
<form accept-charset="UTF-8" action="/admin/users/3548003/user_linear_entitlements" class="form with-border" id="edit_user_3548003" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="_method" type="hidden" value="put"><input name="authenticity_token" type="hidden" value="samplevalue"></div>
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>GUID</th>
<th>CMS Subpack ID</th>
<th>Last Updated</th>
</tr>
</thead>
<tbody>
<tr id="linear_entitlement_1">
<td>
<div class="control-group">
<label class="checkbox">
<span>SUN Pack</span>
<input class="checkbox" id="user_linear_entitlement_ids_" name="user[linear_entitlement_ids][]" type="checkbox" value="1">
</label>
</div>
</td>
<td> 2a59739c-13ed-11e2-a36b-12313d298802 </td>
<td> 1 </td>
<td> 2014-02-12 21:32:56 UTC <div style="float:right">→</div></td>
</tr>
<tr id="linear_entitlement_7">
<td>
<div class="control-group">
<label class="checkbox">
<span>Tamil Pack - Legacy</span>
<input class="checkbox" id="user_linear_entitlement_ids_" name="user[linear_entitlement_ids][]" type="checkbox" value="7">
</label>
</div>
</td>
<td> 2ab298dc-13ed-11e2-a36b-12313d298802 </td>
<td> 3 </td>
<td> 2015-04-01 23:11:33 UTC <div style="float:right">→</div></td>
</tr>
</tbody>
</table>
<div class="form-actions">
<input class="btn primary input_submit" name="commit" type="submit" value="Update"> <button type="reset" class="btn">Cancel</button>
</div>
</form>
So far I have this, which selects the form:
htmlResponse2 = browser.open(URL + 'admin/users/' + old_user_url + '/edit')
browser.select_form(nr=0)
I don't know if you need to select the form, but if you did so, following should just do the trick:
br.find_control(type="checkbox").items[0].selected=True
if you want to select all checkboxes:
for i in range(0, len(br.find_control(type="checkbox").items)):
br.find_control(type="checkbox").items[i].selected =True
then submit
br.submit()

Signing In and Scraping Webpages with Scrapy

Hoping some one is able to help me with this. I am using Scrapy to login and scrape data. This particular code works for one website. But I have another website that Scrapy cannot log on to because of the following issue:
There are no form elements on the page.
The username and password fields, and the submit button are not in a form element, instead they are in a table element, which makes it a bit confusing. How can I make Scrapy log into the webpage using the table/tr elements....and not the form element?
Any help would be appreciated!
class LoginSpider(BaseSpider):
name = 'project'
allowed_domains = ["domain.com"]
start_urls = ["theloginURL"]
#this function will look for the form element and login with the username and password
def parse(self, response):
return [FormRequest.from_response(response,
formdata={'user_name': ' username123', 'Password': ' psd123'},
formxpath='//*[#name="Form1"]',callback=self.after_login)]
This is the HTML code that is on the log in page, in case you guys need it:
<table height="260px" id="loginMainTable" width="100%" cellspacing="0" cellpadding="0">
<tbody><tr>
<td>
<table align="center" class="blueBorder" cellspacing="0" cellpadding="0">
<tbody><tr class="HeaderFooterHide">
<td id="companyTD" width="100%" colspan="3" style="position:relative;">
<span class="header1 header1pos">
Application and Network Access Portal
</span>
<table width="100%" cellpadding="0" cellspacing="0">
<tbody><tr>
<td width="32px">
<img src="/uniquesig6d7a33b352f4c09846f8a6563bae192b/uniquesig0/InternalSite/images/headertopl.gif" align="absmiddle">
</td>
<td style="background-image: url('/uniquesig6d7a33b352f4c09846f8a6563bae192b/uniquesig0/InternalSite/images/headertopm.gif'); background-repeat: repeat-x">
</td>
<td width="520px" style="background-image: url('/uniquesig6d7a33b352f4c09846f8a6563bae192b/uniquesig0/InternalSite/images/headertopr.gif');">
</td>
</tr>
</tbody></table>
</td>
</tr>
<tr class="HeaderFooterHide">
<td width="100%" colspan="3" style="position:relative;">
<span style="position:absolute;margin-left:20px;">
</span>
<table width="100%" cellpadding="0" cellspacing="0">
<tbody><tr>
<td width="30px">
<img src="/uniquesig6d7a33b352f4c09846f8a6563bae192b/uniquesig0/InternalSite/images/headerbottoml.gif" align="absmiddle">
</td>
<td style="background-image: url('/uniquesig6d7a33b352f4c09846f8a6563bae192b/uniquesig0/InternalSite/images/headerbottomm.gif'); background-repeat: repeat-x">
</td>
<td width="30px" style="background-image: url('/uniquesig6d7a33b352f4c09846f8a6563bae192b/uniquesig0/InternalSite/images/headerbottomr.gif');">
</td>
</tr>
</tbody></table>
</td>
</tr>
<tr>
<td class="contentleft">
</td>
<td align="center" class="internalTD">
<table width="100%" height="100%" cellspacing="0" cellpadding="0" align="center">
<form id="form1" name="form1" autocomplete="off" method="post" action="/uniquesig6d7a33b352f4c09846f8a6563bae192b/uniquesig0/InternalSite/Validate.asp" onsubmit="return(SubmitForm());"></form>
<tbody><tr>
<td valign="top" height="250px" align="center">
<table border="0" cellspacing="0" cellpadding="0" class="content">
<tbody><tr>
<td class="msgText">Log On</td>
</tr>
</tbody></table>
<table border="0" cellspacing="0" cellpadding="0" class="content">
<tbody><tr>
<td class="paramText">Username</td>
<td><input class="paramTextbox" type="text" id="user_name" name="user_name" maxlength="50" size="11"></td>
</tr>
<tr>
<td class="paramText">Password</td>
<td><input class="paramTextbox" type="password" id="password" name="password" maxlength="20" onkeypress="capsDetect(arguments[0]);" size="11"></td>
</tr>
<input type="hidden" id="repository" name="repository" value="ADLDS" size="11">
<tr height="0px">
<td colspan="2" id="capsLockTD" height="0px"> </td>
</tr>
<tr>
<td> </td>
<td class="EzBiz_Text1"><input name="chkUsername" type="CHECKBOX" onclick="saveUserName()" id="chkUsername">Remember my User ID</td>
</tr>
<tr>
<td colspan="2" align="right">
<input border="0" class="button" type="submit" id="submit_button" value="Log On">
</td>
<td></td>
</tr>
</tbody></table>
<div class="EzBiz_Loginbutton" style="float: left;">
<input border="0" class="EzBiz_Button" type="image" id="submit_button" src="/uniquesig6d7a33b352f4c09846f8a6563bae192b/uniquesig0/InternalSite/images/CustomUpdate/login_submit.jpg">
</div>
<div class="EzBiz_PasswordForget">
<img src="/uniquesig6d7a33b352f4c09846f8a6563bae192b/uniquesig0/InternalSite/images/CustomUpdate/arrow.gif">
Did you forget your password?
</div>
</td>
</tr>
<input type="hidden" name="site_name" id="site_name" value="ezbizportal">
<input type="hidden" name="secure" id="secure" value="1">
<input type="hidden" name="resource_id" id="resource_id" value="49203789E2E14C4A92CEC904C24909CE">
<input type="hidden" name="login_type" id="login_type" value="2">
<tr>
<td>
<table cellspacing="0" cellpadding="0" class="content" width="100%">
<tbody><tr>
<td id="openerExistsTD" class="notification">
For security purposes, when you finish working with this site do one of the following:<li>Click the Logoff button to log off from the site.</li><li>Close all browser windows (including applications that are open in other windows).</li>
</td>
</tr>
<!-- Windows XP Service Pack 2 / 2003 / Vista Message - Start -->
<tr>
<td class="notification">
This site is intended for authorized users only.<br>
If you experience access problems contact the site administrator.
</td>
</tr>
</tbody></table>
</td>
</tr>
</tbody></table>
</td>
<td class="contentright">
</td>
</tr>
<tr class="HeaderFooterHide">
<td width="100%" colspan="3" style="position:relative;">
<span class="bottomText bottomTextPos">
© 2010 Microsoft Corporation. All rights reserved. Terms and Conditions.
</span>
<table width="100%" cellpadding="0" cellspacing="0">
<tbody><tr>
<td width="47px">
<img src="/uniquesig6d7a33b352f4c09846f8a6563bae192b/uniquesig0/InternalSite/images/footerbgl.gif" align="absmiddle">
</td>
<td style="background-image: url('/uniquesig6d7a33b352f4c09846f8a6563bae192b/uniquesig0/InternalSite/images/footerbgm.gif'); background-repeat: repeat-x">
</td>
<td width="47px" style="background-image: url('/uniquesig6d7a33b352f4c09846f8a6563bae192b/uniquesig0/InternalSite/images/footerbgr.gif');">
</td>
</tr>
</tbody></table>
</td>
</tr>
</tbody></table>
</td>
</tr>
</tbody></table>
A generic login module would be more useful. Try scrapy loginform library:
Install
pip install -i https://pypi.binstar.org/pypi/simple loginform
Code
from loginform import fill_login_form
import requests
url = "https://example.com/login"
r = requests.get(url)
result=fill_login_form(url, text, 'username', 'password')
About
loginform is a library for filling HTML login forms given the login
url, username & password. Which form and fields to fill are inferred
automatically.

Google App Engine Python HTML Table

I am trying to create a table within a Google App Engine Application where the background color in a table changes periodically based on input. Does anyone know how to accomplish this?
Here is my code:
self.response.out.write("""
<img src="/images/resistor.png" width = "150">
<table border = "1">
<tr height="150" >
<td bgcolor="%s" width="35"> </td> <td bgcolor="%s" width="35"> </td> <td bgcolor="%s" width="35"> </td> <td bgcolor="%s" width="35"> </td> %(Red,Blue,Black,Green)
</tr>
</table>
<form action="/sign" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form> """)
self.response.out.write('</pre></body></html>')
For example the Red,Green... Colors in %( ) will be variables that will change so at one point they all may be Red or Blue and Yellow.
That type of string-formatting is deprecated. Please use the .format() method in new code. Example:
self.response.out.write("""
<img src="/images/resistor.png" width = "150">
<table border = "1">
<tr height="150" >
<td bgcolor="{}" width="35"> </td>
<td bgcolor="{}" width="35"> </td>
<td bgcolor="{}" width="35"> </td>
<td bgcolor="{}" width="35"> </td>
</tr>
</table>
<form action="/sign" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form> """.format( ('Red','Blue','Black','Green') ))
self.response.out.write('</pre></body></html>')
And for anything beyond the basic have a look at using templates. Examples of templating systems are Jinja2 and Django Templates.

Categories