Programmatically logging into a site

后端 未结 3 1568
情书的邮戳
情书的邮戳 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:06

    Yes, you could use a HttpWebRequest in order to send HTTP requests to a given URL. So the idea would be to POST your credentials to the given url which is supposed to authenticate the user and capture any cookies that could be used on subsequent requests to authenticated parts of the site.

    Of course that's pretty raw work, if the remote site provides an API you might also use this API as it will simplify things.

    0 讨论(0)
  • 2021-01-25 09:12

    You can use tools like selenium to automate and generate code for programmatically login. Also a search in google sure reveals more tools aimed to do "mashups" of web sites, like BeautifulSoup recommended in first answer.

    Regards

    0 讨论(0)
  • 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()
    
    0 讨论(0)
提交回复
热议问题