=== modified file 'examples/myspace.py' --- examples/myspace.py 2007-04-16 14:05:27 +0000 +++ examples/myspace.py 2007-06-12 18:51:30 +0000 @@ -12,7 +12,7 @@ #pykhtml.useXvfb = True def login(email, password, browser): - print "Here" + #print "Here" document = browser.document # First, check if we're already logged in header = document.getElementById("header") @@ -47,11 +47,33 @@ # called when the next browsed-to page has finished # loading browser.onNextLoad = displayAccountInformation + print "Logging in..." submit.click() - print "Clicked" + #print "Clicked" def displayAccountInformation(browser): - print "Stop" + document = browser.document + # all elements with a class of 'heading' and a tag name of 'h4' + headings = document.getElementsByClass("heading", "h4") + # parse out the nick name + if headings: + # should be of the form " Hello, NickName! " + welcomeMessage = list(headings)[0].text + nickName = welcomeMessage.split(",")[1].strip().rstrip("!") + print "Nickname:", nickName + else: + print "Oops, couldn't find the header" + # then the number of friends + for anchor in document.getElementsByTagName("a"): + # to do that, we find all links to the page that lets + # you view your friends... + if anchor.href.count("index.cfm?fuseaction=user.viewfriends"): + # and that also consists of nothing but digits (once + # stripped of the superfluous whitespace that + # Myspace's code is full of, of course) + if anchor.text.strip().isdigit(): + print "Number of friends:", anchor.text.strip() + break pykhtml.stopEventLoop() def signedOut(browser): @@ -69,6 +91,7 @@ browser = pykhtml.Browser() # the browser is passed as a parameter to `login` # when it is called (when the page has loaded) + print "Connecting to myspace.com..." browser.load("http://www.myspace.com/", pykhtml.partial(login, email, password)) # kick things off pykhtml.startEventLoop()