hotmail

brueckd at tbye.com brueckd at tbye.com
Sat Mar 2 16:33:41 EST 2002


On Fri, 1 Mar 2002, Cameron Laird wrote:

> >> im just wondering whether it is possible to access hotmail thru python
> >> so that i have a far quicker way of checking my emails. can telnetlib
> >> do this?
> >
> >If you're on Windows (and have either ActiveState Python or Mark Hammond's
> >win32all extensions) then you can use COM to create a web browser instance
> >and use its DOM interface to navigate. I made a small script to retrieve
> >my address book this way - I'll try to dig it up and post it, but it's
> >pretty easy to do from scratch too.
> >
> >-Dave
>
> I'd like to see this.

I don't know what I did with the original version I wrote, so I
rewrote it. I don't know if I'm doing everything by the "officially
correct" methods, but it works great. Here you go:

'''
Example of using InternetExplorer COM/DOM interfaces to
retrieve your Hotmail address book.

Dave Brueck
'''

import win32com.client, time

def WaitForDoc(ie):
    'Return the document once it is loaded'
    while ie.Busy: time.sleep(0.1)
    doc = ie.Document
    while doc.readyState != 'complete': time.sleep(0.1)
    return doc

def ClickOn(ie, text):
    'Follow a link and return the resulting doc'
    for link in ie.Document.Links:
        if link.href.find(text) != -1:
            link.Click()
            break
    return WaitForDoc(ie)

# Start up a browser
ie = win32com.client.Dispatch('InternetExplorer.Application.1')
ie.Visible=0 # Change to 1 to see it in action
ie.Navigate('http://www.hotmail.com')
doc = WaitForDoc(ie)

# Fill in the username and password
doc.All('login').Value = 'USERNAME' # Insert your own here
doc.All('passwd').Value = 'PASSWORD' # Ditto
doc.Forms[0].Submit()

# Click on the Address Book link
doc = WaitForDoc(ie)
doc = ClickOn(ie, 'addresses')

# Find the correct table
doc = WaitForDoc(ie)
table = doc.getElementsByName('ListTable')[0]

# Extract the rows (spruce up as needed)
rows = table.rows
for i in range(rows.length):
    cells = rows[i].cells
    for j in range(cells.length):
        print cells[j].innerText, '|',
    print

# Logout
doc = ClickOn(ie, 'logout')
doc = WaitForDoc(ie)






More information about the Python-list mailing list