Note that there are some explanatory texts on larger screens.

plurals
  1. POPython 3.X Playing with the internet
    text
    copied!<p>I'm doing a small project to help my work go by faster. I currently have a program written in Python 3.2 that does almost all of the manual labour for me, with one exception. I need to log on to the company website (username and password) then choose a month and year and click download. I would like to write a little program to do that for me, so that the whole process is completely done by the program.</p> <p>I have looked into it and I can only find tools for 2.X. I have looked into urllib and I know that some of the 2.X moudles are now in urllib.request.</p> <p>I have even found some code to start it off, however I'm confused as to how to put it into practise.</p> <p>Here is what I have found:</p> <pre><code>import urllib2 theurl = 'http://www.someserver.com/toplevelurl/somepage.htm' username = 'johnny' password = 'XXXXXX' # a great password passman = urllib2.HTTPPasswordMgrWithDefaultRealm() # this creates a password manager passman.add_password(None, theurl, username, password) # because we have put None at the start it will always # use this username/password combination for urls # for which `theurl` is a super-url authhandler = urllib2.HTTPBasicAuthHandler(passman) # create the AuthHandler opener = urllib2.build_opener(authhandler) urllib2.install_opener(opener) # All calls to urllib2.urlopen will now use our handler # Make sure not to include the protocol in with the URL, or # HTTPPasswordMgrWithDefaultRealm will be very confused. # You must (of course) use it when fetching the page though. pagehandle = urllib2.urlopen(theurl) # authentication is now handled automatically for us </code></pre> <p>All Credit to Michael Foord and his page: <a href="http://www.voidspace.org.uk/python/articles/authentication.shtml" rel="nofollow">Basic Authentication</a></p> <p>So I changed the code around a bit and replaced all the 'urllib2' with 'urllib.request'</p> <p>Then I learned how to open a webpage, figuring the program should open the webpage, use the login and password data to open the page, then I'll learn how to download the files from it.</p> <pre><code>ie = webbrowser.get('c:\\program files\\internet explorer\\iexplore.exe') ie.open(theurl) </code></pre> <p>(I know Explorer is garbage, just using it to test then I'll be using crome ;) )</p> <p>But that doesnt open the page with the login data entered, it simply opens the page as though you had typed in the url.</p> <p>How do I get it to open the page with the password handle? I sort of understand how Michael made them, but I'm not sure which to use to actually open the website.</p> <p>Also an after thought, might I need to look into cookies?</p> <p>Thanks for your time</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload