简体   繁体   English

使用动态下拉选择的Python机械化

[英]Python Mechanize with Dynamic Dropdown Selection

I'm using mechanize to fill forms and I run into a problem with dynamically-filled dropdown lists that are dependent on a previous selection. 我正在使用机械化来填充表单,并且我遇到了依赖于先前选择的动态填充下拉列表的问题。

In mechanize, I do something like this to select the category: 在机械化中,我做这样的事情来选择类别:

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()

I cannot choose the city as "San Francisco" until I have chosen the state as "California," because the city dropdown list is dynamically populated after choosing "California." 在我选择州为“加利福尼亚州”之前,我不能选择这个城市作为“旧金山”,因为城市下拉列表是在选择“加利福尼亚”后动态填充的。

How can I submit the city with Python and mechanize? 如何使用Python提交城市并进行机械化?

mechanize doesn't support JavaScript. mechanize不支持JavaScript。 Instead, you should use urllib2 to send the desired values. 相反,您应该使用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...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM