How do i use python to login to a website?

How can I do it? I was trying to enter some specified link (with urllib), but to do it, I need to log in.

I have this source from the site:

Is this possible?

Vadim Kotov

7,8468 gold badges46 silver badges61 bronze badges

asked May 26, 2010 at 5:17

How do i use python to login to a website?

Bruno 'Shady'Bruno 'Shady'

4,10812 gold badges53 silver badges73 bronze badges

Maybe you want to use twill. It's quite easy to use and should be able to do what you want.

It will look like the following:

from twill.commands import *
go('http://example.org')

fv("1", "email-email", "blabla.com")
fv("1", "password-clear", "testpass")

submit('0')

You can use showforms() to list all forms once you used go… to browse to the site you want to login. Just try it from the python interpreter.

answered May 26, 2010 at 5:38

How do i use python to login to a website?

6

Let me try to make it simple, suppose URL of the site is www.example.com and you need to sign up by filling username and password, so we go to the login page say http://www.example.com/login.php now and view it's source code and search for the action URL it will be in form tag something like

 

now take userinfo.php to make absolute URL which will be 'http://example.com/userinfo.php', now run a simple python script

import requests
url = 'http://example.com/userinfo.php'
values = {'username': 'user',
          'password': 'pass'}

r = requests.post(url, data=values)
print r.content

I Hope that this helps someone somewhere someday.

answered Feb 20, 2015 at 12:01

5

Typically you'll need cookies to log into a site, which means cookielib, urllib and urllib2. Here's a class which I wrote back when I was playing Facebook web games:

import cookielib
import urllib
import urllib2

# set these to whatever your fb account is
fb_username = ""
fb_password = "secretpassword"

class WebGamePlayer(object):

    def __init__(self, login, password):
        """ Start up... """
        self.login = login
        self.password = password

        self.cj = cookielib.CookieJar()
        self.opener = urllib2.build_opener(
            urllib2.HTTPRedirectHandler(),
            urllib2.HTTPHandler(debuglevel=0),
            urllib2.HTTPSHandler(debuglevel=0),
            urllib2.HTTPCookieProcessor(self.cj)
        )
        self.opener.addheaders = [
            ('User-agent', ('Mozilla/4.0 (compatible; MSIE 6.0; '
                           'Windows NT 5.2; .NET CLR 1.1.4322)'))
        ]

        # need this twice - once to set cookies, once to log in...
        self.loginToFacebook()
        self.loginToFacebook()

    def loginToFacebook(self):
        """
        Handle login. This should populate our cookie jar.
        """
        login_data = urllib.urlencode({
            'email' : self.login,
            'pass' : self.password,
        })
        response = self.opener.open("https://login.facebook.com/login.php", login_data)
        return ''.join(response.readlines())

You won't necessarily need the HTTPS or Redirect handlers, but they don't hurt, and it makes the opener much more robust. You also might not need cookies, but it's hard to tell just from the form that you've posted. I suspect that you might, purely from the 'Remember me' input that's been commented out.

answered May 26, 2010 at 6:19

Anthony BriggsAnthony Briggs

3,2351 gold badge20 silver badges12 bronze badges

Web page automation ? Definitely "webbot"

webbot even works web pages which have dynamically changing id and classnames and has more methods and features than selenium or mechanize.

Here's a snippet :)

from webbot import Browser 
web = Browser()
web.go_to('google.com') 
web.click('Sign in')
web.type('' , into='Email')
web.click('NEXT' , tag='span')
web.type('mypassword' , into='Password' , id='passwordFieldId') # specific selection
web.click('NEXT' , tag='span') # you are logged in ^_^

The docs are also pretty straight forward and simple to use : https://webbot.readthedocs.io

answered Jul 4, 2018 at 9:22

Natesh bhatNatesh bhat

11k10 gold badges74 silver badges115 bronze badges

4

import cookielib
import urllib
import urllib2

url = 'http://www.someserver.com/auth/login'
values = {'email-email' : '',
          'password-clear' : 'Combination',
          'password-password' : 'mypassword' }

data = urllib.urlencode(values)
cookies = cookielib.CookieJar()

opener = urllib2.build_opener(
    urllib2.HTTPRedirectHandler(),
    urllib2.HTTPHandler(debuglevel=0),
    urllib2.HTTPSHandler(debuglevel=0),
    urllib2.HTTPCookieProcessor(cookies))

response = opener.open(url, data)
the_page = response.read()
http_headers = response.info()
# The login cookies should be contained in the cookies variable

For more information visit: https://docs.python.org/2/library/urllib2.html

gmsi

1,0121 gold badge14 silver badges26 bronze badges

answered May 26, 2010 at 6:18

blokeleyblokeley

6,4889 gold badges51 silver badges75 bronze badges

0

Websites in general can check authorization in many different ways, but the one you're targeting seems to make it reasonably easy for you.

All you need is to POST to the auth/login URL a form-encoded blob with the various fields you see there (forget the labels for, they're decoration for human visitors). handle=whatever&password-clear=pwd and so on, as long as you know the values for the handle (AKA email) and password you should be fine.

Presumably that POST will redirect you to some "you've successfully logged in" page with a Set-Cookie header validating your session (be sure to save that cookie and send it back on further interaction along the session!).

answered May 26, 2010 at 5:27

Alex MartelliAlex Martelli

824k163 gold badges1203 silver badges1380 bronze badges

How do I connect to a website using Python?

Before we run the code to connect to Internet data, we need to import statement for URL library module or “urllib”..
Import urllib..
Define your main function..
Declare the variable webUrl..
Then call the urlopen function on the URL lib library..
The URL we are opening is guru99 tutorial on youtube..

Can you integrate Python into a website?

The Python programming language can be used to create a huge variety of different types of things, including websites. Making websites with Python is easier than most people think because of the fact that this language makes use of something called “frameworks.”

How do I get the username and password for a website using Python?

Before we begin let's make sure the folder is set up correctly so after everything is done the folder should hold 3 files. Our main script which is called test_login.py, the username and password file, called "login_details", and finally chromdriver.exe. Let's make sure our program works and begin using our first bot.

How do I automate a website login?

Steps for Login Automation using Selenium WebDriver.
Create a Selenium WebDriver instance..
Configure browser if required..
Navigate to the required web page..
Locate the relevant web element..
Perform action on the web element..
Verify and validate the action..