使用Mechanize修改表单

时间:2014-11-17 18:17:39

标签: python python-2.7 mechanize

我使用python 2.7来修改URL表单,但它不允许我这样做。请帮忙

这是我到目前为止所尝试的。

#@PydevCodeAnalysisIgnore
import re
import mechanize
import cookielib
## //*[@id="license"]
## //*[@id="state"] 

# Browser
br = mechanize.Browser()

# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

# Browser options
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)

# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

# User-Agent (this is cheating, ok?)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

# The site we will navigate into, handling it's session
br.open("http://www.autoreturn.com/app/search")

# Select the first (index zero) form
print [form for form in br.forms()][0]

plate = "GEL997" 
# User credentials
# br.select_form(id="searchParameters")

br.form['license'] = plate
br.form['licenseState'] = 'MD'


#response = br.response()
#a = response.read()
#print a
for f in br.forms():
    print f.name


response = br.submit() 
content = response.read() 
result = re.findall(r'[Ss]earch', content) 
print result[0]

2 个答案:

答案 0 :(得分:1)

没有任何名为licenceliceneState的内容。以下代码将成功提交查询:

import mechanize
br = mechanize.Browser()
res = br.open('http://www.autoreturn.com/app/search')

br.form = list(br.forms())[0]
br.form['l'] = 'GEL997'
br.form['region'] = ['BCO-MD']
br.form['s'] = ['MD']
r = br.submit()
print r.read()

使用以下代码中的print来获取下拉菜单的有效值:

for control in br.form.controls:
    print control
    print "type=%s, name=%s value=%s" % (control.type, control.name, br[control.name])

答案 1 :(得分:1)

由于只有一个表单且表单没有名称,您可以这样做以获取表单:

#Change this
print [form for form in br.forms()][0]
#to
br.form = [form for form in br.forms()][0]

然后又做了一些改变:

#Change this
br.form['license'] = plate
br.form['licenseState'] = 'MD'
#To
br.form['l'] = plate
br.form['s'] = ['MD',]

编辑:我认为你错过了第一个字段下拉区域的价值'太。 看起来许可证字段有多个名称标签,其中包含' license'和' l',与' licenseState'相同 然后尝试再次提交,看看你得到了什么。