本文整理汇总了Python中mechanize.Browser.set_response方法的典型用法代码示例。如果您正苦于以下问题:Python Browser.set_response方法的具体用法?Python Browser.set_response怎么用?Python Browser.set_response使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mechanize.Browser
的用法示例。
在下文中一共展示了Browser.set_response方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: submit_form
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import set_response [as 别名]
def submit_form():
br = Browser() # create browser instance
response = br.open(scraper_url) # load page
# hack
rp_data = response.get_data()
rp_data = re.sub(r'<optgroup label=".+">', "", rp_data) # replace all optgroup elements
response.set_data(rp_data)
br.set_response(response)
# eohack
br.select_form(name='form_spar_topz')
# fill in custom values
br['from'] = from_city
br['to_spar'] = to_city
br.form.find_control('fromDate').readonly = False
br['fromDate'] = on_date
br['fromTime'] = at_time
return br.submit()
示例2: main
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import set_response [as 别名]
def main(ra, senha, arquivos):
if not ra:
ra = raw_input("Digite seu RA: ")
if not senha:
senha = getpass.getpass("Senha: ")
br = Browser()
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.addheaders = [('User-agent',
'Mozilla/5.0 (X11; Linux x86_64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1')]
#link = 'https://progradweb.ufscar.br/progradweb/servlet/Superior'
link = 'http://progradweb.ufscar.br:8080/progradweb/servlet/Superior'
br.open(link)
br.select_form(name="fSuperior")
br.form["Usuario"] = ra
br.form["sess"] = senha
br.submit()
br.select_form(name="fPrincipal")
resp = br.submit()
#Corrige nested FORMs
soup = BeautifulSoup(resp.get_data())
resp.set_data(soup.prettify())
br.set_response(resp)
br.select_form(name="fHistorico")
pagina = br.submit()
data = pagina.get_data()
# Possui mais de 1 enfase?
if data.find("Clique em uma das ênfases abaixo para ver o") != -1:
links = list(br.links(url_regex=re.compile(r"^javascript:submita")))
print 'Enfases:'
for index, link in enumerate(links, start=1):
print '({}) - {}'.format(index, link.text)
n = int(raw_input("Digite o numero da enfase: "))
pattern = re.compile(r'''
javascript:submita\(\'
(\d*)\',%20\'
(\d*)\',%20\'
(\d)
''', re.VERBOSE)
enfase, ano, semestre = pattern.search(links[n - 1].url).groups()
br.back()
br.select_form(name="fHistorico")
br.form.new_control('text', 'RA', {'value': ra})
br.form.new_control('text', 'Enfase', {'value': enfase})
br.form.new_control('text', 'AnoIni', {'value': ano})
br.form.new_control('text', 'SemIni', {'value': semestre})
br.form.new_control('text', 'Tipo', {'value': '1'})
br.form.new_control('text', 'MaisEnfase', {'value': 'S'})
br.form.new_control('text', 'Modo', {'value': '2'})
br.form.new_control('text', 'CodigoCurso', {'value': ''})
br.form.new_control('text', 'Certificado', {'value': '0'})
br.form.new_control('text', 'Consulta', {'value': '0'})
br.form.new_control('text', 'sC', {'value': '51'})
br.form.fixup()
pagina = br.submit()
html = BeautifulSoup(pagina.get_data())
linhas = html.findAll('tr')
creditos_aprovados = 0
creditos_solicitados = 0
creditos_desistentes = 0
creditos_trancados = 0
creditos_reprovados = 0
nota_ponderada = 0
if arquivos:
materias = []
for lin in linhas:
if len(lin) == 21:
materia = lin.findAll('td')
nome = materia[2].text.encode('utf-8')
nota = materia[3].text
if nota == ' ':
nota = 0
nota = float(nota)
resultado = materia[5].text
creditos = int(materia[6].text)
if arquivos:
#.........这里部分代码省略.........
示例3: day
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import set_response [as 别名]
</div>
<div class="radio">
<label for="radios-1">
<input type="radio" name="duration" id="radios-1" value="2">
1 day (hostel zone)
</label>
</div>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="approveBtn"></label>
<div class="col-md-4">
<button id="approveBtn" name="approveBtn" class="btn btn-primary">Authorize</button>
</div>
</div>
</fieldset>
</form>
'''
resp.set_data( response )
br.set_response( resp )
# Filling the form using the response
br.select_form( nr = 0 )
# Set 1 for one hour and 2 for one day
br.form[ 'duration' ] = [ '2' ]
response = br.submit()
print "I put your Netaccess"
示例4: login
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import set_response [as 别名]
def login(roll, password):
# New browser object
br = Browser()
#Open Login page
print "Logging in"
page = br.open( 'https://netaccess.iitm.ac.in/account/login' )
br.select_form( nr = 0 )
# Fill in the fields
br.form[ "userLogin" ] = roll
br.form[ "userPassword" ] = password
# Submit to login
br.submit()
print "Approving your IP"
# Directly go to the approve page. Cookies taken care of by Mechanize browser
resp = br.open( 'https://netaccess.iitm.ac.in/account/approve' )
# Hardcoded response due to bad HTML on site
hardcoded_resp = '''
<form class="form-horizontal" method="post" action="/account/approve">
<fieldset>
<!-- Form Name -->
<legend>Authorization</legend>
<!-- Multiple Radios -->
<div class="form-group">
<label class="col-md-4 control-label" for="radios">Duration</label>
<div class="col-md-4">
<div class="radio">
<label for="radios-0">
<input type="radio" name="duration" id="radios-0" value="1" checked="checked">
60 minutes (recommended for public machines)
</label>
</div>
<div class="radio">
<label for="radios-1">
<input type="radio" name="duration" id="radios-1" value="2">
1 day (hostel zone)
</label>
</div>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="approveBtn"></label>
<div class="col-md-4">
<button id="approveBtn" name="approveBtn" class="btn btn-primary">Authorize</button>
</div>
</div>
</fieldset>
</form>
'''
# Setting response for Page as the hardcoded response.
resp.set_data( hardcoded_resp )
br.set_response( resp )
# Filling the form using the hardcoded response
br.select_form( nr = 0 )
# Set duration as 1 for one hour and 2 for one day
br.form[ 'duration' ] = [ '2' ]
response = br.submit()
print "Logged in"
f = open('/home/kevinselvaprasanna/netaccess-indicator/netaccess.html', 'w')
f.write(response.read())
f.close()