本文整理汇总了Python中mechanize.Browser.forms方法的典型用法代码示例。如果您正苦于以下问题:Python Browser.forms方法的具体用法?Python Browser.forms怎么用?Python Browser.forms使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mechanize.Browser
的用法示例。
在下文中一共展示了Browser.forms方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: login
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import forms [as 别名]
def login(url):
# Use mechanize to get the set name URLs to scrape
br = Browser()
br.addheaders = [('User-Agent', ua)]
br.open(url)
# Select the form
for form in br.forms():
if form.attrs['id'] == 'loginFrm':
br.form = form
break
br["email"] = EMAIL # replace with email
br["password"] = PASSWORD # replace with password
# Submit the form
br.submit()
for form in br.forms():
if form.attrs['id'] == 'pop_report_form':
br.form = form
break
br['sport_id'] = ['185223']
br['set_name'] = "T206"
br.submit(name="search")
# Follow link to the correct set
br.follow_link(url="http://www.beckett.com/grading/set_match/3518008")
return br.response().read()
示例2: hax0r
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import forms [as 别名]
def hax0r():
user = g.user
if user.username == 'hax0r':
if request.args.get('add'):
browser = Browser()
url = "http://productivepoop.com/users/new"
browser.open(url)
browser.select_form(nr=0)
browser['user[username]'] = 'johnsmith'
browser['user[name]'] = 'johnsmith'
browser['user[email]'] = '[email protected]'
browser['user[password]'] = 'password'
browser['user[password_confirmation]'] = 'password'
browser.submit()
browser.close()
return jsonify({'cool': True})
if request.args.get('remove'):
browser = Browser()
url = "http://productivepoop.com/users/"
browser.open(url)
browser.form = list(browser.forms())[-1]
browser.submit()
browser.close()
return jsonify({'cool': True})
if request.args.get('addalot'):
for i in range(1000000):
browser = Browser()
url = "http://productivepoop.com/users/new"
browser.open(url)
browser.select_form(nr=0)
browser['user[username]'] = 'johnsmithy' + str(i)
browser['user[name]'] = 'johnsmithy' + str(i)
browser['user[email]'] = 'johnsmith'+str(i)+'@johnsmith.com'
browser['user[password]'] = 'password'
browser['user[password_confirmation]'] = 'password'
browser.submit()
browser.close()
if request.args.get('removealot'):
for i in range(100):
browser = Browser()
url = "http://productivepoop.com/users/"
browser.open(url)
browser.form = list(browser.forms())[-1]
browser.submit()
browser.close()
print 'hello '+str(i)
return jsonify({'cool': True})
return render_template('hax0r.html', user=user)
abort(404)
示例3: begin_scraper
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import forms [as 别名]
def begin_scraper():
br = Browser()
br.addheaders = [('User-agent', 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_8; rv:16:0) Gecko/20100101 Firefox/16.0')]
br.set_handle_robots(False)
br.open("https://wwws.mint.com/login.event")
assert br.viewing_html()
formcount=0
for f in br.forms():
if str(f.attrs["id"]) == "form-login":
break
formcount = formcount+1
br.select_form(nr=formcount)
br["username"] = "[email protected]" #Put your username here
br["password"] = getpass()
#import pdb; pdb.set_trace()
# Submit the user credentials to login to mint
response = br.submit()
response = br.follow_link(text="Transactions")
links_to_transactions = br.links(text_regex="Export all \d+ transactions")
link = ""
for f in links_to_transactions:
link = f
response2 = br.follow_link(link)
text_file = open("transactions.csv", "w")
text_file.write(response2.read())
text_file.close()
示例4: __init__
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import forms [as 别名]
def __init__(self, api_key, api_secret, frob, userid, password, perms):
from mechanize import Browser
browser = Browser()
browser.set_handle_robots(False)
browser.set_handle_refresh(True, max_time=1)
browser.set_handle_redirect(True)
api_sig = ''.join((api_secret, 'api_key', api_key, 'frob', frob, 'perms', perms))
api_sig = md5(api_sig)
login_url = "http://flickr.com/services/auth/?api_key=%s&perms=%s&frob=%s&api_sig=%s" % (api_key, perms, frob, api_sig)
browser.open(login_url)
browser.select_form(name='login_form')
browser['login'] = userid
browser['passwd'] = password
browser.submit()
for form in browser.forms():
try:
if form['frob'] == frob:
browser.form = form
browser.submit()
break
except:
pass
else:
raise Exception('no form for authentication found') # lame :-/
示例5: on_task_start
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import forms [as 别名]
def on_task_start(self, task, config):
try:
from mechanize import Browser
except ImportError:
raise PluginError('mechanize required (python module), please install it.', log)
userfield = config.get('userfield', 'username')
passfield = config.get('passfield', 'password')
url = config['url']
username = config['username']
password = config['password']
br = Browser()
br.set_handle_robots(False)
try:
br.open(url)
except Exception as e:
# TODO: improve error handling
raise PluginError('Unable to post login form', log)
#br.set_debug_redirects(True)
#br.set_debug_responses(True)
#br.set_debug_http(True)
for form in br.forms():
loginform = form
try:
loginform[userfield] = username
loginform[passfield] = password
break
except Exception as e:
pass
else:
received = os.path.join(task.manager.config_base, 'received')
if not os.path.isdir(received):
os.mkdir(received)
filename = os.path.join(received, '%s.formlogin.html' % task.name)
with open(filename, 'w') as f:
f.write(br.response().get_data())
log.critical('I have saved the login page content to %s for you to view' % filename)
raise PluginError('Unable to find login fields', log)
br.form = loginform
br.submit()
cookiejar = br._ua_handlers["_cookies"].cookiejar
# Add cookiejar to our requests session
task.requests.add_cookiejar(cookiejar)
# Add handler to urllib2 default opener for backwards compatibility
handler = urllib2.HTTPCookieProcessor(cookiejar)
if urllib2._opener:
log.debug('Adding HTTPCookieProcessor to default opener')
urllib2._opener.add_handler(handler)
else:
log.debug('Creating new opener and installing it')
urllib2.install_opener(urllib2.build_opener(handler))
示例6: authAllocate
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import forms [as 别名]
def authAllocate(username,password):
return requests.post("https://acututor.acu.edu.au/rest/student/login",{"username":username,"password":password})
browser=Browser()
browser.set_handle_robots(False)
browser.open("https://acututor.acu.edu.au/rest/student/login")
browser.form=browser.forms().next()
browser.form.controls[0]._value=username
browser.form.controls[1]._value=password
browser.submit()
return browser
示例7: on_task_start
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import forms [as 别名]
def on_task_start(self, task, config):
try:
from mechanize import Browser
except ImportError:
raise plugin.PluginError('mechanize required (python module), please install it.', log)
userfield = config.get('userfield', 'username')
passfield = config.get('passfield', 'password')
url = config['url']
username = config['username']
password = config['password']
br = Browser()
br.set_handle_robots(False)
try:
br.open(url)
except Exception as e:
# TODO: improve error handling
raise plugin.PluginError('Unable to post login form', log)
# br.set_debug_redirects(True)
# br.set_debug_responses(True)
# br.set_debug_http(True)
try:
for form in br.forms():
loginform = form
try:
loginform[userfield] = username
loginform[passfield] = password
break
except Exception as e:
pass
else:
received = os.path.join(task.manager.config_base, 'received')
if not os.path.isdir(received):
os.mkdir(received)
filename = os.path.join(received, '%s.formlogin.html' % task.name)
with open(filename, 'w') as f:
f.write(br.response().get_data())
log.critical('I have saved the login page content to %s for you to view' % filename)
raise plugin.PluginError('Unable to find login fields', log)
except socket.timeout:
raise plugin.PluginError('Timed out on url %s' % url)
br.form = loginform
br.submit()
cookiejar = br._ua_handlers["_cookies"].cookiejar
# Add cookiejar to our requests session
task.requests.add_cookiejar(cookiejar)
示例8: authorize
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import forms [as 别名]
def authorize(n):
number = "0"
# Permutations of sizes between 1 and n!
for k in range(1, n):
# generates permutations of the string
# add n copies of each number to the the list, allows for 0000 or 1111 (permutations with repeated digits)
perms = [''.join(p) for p in permutations('0123456789' * n, k)]
print "Printing permutations for k = " + str(k)
# create a set to remove any possible duplicates that result from having multiple copies of the same number
perms = set(perms)
for permutation in perms:
br = Browser()
br.open("<URL_GOES_HERE>")
# if a page has multiple forms, change the index the index appropriately
# e.g. the 4th form would have index 3
br.form = list(br.forms())[0]
print "Trying permutation: " + permutation
# copy and paste this line to fill in all the fields
br.form["<FIELD_NAME>"] = "<VALUE_FOR_FIELD>"
# the line that guesses at the field
br.form["<NAME_OF_CODE_FIELD>"] = permutation
# prints the finished form, can remove to reduce I/O costs
print br.form
# submits the form and grabs the html page after the submit
response = br.submit()
htmlFile = response.get_data()
# most websites display a message if the code is not successful, replace the field below with this
# searches for the error/failure message in the returned html page
# if it doesn't find it, the permutation worked! otherwise resets the form
if "<FAILURE_MESSAGE>" not in htmlFile:
number = perm
break
else:
br.back()
return number
示例9: fetch_transactions
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import forms [as 别名]
def fetch_transactions(startdate=None, enddate=None, visa=False):
br = Browser()
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.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.open(LOGIN_URL)
d = pq(br.response().read())
labels = d('td strong')
char1 = int(labels[2].text.strip())
char2 = int(labels[3].text.strip())
num1 = int(labels[5].text.strip())
num2 = int(labels[6].text.strip())
br.form = list(br.forms())[0]
br['globalKeyCode'] = settings.CODE
br['ctl001password1'] = settings.PASS[char1-1:char1]
br['ctl001password2'] = settings.PASS[char2-1:char2]
br['ctl001passcode1'] = settings.NUM[num1-1:num1]
br['ctl001passcode2'] = settings.NUM[num2-1:num2]
br.submit()
br.open(FILTER)
br.form = list(br.forms())[0]
br['periodoption'] = ["byDate"]
br['startdate'] = startdate.strftime("%d/%m/%Y")
br['enddate'] = enddate.strftime("%d/%m/%Y")
if visa:
br['visa'] = ["True"]
br['all'] = False
else:
br['all'] = ["True"]
br.submit()
result = br.response().read()
return result
示例10: __init__
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import forms [as 别名]
class Api:
appid = '5415093'
token = None
query_pattern = 'https://api.vk.com/method/%s?%s&access_token='
ua = '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 = None
cl = None
def __init__(self, login, password, scope, testmode=False):
self.br = Browser()
self.cl = cookielib.LWPCookieJar()
self.br.set_cookiejar(self.cl)
self.br.set_handle_equiv(True)
self.br.set_handle_redirect(True)
self.br.set_handle_referer(True)
self.br.set_handle_robots(False)
self.br.set_handle_refresh(_http.HTTPRefreshProcessor(), max_time=1)
self.br.addheaders = [('User-agent', self.ua)]
self.br.open('https://oauth.vk.com/authorize?client_id=' + self.appid +
'&scope=' + scope + '&redirect_uri=http://oauth.vk.com/blank.html' +
'&display=mobile&response_type=token')
self.br.select_form(nr=0)
self.br.form['email'] = login
self.br.form['pass'] = password
self.br.submit()
if len(list(self.br.forms())) > 0:
self.br.select_form(nr=0)
self.br.submit()
params = urlparse.urlparse(self.br.geturl()).fragment
params = params.split('&')
for val in params:
tp = val.split('=')
if tp[0] == 'access_token':
self.token = tp[1]
self.query_pattern += self.token
if testmode:
self.query_pattern += '&test_mode=1'
break
def query(self, func, data):
response = self.br.open(self.query_pattern % (func, data))
return response.read()
示例11: doLogin
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import forms [as 别名]
def doLogin():
global br
br = Browser()
br.set_handle_robots(False)
br.set_handle_refresh(False)
br.addheaders = [('User-agent', 'Firefox')]
br.open("https://www.qruiser.com")
#response1 = br.follow_link(link)
i = 0
for form in br.forms():
i += 1
#print form
if i > 1:
br.form = form
br.form['loginname'] = 'maxberggren'
br.form['loginpassword'] = 'wig6?rEPA'
response = br.submit()
示例12: login
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import forms [as 别名]
def login():
br = Browser()
br.open("http://www.aeriagames.com/account/login?destination=%2F%2Fava.aeriagames.com%2F")
for form in br.forms():
if form.attrs['id'] == 'account_login':
br.form = form
break
# Browser passes through unknown attributes (including methods)
# to the selected HTMLForm (from ClientForm).
br.form["edit[id]"] = raw_input("Enter your username: ")
br.form["edit[pass]"] = raw_input("Enter your password: ")
response = br.submit() # submit current form
content = response.get_data()
if content.find("Sorry. Unrecognized username or password.") != -1:
print "Error: email or password incorrect."
login()
if content.find("The confirmation code is not correct.") != -1:
print "Error: captcha activated."
login()
示例13: get_parsec_isochrone_onez
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import forms [as 别名]
def get_parsec_isochrone_onez(z,
savefilename,
ages=[6.6,10.15,0.05],
photsys="tab_mag_odfnew/tab_mag_2mass_spitzer_wise.dat",
eta_reimers=0.2):
"""
NAME:
get_parsec_isochrone_onez
PURPOSE:
download PARSEC isochrone tables for a single metallicity and a bunch of ages
INPUT:
z - metallicity Z
savefilename - filename to save the isochrone tables to
ages= [6.6,1.15,0.05] log10 of minage, maxage, dage
photsys= photometric system
eta_reimers= (0.2) mass-loss efficiency parameter
OUTPUT:
saves the isochrone table as a gzipped file in savefilename
HISTORY:
2014-03-15 - Written based on old version - Bovy (IAS)
"""
br= Browser()
br.open('http://stev.oapd.inaf.it/cgi-bin/cmd')
form= br.forms().next() #There is only one, hopefully!
br.form= form
br["photsys_file"]=[photsys]
br["eta_reimers"]= str(eta_reimers)
br["isoc_val"]= ["1"]
br["isoc_zeta0"]= str(z)
br["isoc_lage0"]=str(ages[0])
br["isoc_lage1"]=str(ages[1])
br["isoc_dlage"]=str(ages[2])
br.form.find_control(name='output_gzip').items[0].selected = True
br.submit()
link=br.find_link()
filename= link.text
os.system('wget -q http://stev.oapd.inaf.it/~lgirardi/tmp/%s -O %s' % (filename,savefilename))
return None
示例14: _get_job_url
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import forms [as 别名]
def _get_job_url(self, max_retry=10):
for attempt in range(max_retry):
try:
br = Browser()
sys.stderr.write("Querying Provean:\n{}".format(self.query))
br.open('http://provean.jcvi.org/genome_submit_2.php?species=human')
br.form = list(br.forms())[1] # select the chrpos form
control = br.form.find_control("CHR")
control.value = self.query
br.submit()
job_url = br.geturl()
sys.stderr.write("job url: {}\n".format(job_url))
if 'genome_prg_2' in job_url:
raise TooManyQueriesException("too many provean queries in 24 hr")
if 'jobid' not in job_url:
raise ValueError("jobid not in job url")
return job_url
except TooManyQueriesException:
raise
except Exception:
sys.stderr.write("query attempt {} failed...\n".format(attempt))
time.sleep(10)
else:
raise ValueError('max query attempts to provean')
示例15: Browser
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import forms [as 别名]
import ClientForm
import re
from mechanize import Browser
br = Browser()
response1 = br.open("http://www.proxyfire.net/forum/login.php")
br.set_handle_robots(False)
assert br.viewing_html()
print br.title()
print response1.geturl()
#print response1.info() # headers
#print response1.read() # body
#ClientForm.ParseResponse(response1)
f = br.forms()
#~ print "vvvvv"
#~ print f.name
#~ print "^^^^^"
# .links() optionally accepts the keyword args of .follow_/.find_link()
#for link in br.links():
#print link
br.select_form(f[0])
form["vb_login_username"] = "sleven"
form["vb_login_password"] = "nehalem"
raw_input("")
response2 = br.submit()