Python Mechanize Error +

D3T*_*XER 1 python forms user-input mechanize

我是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()
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

我查看了\ 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="******">
Run Code Online (Sandbox Code Playgroud)

上面的内容是登录页面上的帖子.下面的东西来了

<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>
Run Code Online (Sandbox Code Playgroud)

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

 br.select_form(name="orders")
 br.form['continue'] = 'E23558'
Run Code Online (Sandbox Code Playgroud)

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

Ped*_*ano 6

从您的错误消息,您正在尝试安装并mechanize使用Python 3.2运行该程序包.

mechanize但是不支持Python 3,所以你应该安装一个Python 2.x版本(最新版本是2.7.5),在mechanize那里安装软件包,然后重试运行你的脚本.