0

I'm trying to do a script to login into a website and then click on a button on the homepage. So far, I've got this:

import mechanize
import cookielib
#cria um navegador, um browser de codigo...
br = mechanize.Browser()
url = 'http://www.gokano.com' # preencha com seu site joomla
email = 'xxx' # o login utilizado
senha = 'xxx'    # a senha utilizada

# Prepara para tratar cookies...
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

# Ajusta algumas opções do navegador...
br.set_handle_equiv(True)
br.set_handle_gzip(False)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

# Configura o user-agent.
# Do ponto de vista do servidor, o navegador agora é o Firefox.
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)

br.select_form(nr=0)

# Preencher o formulário com os dados de login...
br.form['email'] = email
br.form['password'] = senha

# Enviar o formulário usando o método HTTP POST
br.submit()

# E finalmente, busque o HTML retornado:
html = br.response().read()

Checking the return on html var, I assume that login is going well... but, from this point I don't know how I'm gonna click that button. Here's its DOM:

<a href="http://gokano.com/daily">Collect daily points</a>

can anyone help me? Thanks!

Ronan Lopes
  • 3,320
  • 4
  • 25
  • 51

1 Answers1

1

You can use br.follow_link():

for link in br.links():
  if "gokano.com" in link.url:  # or select your link by whatever criteria
    to_follow = link
    break

br.follow_link(to_follow)

Or if it you know that the link will be the third link on the page, you can use br.follow_link(nr=2)

See here for more on follow_link()

Community
  • 1
  • 1
Will
  • 4,299
  • 5
  • 32
  • 50
  • This is exactly what I was looking for. Thanks!! – Ronan Lopes Dec 22 '16 at 15:36
  • Sure thing :) Here's an interesting (tangentially related) answer about scraping: http://stackoverflow.com/questions/31530335/selenium-webdriver-vs-mechanize – Will Dec 22 '16 at 15:44