2017-06-12 93 views
0

我必須點擊我發現的鏈接,但是當我運行該腳本時,它會點擊第一個鏈接,我該如何修復它? 進口機械化點擊第一個鏈接mechanize python

BR = mechanize.Browser()

br.addheaders = [( 「用戶代理」, 「瀏覽器」)]

br.set_handle_robots(假)

而True:

kaynak = br.open("https://temp-mail.org/en/option/change/") 

br.select_form(nr=0) 

email = br.form.find_control(id='mail', name='mail') 

email.value = "something" 

submit = br.submit(nr=0) 

go = br.open("https://temp-mail.org/en/option/refresh/") 

target_text = 'Facebook' 

for link in br.links(): 
    print(link) 
    print(link.text) 

    if link.text == target_text: 
     print('match found') 
     break 

    br.follow_link(link) 
    print(br.geturl()) 

回答

0

您的indentaion已關閉。您的for循環現在遵循它找到的每個鏈接,而不僅僅是匹配的鏈接。或者在break之前或在for循環之後將您的關注聲明放在if之內:

for link in br.links(): 
    print(link) 
    print(link.text) 

    if link.text == target_text: 
     print('match found') 
     # Here... 
     br.follow_link(link) 
     print(br.geturl()) 
     break 

# Or here... 
br.follow_link(link) 
print(br.geturl())