2

我正在玩机械化尝试为我的网站创建蛮力我只是想知道如何检查表单是否已成功提交

至今:

import mechanize
import cookielib
import urllib
import HTMLParser
import re
# Browser
br = mechanize.Browser()

# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

url = 'http://mywebsite'
pattern = '''Welcome (.*)'''

passwords = ('lol','lol1','correctPassword')

r = br.open(url)
try:
    br.select_form(nr=0)
    br.form['username']='Matt'
    found = False
    i=0
    while not found:
        br.form['password']=passwords[i]
        # here I want to check if br.submit() was successful 
        # than break the loop (or simply make found = True)
        # if not, increment i
        br.submit()
except:
    print "Not Good"
html = br.response().read()
message = re.findall(pattern, html)

parser = HTMLParser.HTMLParser()
print parser.unescape(message)
4

1 回答 1

2

我有一个类似的问题。并且没有关于它的文档。如果还不知道。我建议你使用 br.click()

我通过查看网页上的错误元素来检查错误。像这样的东西

request = br.click(name='login')
 response = br.open(request)
 mainpage = response.read()
 if mainpage.find('<the error text>')==-1
        #print "Could not login"
        success = True

希望这可以帮助

于 2012-09-28T22:50:41.833 回答