本文整理汇总了Python中mechanize.Browser.close方法的典型用法代码示例。如果您正苦于以下问题:Python Browser.close方法的具体用法?Python Browser.close怎么用?Python Browser.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mechanize.Browser
的用法示例。
在下文中一共展示了Browser.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _login
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import close [as 别名]
def _login(self, username, password):
br = Browser()
br.open("http://netlogin.kuleuven.be/")
br.select_form(name="wayf")
br.submit()
br.select_form(name="netlogin")
br[br.form._pairs()[2][0]]=username
br[br.form._pairs()[3][0]]=password
result = br.submit()
lines = [k for k in result.readlines()]
br.close()
return lines
示例2: fetch_page_after_auth
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import close [as 别名]
def fetch_page_after_auth(username, password, next_url):
print username, password
logout_url = 'https://secure.ninjacourses.com/account/logout/'
login_url = 'https://secure.ninjacourses.com/account/login/?next=%s' % next_url
br = Browser(file_wrapper=ResponseWrapper)
br.set_handle_robots(False)
br.open(logout_url)
br.open(login_url)
br.select_form()
br['username'], br['password'] = username, password
result_page = br.submit().read()
br.close()
if 'correct username' in result_page:
raise ValueError
return result_page
示例3: getFileCoverage
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import close [as 别名]
def getFileCoverage(baseURL, filename):
"""For a given filename, fetch coverage from an online LCOV source."""
covURL = baseURL + filename + ".gcov.html"
mech = Browser()
try:
urlObj = mech.open(covURL)
except:
return ("N/A", "N/A", "N/A", "N/A")
parsedUrlObj = lxml.html.parse(urlObj)
branchCoveragePercent = "N/A"
branchCoverageMissing = "N/A"
lineCoveragePercent = "N/A"
lineCoverageMissing = "N/A"
try:
# Xpath to the coverage, see below
lineCoveragePercent = float(parsedUrlObj.xpath("/html/body/table[1]/tr[3]/td/table/tr[2]/td[7]")[0].text.replace(" %", ""));
# ------------------------------------------------------------------------------------^
# 2 - line coverage
# 3 - function coverage
# 4 - branch coverage
# ------------------------------------------------------------------------------------------^
# 5 - # hit
# 6 - # total
# 7 - % hit
lineCoverageHit = int(parsedUrlObj.xpath("/html/body/table[1]/tr[3]/td/table/tr[2]/td[5]")[0].text)
lineCoverageTotal = int(parsedUrlObj.xpath("/html/body/table[1]/tr[3]/td/table/tr[2]/td[6]")[0].text)
lineCoverageMissing = lineCoverageTotal - lineCoverageHit
except ValueError:
pass
try:
branchCoveragePercent = float(parsedUrlObj.xpath("/html/body/table[1]/tr[3]/td/table/tr[4]/td[7]")[0].text.replace(" %", ""));
branchCoverageHit = int(parsedUrlObj.xpath("/html/body/table[1]/tr[3]/td/table/tr[4]/td[5]")[0].text)
branchCoverageTotal = int(parsedUrlObj.xpath("/html/body/table[1]/tr[3]/td/table/tr[4]/td[6]")[0].text)
branchCoverageMissing = branchCoverageTotal - branchCoverageHit
except ValueError:
pass
mech.close()
return (lineCoveragePercent, lineCoverageMissing, branchCoveragePercent, branchCoverageMissing)
示例4: getCurrentCoverageDirectory
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import close [as 别名]
def getCurrentCoverageDirectory(baseURL):
mech = Browser()
mech.open(baseURL)
currentLink = None
for link in mech.links():
# Find the first directory link that is not the parent
if (link.url.endswith("/") and not link.url.startswith("/")):
currentLink = link
break
if currentLink == None:
mech.close()
raise "Unable to find current coverage directory"
linkURL = currentLink.base_url + currentLink.url
mech.close()
return linkURL
示例5: search
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import close [as 别名]
def search(arg):
assert '/' not in arg # because we use it in a filename
cache = rc['authority_cache']
filename = cache + '/' + arg
if os.path.exists(filename):
return [eval(i) for i in open(filename)]
br = Browser()
br.set_handle_robots(False)
br.open(start)
br.select_form(name="querybox")
br['Search_Arg'] = arg.encode('utf-8')
br['Search_Code'] = ['NHED_']
res = br.submit()
found = list(read_serp(res))
br.close()
out = open(filename, 'w')
for i in found:
print >> out, i
out.close()
return found
示例6: _mapgen_speed_fired
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import close [as 别名]
def _mapgen_speed_fired(self):
test_dir(join(self.dirname, 'gps_vis'))
br = Browser()
# Ignore robots.txt
br.set_handle_robots( False )
# Google demands a user-agent that isn't a robot
br.addheaders = [('User-agent', 'Firefox')]
resp1 = br.open( "http://www.gpsvisualizer.com/map_input" )
# Select the search box and search for 'foo'
br.select_form( name='main' )
br.form['width'] = '870'
br.form['height'] = '600'
br.set_value(['google_openstreetmap'], name='bg_map')
br.set_value(['speed'], name='trk_colorize')
br.form['legend_steps'] = '10'
br.add_file(open(self.filename_converted), "text/plain", self.filename_converted, name='uploaded_file_1')
# Get the search results
resp2 = br.submit()
resp = None
for link in br.links():
siteMatch = re.compile( 'download/' ).search( link.url )
if siteMatch:
resp = br.follow_link( link )
break
# Print the site
content = resp.get_data()
ofile = open(join(self.dirname, 'gps_vis', 'map_speed.html'),'w')
ofile.write(content)
ofile.close()
br.close()
print 'map generated (speed color)'
示例7: hax0r
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import close [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)
示例8: _profilegen_fired
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import close [as 别名]
def _profilegen_fired(self):
test_dir(join(self.dirname, 'gps_vis'))
br = Browser()
# Ignore robots.txt
br.set_handle_robots( False )
# Google demands a user-agent that isn't a robot
br.addheaders = [('User-agent', 'Firefox')]
# Retrieve the Google home page, saving the response
resp1 = br.open( "http://www.gpsvisualizer.com/profile_input" )
# Select the search box and search for 'foo'
br.select_form( name='main' )
br.form['width'] = '870'
br.form['height'] = '250'
br.form['legend_steps'] = '10'
br.add_file(open(self.filename_converted), "text/plain", self.filename_converted, name='uploaded_file_1')
# Get the search results
resp2 = br.submit()
resp = None
for link in br.links():
siteMatch = re.compile( 'download/' ).search( link.url )
if siteMatch:
resp = br.follow_link( link )
break
# Print the site
content = resp.get_data()
ofile = open(join(self.dirname, 'gps_vis', 'profile.png'),'wb')
ofile.write(content)
ofile.close()
br.close()
print 'profile generated'
示例9: _send_fired
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import close [as 别名]
def _send_fired(self):
br = Browser()
# Ignore robots.txt
br.set_handle_robots(False)
# Google demands a user-agent that isn't a robot
br.addheaders = [('User-agent', 'Firefox')]
# Retrieve the Google home page, saving the response
resp = br.open("https://www.t-mobile.cz/.gang/login-url/portal?nexturl=https%3A%2F%2Fwww.t-mobile.cz%2Fweb%2Fcz%2Fosobni")
br.select_form(nr=2)
br.form['username'] = 'kelidas'
br.form['password'] = self.password
resp = br.submit()
# print resp.get_data()
resp = br.open("https://sms.client.tmo.cz/closed.jsp")
br.select_form(nr=1)
# print br.form
# help(br.form)
br.form['recipients'] = self.phone_number # '736639077'#'737451193' #'605348558'
br.form['text'] = self.message
br.form.find_control("confirmation").get("1").selected = self.confirmation
resp = br.submit()
# logout
resp = br.follow_link(url_regex='logout')
br.close()
information(None, 'SMS sent!')
示例10: get_balance
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import close [as 别名]
def get_balance( username, password ):
balance = 0.0
browser = Browser()
try:
browser.open( URL_PATH )
except:
return balance
browser.select_form( nr=0 )
browser.form[USER_FIELD] = username
browser.form[PASS_FIELD] = password
browser.submit()
response = browser.response()
html = response.read()
browser.close()
balance_string = html_filter( html )
result = get_float( balance_string )
return result
示例11: get_balance
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import close [as 别名]
def get_balance( username, password ):
balance = 0.0
browser = Browser()
try:
browser.open( "http://sipnet.ru" )
except:
return balance
browser.select_form( nr=0 )
browser.form['Name'] = username
browser.form['Password'] = password
browser.submit()
response = browser.response()
html = response.read()
browser.close()
balance_string = html_filter( html )
result = get_float( balance_string )
return result
示例12: toggleStatus
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import close [as 别名]
def toggleStatus(list_com):
status = "On.png" if list_com[1] == "/open" else "Off.png"
login = list_com[2]
passwd = list_com[3]
url = 'http://teresinahc.org/wiki/index.php?title=Especial:Autenticar-se&returnto=P%C3%A1gina+principal'
br = Browser()
br.set_handle_robots(False)
br.addheaders = [('User-agent', 'Firefox')]
br.open(url)
if 'Autenticar-se' in br.response().read():
br.select_form('userlogin')
br.form['wpName'] = login
br.form['wpPassword'] = passwd
br.submit()
pag = br.response().read()
if '"wgUserName":null' not in pag:
br.open('http://teresinahc.org/wiki/index.php?title=Status&action=edit')
if 'value="Salvar página"' in br.response().read():
br.select_form('editform')
br.form['wpTextbox1'] = '<center>[[Arquivo:'+status+']]</center>'
br.submit(name='wpSave')
br.close()
if status == 'On.png':
return 'no momento o Teresina Hacker Clube encontra-se ABERTO!'
else:
return 'no momento o Teresina Hacker Clube encontra-se FECHADO!'
else:
br.close()
return 'Voc\xc3\xaa n\xc3\xa3o tem permiss\xc3\xa3o para alterar p\xc3\xa1ginas da Wiki do Teresina Hacker Clube'
else:
br.close()
output = re.compile('<div class="errorbox">(.*?)</div>', re.DOTALL | re.IGNORECASE).findall(pag)
return "</code>"+output[0].replace("<br />", "").replace("\t", "")+"<code>"
else:
br.close()
return 'Desculpe, por algum motivo n\xc3\xa3o foi poss\xc3\xadvel acessar a Wiki do Teresina Hacker Clube.'
示例13: trilegal
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import close [as 别名]
#.........这里部分代码省略.........
myBrowser["eq_alpha"] = str(longitude)
myBrowser["eq_delta"] = str(latitude)
myBrowser["field"] = str(fieldArea)
myBrowser["icm_lim"] = str(passband)
myBrowser["mag_lim"] = str(magnitudeLimit)
myBrowser["mag_res"] = str(magnitudeResolution)
myBrowser["binary_kind"] = [str(int(includeBinaries))]
myBrowser["binary_frac"] = str(binaryFraction)
myBrowser["binary_mrinf"] = str(lowerBinaryMassRatio)
myBrowser["binary_mrsup"] = str(upperBinaryMassRatio)
myBrowser["extinction_kind"] = [str(extinctionType)]
if extinctionType == 1:
myBrowser["extinction_rho_sun"] = str(extinctionValue)
if extinctionType == 2:
myBrowser["extinction_infty"] = str(extinctionValue)
myBrowser["extinction_sigma"] = str(extinctionSigma)
if useThinDisc:
myBrowser["thindisk_kind"] = ["3"]
else:
myBrowser["thindisk_kind"] = ["0"]
if useThickDisc:
myBrowser["thickdisk_kind"] = ["3"]
else:
myBrowser["thickdisk_kind"] = ["0"]
if useBulge:
myBrowser["bulge_kind"] = ["2"]
else:
myBrowser["bulge_kind"] = ["0"]
# Submit the completed form
timestamp = strftime("%a, %d %b %Y %H:%M:%S", gmtime())
print("{0}: Submitting completed TRILEGAL web form".format(timestamp))
nextWebPage = myBrowser.submit()
# Trilegal is now computing the result. Click on the special "Refresh"
# button until the webpage says that the computations are finished.
timestamp = strftime("%a, %d %b %Y %H:%M:%S", gmtime())
print ("{0}: Waiting until TRILEGAL computations are finished".format(timestamp))
myBrowser.select_form(nr=0) # one form on the "be patient" web page
message = "Your job was finished"
while (message not in nextWebPage.read()):
nextWebPage = urlopen(myBrowser.click()) # click on the Refresh button
myBrowser.select_form(nr=0) # select form again, so that we can make a click again
sleep(5) # to not overload the website with refresh requests
# Get the url of the outputfile, and retrieve it. This can take a while.
timestamp = strftime("%a, %d %b %Y %H:%M:%S", gmtime())
print("{0}: Retrieving TRILEGAL output file".format(timestamp))
outputLink = myBrowser.links(url_regex="lgirardi/tmp/output").next()
urlretrieve(outputLink.absolute_url, outputFileName)
myBrowser.close()
# Save the parameters in an info file
parameterInfo = """
coordinateType {0}
longitude {1}
latitude {2}
fieldArea {3}
passband {4}
magnitudeLimit {5}
magnitudeResolution {6}
IMFtype {7}
includeBinaries {8}
binaryFraction {9}
lowerBinaryMassRatio {10}
upperBinaryMassRatio {11}
extinctionType {12}
extinctionValue {13}
extinctionSigma {14}
""".format(coordinateType,
longitude,
latitude,
fieldArea,
passband,
magnitudeLimit,
magnitudeResolution,
IMFtype,
includeBinaries,
binaryFraction,
lowerBinaryMassRatio,
upperBinaryMassRatio,
extinctionType,
extinctionValue,
extinctionSigma)
infoFileName = "info_" + outputFileName
with open(infoFileName, 'w') as infoFile:
infoFile.write(parameterInfo)
示例14: LconnectScraper
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import close [as 别名]
class LconnectScraper(ClassDataScraper):
LCONNECT_URL = 'http://leopardweb.wit.edu/'
USERAGENT = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.1) ' \
+ 'Gecko/20100122 firefox/3.6.1'
def __init__(self):
# Create a cookie jar and a browser
self._cookieJar = LWPCookieJar()
self._browser = Browser()
self._browser.set_cookiejar(self._cookieJar)
# Set Browser options
self._browser.set_handle_equiv(True)
self._browser.set_handle_gzip(True)
self._browser.set_handle_redirect(True)
self._browser.set_handle_referer(True)
self._browser.set_handle_robots(False)
self._browser.set_handle_refresh(_http.HTTPRefreshProcessor(),
max_time=1)
self._browser.addheaders = [('User-agent', LconnectScraper.USERAGENT)]
# Debugging
self._browser.set_debug_http(True)
self._browser.set_debug_redirects(True)
self._browser.set_debug_responses(True)
def getName(self):
return "Lconnect Scraper"
def connect(self):
"""
Attempts to connect to the data source
"""
try:
# Try to open a connection. 8 Second timeout
self._browser.open(LconnectScraper.LCONNECT_URL, timeout=8)
return True
except URLError:
return False
def disconnect(self):
"""
Disconnects from the data source
"""
self._browser.close()
def requiresAuthentication(self):
"""
Returns whether or not the scraper requires authentication information
"""
return True
def authenticate(self, username, password):
"""
Attempts to authenticate the scraper using username and password
"""
# If we're on the sign in page, try to sign in
if self._browser.title() == 'Sign In':
for form in self._browser.forms():
if form.name is None:
self._browser.form = list(self._browser.forms())[0]
self._browser['username'] = username
self._browser['password'] = password
self._browser.submit()
# If the browser's title is 'Main Menu',
# we've either successfully logged in, or we were already
if self._browser.title() == 'Main Menu':
return True
else:
return False
def getClassData(self):
"""
Returns a list of ClassData objects
"""
return []
示例15: __init__
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import close [as 别名]
class RequestQuery:
def __init__(self,config):
self.br=Browser()
self.config = config
# Initialise connections
self.mySiteDB = SiteDBJSON()
self.dbsPhys01 = DbsApi(url = dbs_base_url+"phys01/DBSReader/")
self.dbsPhys02 = DbsApi(url = dbs_base_url+"phys02/DBSReader/")
self.dbsPhys03 = DbsApi(url = dbs_base_url+"phys03/DBSReader/")
def __del__(self):
self.br.close()
def getScramArchByCMSSW(self):
"""
Get from the list of available CMSSW releases
return a dictionary of ScramArchitecture by CMSSW
"""
# Set temporary conection to the server and get the response from cmstags
url = 'https://cmssdt.cern.ch/SDT/cgi-bin/ReleasesXML'
br = Browser()
br.set_handle_robots(False)
response=br.open(url)
soup = BeautifulSoup(response.read())
# Dictionary form
# {'CMSSW_X_X_X':[slc5_amd64_gcc472], ... }
archByCmssw={}
# Fill the dictionary
for arch in soup.find_all('architecture'):
for cmssw in arch.find_all('project'):
# CMSSW release
cmsswLabel = cmssw.get('label').encode('ascii', 'ignore')
if cmsswLabel not in archByCmssw:
archByCmssw[cmsswLabel]=[]
# ScramArch related to this CMSSW release
archName = arch.get('name').encode('ascii', 'ignore')
archByCmssw[cmsswLabel].append(archName)
return archByCmssw
def getDatasetOriginSites(self, dbs_url, data):
"""
Get the origin sites for each block of the dataset.
Return a list block origin sites.
"""
local_dbs = dbs_url.split('/')[5]
if local_dbs == 'phys01':
response = self.dbsPhys01.listBlocks(detail=True,dataset=data)
elif local_dbs == 'phys02':
response = self.dbsPhys02.listBlocks(detail=True,dataset=data)
elif local_dbs == 'phys03':
response = self.dbsPhys03.listBlocks(detail=True,dataset=data)
pnnList = set()
for block in response:
pnnList.add(block['origin_site_name'])
psnList = self.mySiteDB.PNNstoPSNs(pnnList)
return psnList, list(pnnList)
def setGlobalTagFromOrigin(self, dbs_url,input_dataset):
"""
Get the global tag of the dataset from the source dbs url. If it is not set, then set global tag to 'UNKNOWN'
"""
globalTag = ""
local_dbs = dbs_url.split('/')[5]
if local_dbs == 'phys01':
response = self.dbsPhys01.listOutputConfigs(dataset=input_dataset)
elif local_dbs == 'phys02':
response = self.dbsPhys02.listOutputConfigs(dataset=input_dataset)
elif local_dbs == 'phys03':
response = self.dbsPhys03.listOutputConfigs(dataset=input_dataset)
globalTag = response[0]['global_tag']
# GlobalTag cannot be empty
if globalTag == '':
globalTag = 'UNKNOWN'
return globalTag
def isDataAtUrl(self, dbs_url,input_dataset):
"""
Returns True if the dataset is at the dbs url, if not returns False
"""
local_dbs = dbs_url.split('/')[5]
if local_dbs == 'phys01':
response = self.dbsPhys01.listDatasets(dataset=input_dataset)
elif local_dbs == 'phys02':
response = self.dbsPhys02.listDatasets(dataset=input_dataset)
elif local_dbs == 'phys03':
response = self.dbsPhys03.listDatasets(dataset=input_dataset)
# This means that the dataset is not at the url
#.........这里部分代码省略.........