如何使用Python选择表单之外的选项?

如何使用Python选择表单之外的选项?,python,python-2.7,mechanize,mechanize-python,Python,Python 2.7,Mechanize,Mechanize Python,我想知道如何使用Mechanize选择不在表单中的选项菜单? 我获取这些数据的页面在div中有选择选项,而不是在表单上 <select name="name" onchange="someJavaScript;"> <option value="-1">Value -1</option> <option value="1">Value 1</option> <option value="2">Valu

我想知道如何使用Mechanize选择不在表单中的选项菜单? 我获取这些数据的页面在div中有选择选项,而不是在表单上

<select name="name" onchange="someJavaScript;">
    <option value="-1">Value -1</option>
    <option value="1">Value 1</option>
    <option value="2">Value 2</option>
</select>

值-1
值1
价值2

您介意为我们提供该网站的链接吗

更新:不确定是否已解决问题。我刚刚得到了以下工作,我相信它也应该适合你:

# -*- coding: utf-8 -*- 
import mechanize

#-----------------------------------------------------------------------|
# The following fixes error "URLError: <urlopen error [Errno 8] _ssl.c:503".
# Include if necessary
import ssl
from functools import wraps
def sslwrap(func):
    @wraps(func)
    def bar(*args, **kw):
        kw['ssl_version'] = ssl.PROTOCOL_TLSv1
        return func(*args, **kw)
    return bar

ssl.wrap_socket = sslwrap(ssl.wrap_socket)
# -----------------------------------------------------------------------|

URL = 'https://search.yahoo.com/'
BASE_URL = 'https://search.yahoo.com/'

# The form/controls you'd like to work on, which can be extracted from source by clicking "Inspect" on the page
FORM_HTML = """
<form method="get" name="s" id="sf" role="search" action="https://search.yahoo.com/search;_ylt=AwrBTveB2p9Wj.IAEDDqFAx." accept-charset="utf-8"><label for="yschsp" class="off-left">Search query</label><div id="sbq-wrap" class="sbq-w"><input type="text" class="sbq" id="yschsp" name="p" value="" autocomplete="off" autofocus="" tabindex="1" autocorrect="off" autocapitalize="off" style="-webkit-tap-highlight-color: transparent;"><div id="yui_3_10_0_1_1453316701540_15" class="sa-sbx-container"><div id="yui_3_10_0_1_1453316701540_18" class="sa lowlight"></div></div></div><input type="submit" class="sbb" value="Search" tabindex="2"><input type="hidden" name="fr" value="sfp"><input type="hidden" name="fr2" value=""><input type="hidden" name="iscqry" id="iscqry"></form>
"""
br = mechanize.Browser()
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')]
br.open(URL)

# work on the FORM_HTML
res  = mechanize._form.ParseString(FORM_HTML, BASE_URL)

# get the HTMLForm instance
br.form = res[1]
form = br.form

# -----------------------------------------------
# Refer to: http://stackoverflow.com/questions/13965285/use-mechanize-to-submit-form-without-control-name
# In case the control's name is NONE:
form.set_value('stackover', type= 'text', id='yschsp')

# -----------------------------------------------
# In case the control has name 'p', use the following to set the value:
#form['p'] = 'stackoverflow'

# submit the form
response = mechanize.urlopen(form.click())

# print the page content
print response.read()
#-*-编码:utf-8-*-
进口机械化
#-----------------------------------------------------------------------|
#以下修复了错误“URLError:
#我没有页面源代码。希望有帮助。
html=”“”
值-1
值1
价值2
"""
#在上面的字符串上工作
forms=mechanize.ParseString(html,“假”)
#这里只有一个表单,表单是HTMLForm实例
表格=表格[0]
控件=窗体。控件[0]#选择
#控件“名称”的所有值
打印[control.items中的项的[item.attrs['value']#['-1','1','2']
#此控件的当前值
打印表格['name']#['-1']
#选择一个选项(例如值为“2”的选项)
表格['name']=['2']
#验证该值
打印表格['name']#['2']

不能,但这是html的图像,其中选择标记只是为了使其正确…你是说我可以选择一个表单来使用选择选项,即使选择选项不在标记表单中?是的,你是对的。如果没有使用Mechanize可以找到的表单,我建议你尝试使用Beaut解析/发布数据因为mechanize只查看静态HTML页面上的表单字段,因此填充/提交。
# I don't have the page source. Hope it helps.
html = """<select name="name" onchange="someJavaScript;">
    <option value="-1">Value -1</option>
    <option value="1">Value 1</option>
    <option value="2">Value 2</option>
</select>"""

# work on the string above
forms  = mechanize.ParseString(html, 'fake')

# there is only one form here, form is HTMLForm instance
form = forms[0]

control = form.controls[0] # the select

# All values for control 'name'
print [item.attrs['value'] for item in control.items]   # ['-1','1','2']

# The current value for this control
print form['name']   # ['-1']

# Select a choice (e.g. choice with value '2')
form['name'] = ['2']

# Verify the value
print form['name']   # ['2']