Python机械化如何单击链接

时间:2014-02-10 20:37:06

标签: python mechanize

我需要按下一个包含以下代码的按钮:

"<a href="/follows?followed_id=101769" class="js-toggle-follow btn-social btn-follow     data-method="post" data-remote="true" id="follow" rel="nofollow">"

该按钮没有“名称”,我不知道如何使用“id”找到它,然后点击。

我正在使用带有Mechanize的Python。在coment pulsar enlace中,我想点击按钮,然后转发给下一个用户。

这是剧本:

import mechanize
import cookielib
import sys
import os

url= "http://web.com/users/sign_in"

br = mechanize.Browser()
cj = cookielib.LWPCookieJar()

br.set_cookiejar(cj)
br.set_handle_robots(False)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; es-VE;  rv:1.9.0.1)Gecko/2008071615 Debian/6.0 Firefox/9')]

r=br.open(url)

br.select_form(nr=2)

br.form['user[login]'] = '@hotmail.com'
br.form['user[password]'] = '123123'
os.system('clear')
try:    
print "Loggin....."
br.submit()
except:
print "Logued failed..."
sys.exit()
print "Logued Succesfully!!!"

i=0
ok=0
url77="http://web.com"
enlaceexiste = False
usuarioexiste = False
while i < 100000:

    try: 
    br.open(url77+"/users/"+str(i))
    print "Accessing "+url77+"/users/"+str(i)
    usuarioexiste = True
    except:
    print "User "+str(i)+" failed..."
    usuarioexiste = False
    enlaceexiste = False
    if usuarioexiste:
    try:
        enlace = br.find_link(text='Seguir')
        enlaceexiste = True
            print enlace

    except:
        print "No follow button!"
        enlaceexiste = False

    if enlaceexiste:
    try:
        #pulsar enlace      
        ok+=1
        print "\tTotal users followed and challenged: "+str(ok)
    except:
        print "\tCant follow user "+str(i)
        i+=1
    enlaceexiste = False
   i+=1
print "All users finished!!!"

0 个答案:

没有答案