如何使用Python选择不在表单内的选项?

时间:2016-01-16 14:45:35

标签: 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">Value 2</option>
</select>

2 个答案:

答案 0 :(得分:0)

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

更新:不确定您是否已解决此问题。我刚刚为我做了以下工作,并相信它也适合你:

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

答案 1 :(得分:0)

# 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']