developer tip

Python으로 웹 사이트에 로그인하려면 어떻게해야합니까?

copycodes 2020. 10. 16. 07:34
반응형

Python으로 웹 사이트에 로그인하려면 어떻게해야합니까?


어떻게하니? 지정된 링크 (urllib 사용)를 입력하려고했지만 그렇게하려면 로그인해야합니다.

사이트에서 다음 소스가 있습니다.

<form id="login-form" action="auth/login" method="post">
    <div>
    <!--label for="rememberme">Remember me</label><input type="checkbox" class="remember" checked="checked" name="remember me" /-->
    <label for="email" id="email-label" class="no-js">Email</label>
    <input id="email-email" type="text" name="handle" value="" autocomplete="off" />
    <label for="combination" id="combo-label" class="no-js">Combination</label>
    <input id="password-clear" type="text" value="Combination" autocomplete="off" />
    <input id="password-password" type="password" name="password" value="" autocomplete="off" />
    <input id="sumbitLogin" class="signin" type="submit" value="Sign In" />

이것이 가능한가?


능직 을 사용하고 싶을 수도 있습니다 ( 기계화를 기반으로 함 ). 사용하기 매우 쉽고 원하는 것을 할 수 있어야합니다.

다음과 같이 표시됩니다.

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

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

submit('0')

당신은 사용할 수 있습니다 showforms()당신이 사용되면 모든 형태의 목록에 go(...)당신이 로그인 할 사이트로 이동 할 수 있습니다. 파이썬 인터프리터에서 시도하십시오.


간단하게 만들어 보겠습니다. 사이트의 URL이 www.example.com이고 사용자 이름과 비밀번호를 입력하여 가입해야하므로 http://www.example.com/login 이라는 로그인 페이지로 이동합니다 . .php 이제 소스 코드를보고 다음과 같은 형식 태그에있는 작업 URL을 검색합니다.

 <form name="loginform" method="post" action="userinfo.php">

이제 userinfo.php를 사용하여 ' http://example.com/userinfo.php ' 가 될 절대 URL을 만들고 이제 간단한 파이썬 스크립트를 실행하십시오.

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

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

언젠가 누군가에게 도움이되기를 바랍니다.


일반적으로 사이트에 로그인하려면 쿠키가 필요합니다. 이는 cookielib, urllib 및 urllib2를 의미합니다. 다음은 내가 Facebook 웹 게임을 할 때 답장 한 수업입니다.

import cookielib
import urllib
import urllib2

# set these to whatever your fb account is
fb_username = "your@facebook.login"
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.


import cookielib
import urllib
import urllib2

url = 'http://www.someserver.com/auth/login'
values = {'email-email' : 'john@example.com',
          '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


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('mymail@gmail.com' , 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


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!).


For HTTP things, the current choice should be: Requests- HTTP for Humans

참고URL : https://stackoverflow.com/questions/2910221/how-can-i-login-to-a-website-with-python

반응형