本文整理汇总了Python中twill.commands.get_browser函数的典型用法代码示例。如果您正苦于以下问题:Python get_browser函数的具体用法?Python get_browser怎么用?Python get_browser使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_browser函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: visit_loginurl
def visit_loginurl (aggregate):
"""Check for a login URL and visit it."""
config = aggregate.config
url = config["loginurl"]
if not url:
return
if not fileutil.has_module("twill"):
msg = strformat.format_feature_warning(module=u'twill',
feature=u'login URL visit',
url=u'http://twill.idyll.org/')
log.warn(LOG_CHECK, msg)
return
from twill import commands as tc
log.debug(LOG_CHECK, u"Visiting login URL %s", url)
configure_twill(tc)
tc.go(url)
if tc.get_browser().get_code() != 200:
log.warn(LOG_CHECK, _("Error visiting login URL %(url)s.") % \
{"url": url})
return
submit_login_form(config, url, tc)
if tc.get_browser().get_code() != 200:
log.warn(LOG_CHECK, _("Error posting form at login URL %(url)s.") % \
{"url": url})
return
#XXX store_cookies(tc.get_browser().cj, aggregate.cookies, url)
resulturl = tc.get_browser().get_url()
log.debug(LOG_CHECK, u"URL after POST is %s" % resulturl)
# add result URL to check list
from ..checker import get_url_from
aggregate.urlqueue.put(get_url_from(resulturl, 0, aggregate))
示例2: require
def require(what):
"""
>> require <what>
After each page is loaded, require that 'what' be satisfied. 'what'
can be:
* 'success' -- HTTP return code is 200
* 'links_ok' -- all of the links on the page load OK (see 'check_links'
extension module)
"""
global _requirements
from twill import commands
#
# install the post-load hook function.
#
if _require_post_load_hook not in commands.get_browser()._post_load_hooks:
if DEBUG:
print>>commands.OUT, 'INSTALLING POST-LOAD HOOK'
commands.get_browser()._post_load_hooks.append(_require_post_load_hook)
#
# add the requirement.
#
if what not in _requirements:
if DEBUG:
print>>commands.OUT, 'Adding requirement', what
_requirements.append(what)
示例3: download_project_export
def download_project_export():
url = get_browser().get_url()
assert url.endswith(".zip")
zipcontents = get_browser().get_html()
output = StringIO()
output.write(zipcontents)
z = zipfile.ZipFile(output, "r")
name = url.split("/")[-1]
globals, locals = get_twill_glocals()
globals["__project_export__"] = (z, name)
示例4: fetch_disease_model
def fetch_disease_model(id):
from twill import set_output
set_output(open("/dev/null", "w"))
dismod_server_login()
twc.go(DISMOD_DOWNLOAD_URL % id)
result_json = twc.show()
twc.get_browser()._browser._response.close() # end the connection, so that apache doesn't get upset
dm = DiseaseJson(result_json)
return dm
示例5: no_require
def no_require():
"""
>> no_require
Remove all post-load requirements.
"""
from twill import commands
l = commands.get_browser()._post_load_hooks
l = [ fn for fn in l if fn != _require_post_load_hook ]
commands.get_browser()._post_load_hooks = l
global _requirements
_requirements = []
示例6: wyloguj
def wyloguj(commands):
forms = commands.showforms() # Pobranie formularzy
naszForm = None # Zmienna do ktorej zapiszemy znaleziony przez nas form
for form in forms: # Petla po formualrzach
if form.action == 'https://edukacja.pwr.wroc.pl/EdukacjaWeb/logOutUser.do': # Jesli akcja danego formularza przenosi do szczegolow
naszForm = form # To zapisujemy znaleziony formularz
#print(naszForm) # DO USUNIECIA! Wypisuje znaleziony formularz
ctrl = naszForm.controls # pobieram ze znalezionego formularza wszystkie kontrolki
for ct in ctrl:
if ct.type == 'submit': # szukam wsrod niej tej co ma typ submit
commands.get_browser().clicked(naszForm, ct.attrs['name']) # klikam na ten przycisk
commands.get_browser().submit()
示例7: _run_xpath
def _run_xpath(xpath):
_, twill_locals = get_twill_glocals()
browser = get_browser()
html = browser.get_html()
tree = lxml.html.document_fromstring(html)
try:
results = tree.xpath(xpath)
except XPathEvalError:
err_msg = "Invalid xpath expression: '%s'" % xpath
log_error(err_msg)
raise TwillException(err_msg)
# XXX we aggregate all the values together and warn when there is more than
# one result
if results:
if len(results) > 1:
log_warn("xpath '%s' found multiple results: using all of them" % xpath)
result = "\n".join(lxml.html.tostring(r) for r in results)
else:
log_error("xpath '%s' found no results")
result = ""
# in case we want to cache it at some point
twill_locals["__xpath_result__"] = result
twill_locals["__xpath_expr__"] = xpath
return result
示例8: test_tidy
def test_tidy():
"""
test parsing of tidy-processed HTML.
"""
b = commands.get_browser()
commands.config('use_tidy', '1')
commands.config('use_BeautifulSoup', '0')
commands.config('allow_parse_errors', '0')
commands.go(url)
###
commands.go('/tidy_fixable_html')
forms = [ i for i in b._browser.forms() ]
assert len(forms) == 1, \
"you must have 'tidy' installed for this test to pass"
###
commands.go('/BS_fixable_html')
forms = [ i for i in b._browser.forms() ]
assert len(forms) == 1, \
"there should be one mangled form on this page"
示例9: test_BeautifulSoup
def test_BeautifulSoup():
"""
test parsing of BS-processed HTML.
"""
b = commands.get_browser()
commands.config('use_tidy', '0')
commands.config('use_BeautifulSoup', '1')
commands.config('allow_parse_errors', '0')
commands.go(url)
###
commands.go('/tidy_fixable_html')
forms = [ i for i in b._browser.forms() ]
assert len(forms) == 0, \
"there should be no correct forms on this page"
###
commands.go('/BS_fixable_html')
forms = [ i for i in b._browser.forms() ]
assert len(forms) == 1, \
"there should be one mangled form on this page"
示例10: test_raw
def test_raw():
"""
test parsing of raw, unfixed HTML.
"""
b = commands.get_browser()
commands.config('use_tidy', '0')
commands.config('use_BeautifulSoup', '0')
commands.config('allow_parse_errors', '0')
commands.go(url)
###
# Apparently, mechanize is more tolerant than it used to be.
# commands.go('/tidy_fixable_html')
# forms = [ i for i in b._browser.forms() ]
# logging.info("forms: %s", forms)
# assert len(forms) == 0, "there should be no correct forms on this page"
###
commands.go('/BS_fixable_html')
forms = [ i for i in b._browser.forms() ]
assert len(forms) == 1, "there should be one mangled form on this page"
示例11: authAndRedirect
def authAndRedirect(username, password):
tw.reset_browser()
tw.go(SYS_REDIRECT_URL)
tw.fv('1', "username", username)
tw.fv('1', "password", password)
tw.formaction('1', AUTH_URL)
tw.submit()
return tw.get_browser().get_html()
示例12: url_qs
def url_qs(what):
browser = get_browser()
qs = urllib.splitquery(browser.get_url())[-1]
qs = qs.split('&')
qsdict = {}
for q in qs:
q = q.split('=')
qsdict[q[0]] = q[1]
if what not in qsdict:
raise TwillAssertionError("no match to '%s' in %s" % (what, qs))
示例13: split
def split(what):
"""
>> split <regexp>
Sets __matchlist__ to re.split(regexp, page).
"""
page = get_browser().get_html()
m = re.split(what, page)
global_dict, local_dict = get_twill_glocals()
local_dict['__matchlist__'] = m
示例14: try_posting_disease_model
def try_posting_disease_model(dm, ntries):
# error handling: in case post fails try again, but stop after 3 tries
from twill.errors import TwillAssertionError
import random
import time
url = ""
for ii in range(ntries):
try:
url = post_disease_model(dm)
break
except TwillAssertionError:
pass
if ii < ntries - 1:
debug("posting disease model failed, retrying in a bit")
time.sleep(random.random() * 30)
else:
debug("posting disease model failed %d times, giving up" % (ii + 1))
twc.get_browser()._browser._response.close() # end the connection, so that apache doesn't get upset
return ""
示例15: provide_formname
def provide_formname(self, prefix):
names = []
forms = commands.get_browser()._browser.forms()
for f in forms:
id = f.attrs.get('id')
if id and id.startswith(prefix):
names.append(id)
continue
name = f.name
if name and name.startswith(prefix):
names.append(name)
return names