Python Mechanize 动态下拉选择
我正在使用mechanize这个工具来填写表单,但遇到了一个问题,就是有些下拉列表是根据之前的选择动态生成的。
在mechanize中,我是这样选择类别的:
import mechanize
br = mechanize.Browser()
"""Select the form and set up the browser"""
br["state"] = ["California"]
br["city"] = ["San Francisco"] # this is where the error is
br.submit()
我不能选择城市为“旧金山”,直到我先选择州为“加利福尼亚”,因为城市的下拉列表是在选择“加利福尼亚”之后动态生成的。
那么我该如何用Python和mechanize来提交城市呢?
1 个回答
1
mechanize不支持JavaScript。相反,你应该使用urllib2来发送你想要的值。
import urllib2
import urllib
values = dict(state="CA", city="SF") # examine form for actual vars
try:
req = urllib2.Request("http://example.com/post.php",
urllib.urlencode(values))
response_page = urllib2.urlopen(req).read()
except urllib2.HTTPError, details:
pass #do something with the error here...