本文整理汇总了Python中mechanize.Browser.form方法的典型用法代码示例。如果您正苦于以下问题:Python Browser.form方法的具体用法?Python Browser.form怎么用?Python Browser.form使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mechanize.Browser
的用法示例。
在下文中一共展示了Browser.form方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: login
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import form [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 form [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: __init__
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import form [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 :-/
示例4: get_browser
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import form [as 别名]
def get_browser(self):
"""
Each FAUrl object stores it's own browser instance. On the first call
it is created and if the username and password is set it will
authenticate you.
:return: mechanize.Browser instance.
:raise: FAiler.FAError if FA is down. Time to F5!
:raise: FAiler.FAAuth Your username and password failed
"""
if self._br is None:
br = Browser()
br.set_handle_robots(False)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_equiv(True)
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')]
if self._username is not None and self._password is not None:
loginPage = 'https://www.furaffinity.net/login'
try:
br.open(loginPage)
except urllib2.HTTPError:
raise FAError("FA's down, F5 time.")
br.form = br.global_form()
br.form['name'] = self._username
br.form['pass'] = self._password
br.form.method = 'POST'
br.submit()
if br.geturl() == loginPage + '/?msg=1':
raise FAAuth('Username & Password Incorrect')
self._br = br
return self._br
示例5: on_task_start
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import form [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 form [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 form [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 form [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 form [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: doLogin
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import form [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()
示例11: login
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import form [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()
示例12: get_parsec_isochrone_onez
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import form [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
示例13: _get_job_url
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import form [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')
示例14: generate_json
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import form [as 别名]
def generate_json(vnx_monitor):
"""This function will connect to the VNX web server, parse data and store the output in vnx_monitor.json"""
try:
# Create Mechanize browser
browser = Browser()
# Open the main VNX-Reporter login page and login
browser.open(VNX_REPORTER_WEBSERVER)
# Note that the login form doesn't have a name, but we locate it by ID
for form in browser.forms():
if form.attrs['id'] == 'login-form':
browser.form = form
break
browser["j_username"] = VNX_REPORTER_USERNAME
browser["j_password"] = VNX_REPORTER_PASSWORD
browser.submit()
#
# Now that we are logged in, we can get the page we really want.
#
reply = browser.open(VNX_REPORT_URL)
perf_data = reply.read()
read_csv = csv.reader(StringIO.StringIO(perf_data))
# The file lists several LUNs with a timestamp and IOPs count. Add up a total IOPS for the pool.
timestamp = datetime.now()
for row in read_csv:
# The CSV module will parse each line into an array with the columns
if len(row) > 1: # Skip blank rows
if row[0] == "VNXCS0": # Skip any row without the correct array
pool = row[2]
pool = pool.replace("_Pool","") # Remove the word _POOL from pool names
iops = int(float(row[11]))
vnx_monitor.datapoints.append({"pool":pool, "iops":iops, "timestamp":timestamp})
# Each storage pool will be stored as dict with all the datapoints in this array
statusbar_datasequences = []
# Organize the datapoints by pool
pool_datapoints = defaultdict(list)
for datapoint in vnx_monitor.datapoints:
title = datapoint['timestamp'].strftime("%H:%M")
value = datapoint['iops']
pool_datapoints[(datapoint['pool'])].append({"title": title, "value":value})
# Format raw data as a dictionary for JSON consumption
for pool in pool_datapoints:
statusbar_datasequences.append({"title": pool, "datapoints": pool_datapoints[pool]})
# If we already have the max number of datapoints, delete the oldest item.
pool_count = len(pool_datapoints) # we will delete one datapoint for each pool
if len(vnx_monitor.datapoints) >= (MAX_DATAPOINTS * pool_count):
for i in range(0,pool_count):
del (vnx_monitor.datapoints[0])
# Generate JSON output and assign to snmp_monitor object (for return back to caller module)
statusbar_graph = {
"title": GRAPH_TITLE, "type": "line",
"refreshEveryNSeconds": SAMPLE_INTERVAL,
"datasequences": statusbar_datasequences
}
statusbar_type = {"graph": statusbar_graph}
vnx_monitor.json = json.dumps(statusbar_type)
except Exception as error:
vnx_monitor.json= output_message("Error in VNX_Reporter_Pool_IO", error.message)
if __debug__:
print vnx_monitor.json
示例15: add_provean_info
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import form [as 别名]
def add_provean_info(records, max_retry=30, rest_server='http://grch37.rest.ensembl.org',
provean_script='provean.sh', qsub_script='perl modules/qsub.pl', qsub_queue='jrf.q,all.q',
mem_per_thread='1.5G', num_provean_threads=4):
""" add provean results using remote server or locally if necessary
"""
query = ""
for record in records:
query += "{},{},{},{}\n".format(record.CHROM, record.POS, record.REF, record.ALT[0])
# get the job URL
job_url = None
for attempt in range(max_retry):
try:
br = Browser()
sys.stderr.write("Querying Provean:\n{}".format(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 = query
br.submit()
job_url = br.geturl()
sys.stderr.write("job url: {}\n".format(job_url))
if 'jobid' not in job_url:
raise Exception("jobid not in job url")
break
except:
sys.stderr.write("query attempt {} failed...\n".format(attempt))
time.sleep(10)
else:
sys.stderr.write('max query attempts\n')
break
# parse job result page
if job_url is not None:
for attempt in range(max_retry):
try:
page = urllib2.urlopen(job_url).read()
soup = BeautifulSoup(page, 'html.parser')
link = soup.find('a', href=re.compile('one\.tsv'))
url = 'http://provean.jcvi.org/' + link.get('href')
df = pd.read_table(url)
break
except:
sys.stderr.write("attempt {} failed...\n".format(attempt))
time.sleep(10)
else:
sys.stderr.write('max attempts\n')
return None
for idx, record in enumerate(records):
if df.ix[idx, 'PROTEIN_ID'] != 'record not found':
record.INFO['provean_protein_id'] = df.ix[idx, 'PROTEIN_ID']
record.INFO['provean_pred'] = df.ix[idx, 'PREDICTION (cutoff=-2.5)']
record.INFO['provean_score'] = df.ix[idx, 'SCORE']
else:
record.INFO['provean_protein_id'] = '.'
record.INFO['provean_pred'] = '.'
record.INFO['provean_score'] = '.'
else:
raise(Exception("Local Provean not implemented."))
for record in records:
add_provean_info_local(record, rest_server=rest_server, provean_script=provean_script,
qsub_script=qsub_script, qsub_queue=qsub_queue, mem_per_thread=mem_per_thread,
num_provean_threads=num_provean_threads)
return records