I am new to python (done some java in the past). I recently decided to automate a process that takes me about 20 hours once a year. I need to login to a vendor's website, with a login form they have. That then loads a new form which I can select an order from, and then it loads yet another form I can submit an item number to. This then loads the page with sizes of the item and price per size, I take this information and put it into a spreadsheet. The row has columns based on number of sizes and then price (item,sm,med,lg,9.99,10.99,12.99). After that return to the browser, i hit the back button and load the next item number into the field and so on and so forth. I am about to send a bunch of information your way, so sorry about that.
Upon doing some research I found a library for Python called mechanize which seemed to make it easy to submit web forms and then collect the data.
'''
Created on Sep 29, 2012
@author: Teddy
'''
from tkinter import *
import mechanize
import urllib
import logging
import sys
import http.cookiejar
def main():
br = mechanize.Browser()
cj = http.cookiejar.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
br.open('https://*******/cgi-bin/wfos/order.exe')
# Select the login form named "login"
br.select_form(name="login")
# User credentials, this is usrname and passwords to submit to form
br.form['custno'] = '*******'
br.form['Password1'] = '*******'
br.form['Password2'] = '**********'
# Login, submits to the form
br.submit()
main()
Currently when I compile this I get an error:
Traceback (most recent call last):
File "C:\EclipseWorkspaces\csse120\FOLDERNAME\src\main.py", line 9, in <module>
import mechanize
File "C:\Python32\lib\site-packages\mechanize\__init__.py", line 119, in <module>
from _version import __version__
ImportError: No module named _version
I looked in the \site-packages\mechanize folder and I saw a module name version.py. So I am not sure why I am getting this error.
The site I use reloads the page with new content with in it. There are buttons to choose the order you wish to load.
<FORM NAME="orders" METHOD="POST" ACTION="https://******/cgi-bin/wfos/order.exe">
<input type="hidden" name="form" value="continue">
<input type="hidden" name="cs_id" value="">
<input type="hidden" name="customer_type" value="1">
<input type="hidden" name="customer" value="******">
<input type="hidden" name="custno" value="******">
<input type="hidden" name="password1" value="******">
<input type="hidden" name="password2" value="******">
The stuff above is the post from the login page. The stuff below comes
<tr bgcolor=D3D3D3><td align=center><input name=del23558 type=checkbox></td>
<td align=center><input name=continue value=E23558 type=submit></td>
<td align=center><font size=-1>Sep 29 2012 1:19PM</font></td>
<td align=right><font size=-1>$0.00</font></td>
<td align=right><font size=-1>0</font></td></tr>
</table><P>
<input name="action" type="submit" value="Cancel Checked Orders"
onClick="return confirm('Are you sure you want to cancel the checked orders?')"><P>
<input name="action" type="submit" value="Start a New Order"><P>
</FORM>
To submit the order would this still just be?:
br.select_form(name="orders")
br.form['continue'] = 'E23558'
Thank you very much for anyhelp you could provide.
From your error message you are trying install and run the
mechanize
package with Python 3.2.mechanize
however doesn't support Python 3 so you should install a Python 2.x release (the latest available is 2.7.5), install themechanize
package there and then retry running your script.