Python 使用Mechanize修改表单
我正在使用Python2.7修改URL表单,但它不允许我这样做。请帮忙 这就是我到目前为止所尝试的Python 使用Mechanize修改表单,python,python-2.7,mechanize,Python,Python 2.7,Mechanize,我正在使用Python2.7修改URL表单,但它不允许我这样做。请帮忙 这就是我到目前为止所尝试的 #@PydevCodeAnalysisIgnore import re import mechanize import cookielib ## //*[@id="license"] ## //*[@id="state"] # Browser br = mechanize.Browser() # Cookie Jar cj = cookielib.LWPCookieJar() br.set_
#@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]
没有所谓的
许可证
或许可证日期
。以下代码将成功提交查询:
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()
使用以下代码的打印输出获取下拉菜单的有效值:
for control in br.form.controls:
print control
print "type=%s, name=%s value=%s" % (control.type, control.name, br[control.name])
由于只有一个表单且表单没有名称,因此可以执行以下操作以获取表单:
#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',]
编辑:我认为您也缺少第一个字段下拉列表“region”的值。
许可证字段似乎有多个带有“license”和“l”的名称标记,与“licenseState”相同
然后再次尝试提交,看看您得到了什么。您使用的是
浏览器.forms()
,我想您是想用br.forms()
来代替它?是的,对不起,让我来解决。同样,它仍然不起作用:(请帮助