So I've been trying this for a long time and I've given up. I can't code it myself and no examples are helping. I want to log on to a website and return some text from it. The returning text part won't be hard, the logging in is the problem. For an example, could someone please give me the code to log on to this site and return the html of the page? If you could make it so that all I would have to do is replace my username and password, that would be great. I don't like other people writing my code, but its not so fun anymore, and I need it soon. Thanks!

First of all, use GOOGLE...
Second of all, post some code, and not just beg for replies!

Hint: Use urllib2 module to get the HTML code and write it to a HTML file

...
Of course I've googled it, I put a lot of effort in to making this work. Examples don't work for me, so now I want an example that works for a specific site that someone has written and I can contact that someone if I have any questions. The only code I have are exact examples from online, and none of them work so it would much quicker to get a working version from someone rather than take the code from a random website.

The only code I have are exact examples from online, and none of them work so it would much quicker to get a working version from someone rather than take the code from a random website.

Here's a better approach: provide us the code that you tried on your system. Explain to us what "doesn't work" (ie, expected output vs. observed output). Then we can help to resolve your issues.

This site isn't about solving your homework. We only help those who show effort. It's in the announcements. Please read all the rules and announcements before posting to any forum. That's internet science.

import urllib
import urllib2
import cookielib

cookiejar = cookielib.CookieJar()
urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
urllib2.install_opener(urlOpener)

values = {'loginID': 'username', 'password': 'password'}
data = urllib.urlencode(values)
request = urllib2.Request('website',data)
url = urlOpener.open(request)
print url.read()

This is one of the many things I tried... No matter what site I use it on, when I check the output, it is the output from the page that isn't logged in. Also, this is not a homework assignment, just letting you know.

Hey there,

The short answer is that you haven't looked hard enough. ;)

I realize that you probably don't want to hear that, so here's a quick and dirty explanation. Most sites don't like computers logging themselves into their protected areas. For this reason, they have created security to check browser, etc. It's not going to be easy. Think about it like this: Programmers spend months writing a secure login script, it's not going to take you 5 minutes to automate your way through it. This applies to most sites, email, youtube etc.

the even shorter answer is: you're going to have a hell of a time trying to log into your online banking account.

Haha, I see your point. But I don't think the login scripts are going to complain if I can somehow send the correct data with the POST command... I put send some data to the server and the server verifies that that is my account and it lets me see the resulting page. I don't see anything wrong with what I'm sending, and no login script should guard against correct data, so I don't understand what the problem is...

Haha, I see your point. But I don't think the login scripts are going to complain if I can somehow send the correct data with the POST command... I put send some data to the server and the server verifies that that is my account and it lets me see the resulting page. I don't see anything wrong with what I'm sending, and no login script should guard against correct data, so I don't understand what the problem is...

Sorry man, "well it should work" is not really a valid solution to your problem. Passing a website your username and password is not going to get you access, I can pretty much guarantee that. Even the most basic of login scripts encrypt the passwords, your script isn't even doing that. I realize that's somewhat trivial, but that's exactly my point. There's alot of trivial meaningless stuff to account for.

Anyway, I'm probably not helping you a whole lot, so I'll step aside and let somebody else try to tackle this one.

There's another post in this forum posted earlier today (or maybe late yesterday depending on your timezone).

It gives a working example of logging into a site. In his example he sends headers to spoof using a Mozilla web browser.

import twill, string, os
b=twill.commands.get_browser()
b.set_agent_string("Mozilla/5.0 (Windows; U; Windows NT 5.1;en-GB;rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14")
b.clear_cookies()
b.go("http://www.daniweb.com")
b.showforms()
f=b.get_form("2")
f['vb_login_username']=loginName
f['vb_login_password']=password
b.clicked(f,f)
b.submit()
b.showforms()

html=b.get_html()
html

I checked this and this worked

def get_xml(uri,login):
    netlock = urlparse.urlparse(uri)
    pswdMngr = urllib2.HTTPPasswordMgrWithDefaultRealm()
             pswdMngr.add_password(None,netlock[1],login.get("username"),login.get("password"))
    auth = urllib2.HTTPBasicAuthHandler(pswdMngr)
    req = urllib2.Request(uri)
    opener = urllib2.build_opener(auth)
    f = opener.open(req)
    xml = f.read()
    f.close()
    return xml

I used this to pull down an xml from twitter. Works nicely. login is a dictionary with username and password as keys

This article has been dead for over six months. Start a new discussion instead.