Programmatically logging into a site

后端 未结 3 1575
情书的邮戳
情书的邮戳 2021-01-25 08:46

It might sound silly; but can we programmatically login into a site such as Linkedin by passing our user credentials (userid and password)? I am not talking about using OAuth or

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-25 09:16

    You can login to many sites this way, using scripting. I generally prefer Python with BeautifulSoup, but there are many other possibilities for languages and libraries.

    The general idea is to locate the code for the form that has the username and password fields, fill them in, then post the form.

    This works for me on Linux, with my LinkedIn username and password in my .netrc in the standard format:

    #!/usr/bin/python
    import sys, os, urllib, urllib2, cookielib, urlparse, netrc
    from BeautifulSoup import BeautifulSoup
    URL = 'https://www.linkedin.com/uas/login'
    COOKIES = urllib2.HTTPCookieProcessor(cookielib.CookieJar())
    HANDLER = getattr(urllib2, urlparse.urlsplit(URL).scheme.upper() + 'Handler')
    DEBUGLEVEL = 0  # set to 1 to see unencrypted data
    OPENER = urllib2.build_opener(COOKIES, HANDLER(debuglevel = DEBUGLEVEL))
    def fetch(url, data = None):
     connection = OPENER.open(url, data)
     page = connection.read()
     connection.close()
     return page
    def login():
     soup = BeautifulSoup(fetch(URL))
     form = soup.find('form')
     fields = form.findAll('input')
     auth_info = netrc.netrc().authenticators(urlparse.urlsplit(URL).netloc)
     formdata = dict([[field['name'], field['value']] for field in fields])
     formdata['session_key'], ignored, formdata['session_password'] = auth_info
     assert form['method'] == 'POST'
     posturl = urlparse.urljoin(URL, form['action'])
     print fetch(posturl, urllib.urlencode(formdata))
    if __name__ == '__main__':
     login()
    

提交回复
热议问题