Skip to main content
 首页 » 编程设计

Python Mechanize 错误 +

2025年05月04日19lyj

我是 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' 

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

请您参考如下方法:

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

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