Loging into a webpage using python.

  • Thread starter dacruick
  • Start date
  • Tags
    Python
In summary, the conversation is about having trouble accessing data online due to an authentication process. The code provided uses cookielib and urllib to handle the authentication process, but upon trying to access the desired link, it redirects back to the authentication page. It is suggested that there may be an issue with not properly entering information into the username and password fields or not clicking the login button, as the website uses POST in their form while the code produces GET queries.
  • #1
dacruick
1,042
1
Hi,

I am trying to access some data online, but I am having trouble getting past the actual authentication process.

Code:
import cookielib
import urllib, urllib2

if __name__ == '__main__':
    urlLogin = '[PLAIN]https://www.hobolink.com'[/PLAIN] 

    uid    = 'userid'
    password = 'xxxxxxx'

    fieldId   = 'username'
    fieldPass = 'password'
    
    ButtonId = 'submit'
    Button = 'Log in'

    cj = cookielib.CookieJar()
    data = urllib.urlencode({fieldId:uid, fieldPass:password, ButtonId:Button})

    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

    urllib2.install_opener(opener)
    usock = opener.open(urlLogin)
    usock = opener.open(urlLogin, data)
    #pageSource = usock.read()
    usock.close()
 
    usock = opener.open('FinalWebpage')
    pageSource = usock.read()
    usock.close()
    print(pageSource)

The HTML code which corresponds to the Username, Password, and login button are respectively as follows.

Code:
<input id="username" name="username" type="text">
Code:
<input id="password" name="password" type="password">
Code:
<li id="submit"><input class="button" name="commit" onclick="alertForExplorerBrowserVersion(7, 3);" type="submit" value="Log in"></li>

After I try to access the sought after link, it redirects me back to the authentication page. So two possible things are happening. The first one is, I am not entering anything into the username and password fields. The second possibility is that I am not "clicking" the log in button, but instead am trying to open a page that I am not authenticated to open.
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
One thing I notice is that "hobolink" (do you have their permission to use their site in this way..?) uses POST in their form, whereas urllib will produce GET queries.
 
  • #3
Coin said:
One thing I notice is that "hobolink" (do you have their permission to use their site in this way..?) uses POST in their form, whereas urllib will produce GET queries.

hmm, I'm pretty new at this so I'm not sure exactly what the difference is, but that would be consistent with my results thus far :frown:
 

1. How can I log into a webpage using python?

There are a few different ways to log into a webpage using python. One way is to use the 'requests' library, which allows you to make HTTP requests and handle cookies. Another way is to use the 'selenium' library, which can automate web browser actions like filling out forms and clicking buttons.

2. Do I need to know HTML or web development to log into a webpage using python?

No, you do not need to have knowledge of HTML or web development to log into a webpage using python. However, having a basic understanding of HTML and how webpages work can be beneficial in troubleshooting any issues that may arise.

3. Can I log into any webpage using python?

Technically, yes. However, some webpages may have additional security measures in place that make it more difficult to log in using python. It is also important to make sure that you have permission to access and log into the webpage you are trying to automate.

4. How can I handle errors when logging into a webpage using python?

You can handle errors by using try/except statements in your code. This allows you to catch any errors that may occur and handle them accordingly. You can also use the 'logging' library to create logs of any errors that may occur during the login process.

5. Is it legal to log into a webpage using python?

It depends on the terms of service and the intended use of the webpage. It is always best to check with the website owner or read the terms of service before attempting to log into a webpage using python. In some cases, scraping or automating web actions may be prohibited.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top