Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def title(self):
"""Return title, or None if there is no title element in the document.
Tags are stripped or textified as described in docs for
PullParser.get_text() method of pullparser module.
"""
if not self.viewing_html():
raise BrowserStateError("not viewing HTML")
if self._title is None:
p = pullparser.PullParser(self._response,
encoding=self._encoding(self._response))
try:
p.get_tag("title")
except pullparser.NoMoreTokensError:
pass
else:
self._title = p.get_text()
return self._title
def back(self, n=1):
"""Go back n steps in history, and return response object.
n: go back this number of steps (default 1 step)
"""
n = int(n) + 1 # we need to first get rid of the current page's history
assert n > 1
while n:
try:
self.request, self._response = self._history.pop()
except IndexError:
raise BrowserStateError("already at start of history")
n -= 1
if self._response is not None:
self._parse_html(self._response)
return self._response
def click(self, *args, **kwds):
"""See ClientForm.HTMLForm.click for documentation."""
if not self.viewing_html():
raise BrowserStateError("not viewing HTML")
request = self.form.click(*args, **kwds)
return self._add_referer_header(request)
def viewing_html(self):
"""Return whether the current response contains HTML data."""
if self._response is None:
raise BrowserStateError("not viewing any document")
ct = self._response.info().getheaders("content-type")
return ct and ct[0].startswith("text/html")
def _mech_open(self,
url,
data=None,
update_history=True,
visit=None,
timeout=_sockettimeout._GLOBAL_DEFAULT_TIMEOUT):
try:
url.get_full_url
except AttributeError:
# string URL -- convert to absolute URL if required
scheme, authority = _rfc3986.urlsplit(url)[:2]
if scheme is None:
# relative URL
if self._response is None:
raise BrowserStateError("can't fetch relative reference: "
"not viewing any document")
url = _rfc3986.urljoin(self._response.geturl(), url)
request = self._request(url, data, visit, timeout)
visit = request.visit
if visit is None:
visit = True
if visit:
self._visit_request(request, update_history)
success = True
try:
response = UserAgentBase.open(self, request, data)
except HTTPError as error:
success = False
caption = '%s\nvia %s' % (filtered_msg, nick)
def print_result(result):
try:
print "%s posted a %s to %s" % (nick, result['type'],
result['url'])
except:
print "Call result: %s" % repr(result)
try:
try:
urls = api.readurls()
if url not in api.readurls():
self.bot.set_callback(api.autopost_url, print_result,
args=(url, caption, tag_args))
except BrowserStateError:
# Skipping because of a mechanize error.
return
except URLError:
return
except TumblrError, e:
return
if (times < 3):
print e
print "Error encountered, trying it again."
# try it again, a couple of times.
sleep(3)
self.handle_url(self, message, reply_to, url, sender,
times=times)
else:
print e
def click(self, *args, **kwds):
"""See :meth:`mechanize.HTMLForm.click()` for documentation."""
if not self.viewing_html():
raise BrowserStateError("not viewing HTML")
request = self.form.click(*args, **kwds)
return self._add_referer_header(request)
def click_link(self, link=None, **kwds):
"""Find a link and return a Request object for it.
Arguments are as for :meth:`find_link()`, except that a link may be
supplied as the first argument.
"""
if not self.viewing_html():
raise BrowserStateError("not viewing HTML")
if not link:
link = self.find_link(**kwds)
else:
if kwds:
raise ValueError(
"either pass a Link, or keyword arguments, not both")
request = self.request_class(link.absolute_url)
return self._add_referer_header(request)
For example:
.. code-block:: python
browser.set_cookie(
"sid=abcdef; expires=Wednesday, 09-Nov-06 23:12:40 GMT")
Currently, this method does not allow for adding RFC 2986 cookies.
This limitation will be lifted if anybody requests it.
See also :meth:`set_simple_cookie()` for an easier way to set cookies
without needing to create a Set-Cookie header string.
"""
if self._response is None:
raise BrowserStateError("not viewing any document")
if self.request.get_type() not in ["http", "https"]:
raise BrowserStateError("can't set cookie for non-HTTP/HTTPS "
"transactions")
cookiejar = self._ua_handlers["_cookies"].cookiejar
response = self.response() # copy
headers = response.info()
headers["Set-cookie"] = cookie_string
cookiejar.extract_cookies(response, self.request)
def title(self):
' Return title, or None if there is no title element in the document. '
if not self.viewing_html():
raise BrowserStateError("not viewing HTML")
return self._factory.title