I'm trying to log into a website and then perform some tasks to retrieve some data I need. I've been looking at examples of ways to login but nothing I've tried seems to work for my case. I've heard that the "requests" module is something that I should utilize.
Here is the form section of the login page (https://verification.nws.noaa.gov/services/public/login.aspx):
<form name="PageForm" method="POST" action="/services/public/login.aspx" id="PageForm">
.
.
(a little ways down)
.
.
<p>
<label for="Username">Username:</label>
<br>
<input name="UsernameBox" type="text" id="UsernameBox">
</p>
<p>
<label for="Password">Password:</label>
<br>
</p>
<p>
<input type="submit" name="LoginBtn" value="Login" onclick="javascript: WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("LoginBtn", "", true, "", "", false, false))" language="javascript" id="LoginBtn" class="btn">
</p>
.
.
</form>
This is what I have for my Python code so far and it doesn't seem to login or work:
import requests
# Log into the NWS Performance Management site to get Storm Data.
url = 'https://verification.nws.noaa.gov/services/public/login.aspx?'
values = {'UsernameBox': 'myuser',
'PasswordBox': 'mypass',
'LoginBtn': 'Login'}
session = requests.session()
r = session.post(url, data=values)
# Try opening private webpage when logged in.
r = session.get('https://verification.nws.noaa.gov/stormdat/downloads/csv/index.aspx#top')
Any help would be greatly appreciated. Thanks!
You just missed a few fields in your payload, you can get them from the login page.
<body><form name="PageForm" method="POST" action="/services/public/login.aspx" id="PageForm">
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUIMzc4Mzk0MzlkZF/afEu7JIuhzEtWu2QqdxDm88Un" />
So, your values should like like this
values = {
'__EVENTTARGET': '',
'__EVENTARGUMENT': '',
'__VIEWSTATE' 'Get this value from the login page',
'__VIEWSTATEGENERATOR' 'Get this value from the login page',
'UsernameBox' 'myuser',
'PasswordBox' 'mypass',
'LoginBtn' 'Login'
}
Related
I'm trying to send a PM on a forum that I use, but it's not sending - no error code received.
This is the HTML that I think is causing the issue :
<div style="margin-top:6px">
<input type="hidden" name="s" value="">
<input type="hidden" name="securitytoken" value="1515973553-20dc0500315dc868c0bad3384f0d0adb6b85fdd6">
<input type="hidden" name="do" value="insertpm">
<input type="hidden" name="pmid" value="">
<input type="hidden" name="forward" value="">
<input type="submit" class="button" name="sbutton" id="vB_Editor_001_save" value="Submit Message" accesskey="s" tabindex="1">
<input type="submit" class="button" value="Preview Message" accesskey="r" name="preview" tabindex="1">
</div>
In any case, here's the page: http://forum.toribash.com/private.php?do=newpm
But you'd need an account on the forum to access that page, by default.
Here's my payload, using requests:
msg_data = {
'title': "Discord registration request",
'message': "TEST",
'securitytoken': auth_final,
'do': "insertpm",
}
r = session_requests.post(url, data=msg_data)
result = session_requests.get(url,headers = dict(referer = url))
tree_pm_send = html.fromstring(result.content)
I'm 100% sure that security token variable I entered is correct, but nothing appears in my sent inbox after that.
i am trying to login into a website using python script having the 'form' tag like this in the source code.
<form action="trylogin.php" method="post">
<input name="authenticity_token" type="hidden" value="dpsTlD8zutj35FVWJTIqtUZGX67qQ/vab33hpPyYuaU=" />
<input id="user_username" maxlength="20" name="username" placeholder="Username" size="20" type="text" />
<input id="user_password" name="password" placeholder="Password" size="20" type="password" />
<input class="submit themed_bg themed-dark-hover-background" name="commit" type="submit" value="Login" />
</form>
and i am trying the following python code
import requests
import lxml
import lxml.html
s = requests.session()
login = s.get('url')
login_html = lxml.html.fromstring(login.text)
hidden_inputs = login_html.xpath(r'//form//input[#type="hidden"]')
form = {x.attrib["name"]: x.attrib["value"] for x in hidden_inputs}
form['username'] ='user'
form['password'] ='pass'
form['commit'] = 'Login'
response = s.post('url', data=form)
response.ok
response.url
s.get() is working fine and response.ok also gives 'true'output but url of response is same as of previous page. it seems like it is redirecting the same page. i can't login from python. What should i do Should i use header arguement in s.post()? how to know it? and i have used
form['commit'] = 'Login'
since login is in form of input not button, is it correct?
<input class="submit themed_bg themed-dark-hover-background" name="commit" type="submit" value="Login" />
I'm trying to use requests to login into a website using post. I have this form...
<form action="/" method="post" id="login_form" class="formposition" style="display: block;">
<input type="text" name="btc_address" id="login_form_btc_address">
<input type="password" name="password" id="login_form_password">
<input type="submit" value="LOGIN!" id="login_button" class="button expand" style="margin:0;">
I wrote this code:
import requests
url = "https://freebitco.in/?op=home"
values = { "btc_address": "username", "password": "password"}
r = requests.post(url, data=values)
However, when I run the code it doesn't work... can someone give me an advice?
Using firebug in firefox, you can see that when you login into the website, posting password and address is not enough, you need:
'btc_address': 'your_btc_address',
'csrf_token': 'the_csrf_token',
'op': 'login',
'password': 'your_password'
I have this form, but I am not sure how to create the payload that will do this correctly.
<form method="post" action="/login" name="loginform" id="loginForm">
<fieldset id="fs">
<label for="username">Username:
<input type="text" id="username" name="username" />
</label>
<label for="password">Password:
<input type="password" id="password" name="password" />
</label>
<input type="hidden" name="act" value="login" />
<input name="submit" type="submit" id="submit" value="Login" />
</fieldset>
</form>
I tried doing payload = {"username":"blah","password":"blah"}; r=requests.post(url, data=payload) but I didn't get the response I was expecting; namely, r.text doesn't have the expected "Login failed" line in it.
But when I fill out the form and try to log in for the first time through a browser, it indicated that it was my second failed login.
The website I'm playing with in particular is www.thepiratebay.se, and what I'm working towards is being able to programmatically upload a torrent file.
---EDIT---
The new code I am using is
import requests
user = "username"
pswd = "password"
url = "http://www.thepiratebay.se/login"
payload = {
"act":"login",
"username":user,
"password":pswd,
"submit":"Login"
}
r = requests.post(url, data=payload, allow_redirects=True)
print r.text
Still not working! r.text is just the default login page. Anymore suggestions?
use firebug net tab to track down the actual sent parameters, this is what I got when I gave it a try:
act login
password password
submit Login
username username
Source
username=username&password=password&act=login&submit=Login
I ended up going with a different module, twill, to do what I wanted. I think twill is actually a 'full' web browser. Anyway, this is what the code turned into:
from twill import commands
commands.go("http://www.thepiratebay.se/login")
commands.form("loginform", "username", "blah")
commands.form("loginform", "password", "blah")
commands.submit()
Using python 3.2.3 I am trying to log into some websites and save the source from a hidden page to a file. I am stuck on how to log in. I have figured out how to log into a specific website but can't seem to get it working on others. The source of the webpage I am trying to log into is:
<form id="login_form" name="login_form" action="https://www.o2online.ie/amserver/UI/Login?org=o2ext&goto=%2F%2Fwww.o2online.ie%2Fo2%2Fmy-o2%2F" method="post">
<p id="form_header">My Account login</p>
<input value="Go" type="hidden" name="IDButton" id="IDButton"/>
<input value="o2ext" type="hidden" name="org" id="org"/>
<input value="TRUE" type="hidden" name="CONNECTFORMGET"/>
<label for="IDToken1">Username</label><br />
<input tabindex=1 type="text" id="IDToken1" name="IDToken1" value="Username/mobile" onclick="javascript: this.value='';" maxlength="60" onfocus="this.value='';" tabindex=1 /><br />
<br />
<label for="IDToken2">Password</label><br />
<input tabindex=2 type="password" id="IDToken2" name="IDToken2" value="" maxlength="30" onfocus="this.value='';" tabindex=2 />
<input tabindex=3 type="image" src="../images/my-o2/Login-button.png" id="submit_button" />
</form>
On the other pages that I have logged into sucessfully have had a submit button with a certain value but this webpage has an image for the submit button which has no value.
The code I have been using is:
import urllib.request
import urllib.parse
import http.client
import sys
url = 'http://www.o2online.ie/o2/login/'
login_data = {
'IDToken1': 'xxxx',
'IDToken2': 'xxxx',
#'submitbutton': 'submit'
}
# creating an opener object that will handle the cookies
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor)
r = opener.open(url, urllib.parse.urlencode(login_data).encode())
# logged in
#Opening and saving source to a file
f = opener.open('http://www.o2online.ie/o2/my-o2/')
sys.stdout = open('file.html', 'w')
print (f.read(999999))
I have commented out the submit button.
Can this code be modified to log into this website?
How would you submit this form in python?