我正在玩机械化 试图为我的网站蛮力 我只是想知道如何检查表单是否已成功提交
到目前为止:
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)
答案 0 :(得分: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
希望这有帮助