[Edu-sig] Thinking Python - nice lesson

Jason Cunliffe jasonic@nomadicsltd.com
Sun, 24 Sep 2000 14:35:53 -0400


Apologies for cross-posting quotes below,  but I don't know how else to
discuss it..and you may have missed it.

Following is a mini-tutorial reply posted by Michal Wallace
<sabren@manifestation.com> on comp.lang.python. I think it is a beautiful
python win32com lesson.

This picks up the recent  thread here about learning Python tutorials, and
ways to give a feeling that one is sitting next to a more experienced

Anyway I love his reply because it is practical [solves someone's real
problem],  but also allows one immediately to step into feeling the process
of how to use resources available, and at the python prompt and online to
find the answer.

Above all it is the tone of real-time learning that's says so much 'betweeen
the lines' if you see what I mean.. ok ok whayt I am saying is it is clear
aand human thos samll comments of his really do help.
This expressiveness, body language signals are easily lost online..in formal

Maybe this seems trivial/obvious to you all...maybe not.
Perhaps not your style.
Not sure if one should or could do a whole book or series like this.

I read it and went "aha!" and so did some others

What do you think ?

- Jason

PS. It reminds me a little of the excellent: 'Thinking Forth' by Leo Brodie
which is now back in print.. Paperback 2nd ed edition (1994)
Forth Interest Group; ISBN: 0935533001
The first volume was 'Starting Forth' and still out of print, though listed
on Amazon.

Someone needed to use SSL on win32 with Python.
After frustrated by failed attempts he posted to comp.lang.python for help.
Tim Peters referred him to read:
A pragmatic solution taking advantage of MS internet explorer was offered:

"Pedro" <diazjimenez@ctv.es> wrote :
>I've just get involved in a project that needs to fetch some web pages via
SSL. I >said :
>"ok, no problem. Just let me take my debian box..." . My boss
>said this time it will we a WIN32 project: "oops!" (error #1)
[snip descriptive list of problems he encountered trying to solve this]

Just use the win32com package to automate IE.

On Sat, 23 Sep 2000, Pedro wrote:
> What module I have to import?. Was looking at the library ref. but found
> nothing!

you need the win32 extensions, for one thing..
once you have them:

  - open pythonwin
  - load the "COM Makepy utility" under the Tools menu
  - in the window that pops up, double click on:
     "Microsoft Internet Controls"

This generates a magical python module for you.. Now, in
the python window, type:

>>> from win32com.client import Dispatch
>>> ie = Dispatch("InternetExplorer.Application")
>>> ie.Visible = 1

At this point, if things are going well, a browser will appear,
but will have no content... So.. let's go someplace secure:

>>> ie.Navigate("https://secure.authorize.net/")

... Well, it shows up in the browser... But how to get the

In pythonwin, go to "Tools/COM Browser"
"Registered Type Libraries"
    "Microsoft Internet Controls"
        "Type Library"
            "InternetExplorer - CoClass"

.. That didn't help...

>>> ie
<win32com.gen_py.Microsoft Internet Controls.IWebBrowser2>


"Tools/COM Browser"
    "Registered Type Libraries"
        "Microsoft Internet Controls"
            "Type Library"

Hmmm. That seems to show a bit more info, but we still don't
know what to do with it.. Let's go to msdn and see if we can
look that control up... Here it is:


Looks like we want the "get Document" thing. Back to python.

>>> ie.Document
<COMObject Document>


>>> doc = ie.Document

According to the MSDN docs, this is just a normal DHTML document

>>> doc.url

>>> print doc.body.innerHTML
[it works!]

doesn't print the WHOLE HTML document, granted, just the body..  but
you can basically script it as if it were client side DHTML because
you're using the same engine. You may not need to grab it..


Anyway, to get rid of it:

>>> del doc
>>> ie.Quit()


- Michal <sabren@manifestation.com>
www.manifestation.com  www.sabren.com  www.linkwatcher.com  www.zike.net