Python Mechanize Error +

时间:2012-09-29 19:18:11

标签: python forms mechanize user-input

我是python的新手(过去做了一些java)。我最近决定自动化一个过程,每年花费我大约20个小时。我需要使用他们拥有的登录表单登录供应商的网站。然后加载一个新的表单,我可以从中选择一个订单,然后它加载另一个表格,我可以提交一个项目编号。然后,这会加载包含商品尺寸和每尺寸价格的页面,我会将此信息放入电子表格中。该行具有基于大小数量的列,然后是价格(item,sm,med,lg,9.99,10.99,12.99)。在返回浏览器之后,我点击后退按钮并将下一个项目编号加载到字段中,依此类推。我将以你的方式发送大量信息,对此感到抱歉。

在做了一些研究之后,我发现了一个名为mechanize的Python库,它似乎可以很容易地提交Web表单然后收集数据。

'''
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()

目前,当我编译它时,我收到一个错误:

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

我查看了\ site-packages \ mechanize文件夹,我看到了一个模块名称version.py。所以我不确定为什么会收到这个错误。

我使用的网站重新加载包含新内容的网页。有按钮可以选择您要加载的订单。

 <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="******">

上面的内容是登录页面上的帖子。下面的内容来了

<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>

要提交订单,这仍然只是?:

 br.select_form(name="orders")
 br.form['continue'] = 'E23558'

非常感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:6)

在您尝试安装的错误消息中,使用 Python 3.2 运行mechanize包。

mechanize但是不支持 Python 3 ,因此您应该安装 Python 2.x 版本(最新版本)是2.7.5),在那里安装mechanize包,然后重试运行您的脚本。