2016-12-22 90 views
0

我想做一個腳本登錄到一個網站,然後點擊主頁上的一個按鈕。到目前爲止,我有這樣的:Python機械化 - 登錄後點擊按鈕

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

檢查上的HTML變種的回報,我認爲登錄是順利......但是,從這個角度,我不知道我怎麼我會點擊該按鈕。這是它的DOM:

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

任何人都可以幫我嗎?謝謝!

回答

1

您可以使用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) 

或者,如果你知道該鏈接將頁面上的第三個鏈接,你可以使用br.follow_link(nr=2)

See here更多關於follow_link()

+0

這正是我所期待的。謝謝!! –

+0

肯定的東西:)這裏有一個有趣的(切線相關)關於抓取的答案:http://stackoverflow.com/questions/31530335/selenium-webdriver-vs-mechanize – Will