本文整理汇总了Python中PyQt4.QtNetwork.QNetworkCookieJar.allCookies方法的典型用法代码示例。如果您正苦于以下问题:Python QNetworkCookieJar.allCookies方法的具体用法?Python QNetworkCookieJar.allCookies怎么用?Python QNetworkCookieJar.allCookies使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt4.QtNetwork.QNetworkCookieJar
的用法示例。
在下文中一共展示了QNetworkCookieJar.allCookies方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Ghost
# 需要导入模块: from PyQt4.QtNetwork import QNetworkCookieJar [as 别名]
# 或者: from PyQt4.QtNetwork.QNetworkCookieJar import allCookies [as 别名]
#.........这里部分代码省略.........
def click(self, selector):
"""Click the targeted element.
:param selector: A CSS3 selector to targeted element.
"""
if not self.exists(selector):
raise Exception("Can't find element to click")
return self.evaluate('GhostUtils.click("%s");' % selector)
class confirm:
"""Statement that tells Ghost how to deal with javascript confirm().
:param confirm: A bollean that confirm.
:param callable: A callable that returns a boolean for confirmation.
"""
def __init__(self, confirm=True, callback=None):
self.confirm = confirm
self.callback = callback
def __enter__(self):
Ghost._confirm_expected = (self.confirm, self.callback)
def __exit__(self, type, value, traceback):
Ghost._confirm_expected = None
@property
def content(self):
"""Returns current frame HTML as a string."""
return unicode(self.main_frame.toHtml())
@property
def cookies(self):
"""Returns all cookies."""
return self.cookie_jar.allCookies()
def delete_cookies(self):
"""Deletes all cookies."""
self.cookie_jar.setAllCookies([])
@can_load_page
def evaluate(self, script):
"""Evaluates script in page frame.
:param script: The script to evaluate.
"""
return (self.main_frame.evaluateJavaScript("%s" % script),
self._release_last_resources())
def evaluate_js_file(self, path, encoding='utf-8'):
"""Evaluates javascript file at given path in current frame.
Raises native IOException in case of invalid file.
:param path: The path of the file.
:param encoding: The file's encoding.
"""
self.evaluate(codecs.open(path, encoding=encoding).read())
def exists(self, selector):
"""Checks if element exists for given selector.
:param string: The element selector.
"""
return not self.main_frame.findFirstElement(selector).isNull()
def exit(self):
"""Exits application and relateds."""
示例2: Kit
# 需要导入模块: from PyQt4.QtNetwork import QNetworkCookieJar [as 别名]
# 或者: from PyQt4.QtNetwork.QNetworkCookieJar import allCookies [as 别名]
class Kit(object):
_app = None
def __init__(self, gui=False):
if not Kit._app:
Kit._app = QApplication([])
manager = KitNetworkAccessManager()
manager.finished.connect(self.network_reply_handler)
self.cookie_jar = QNetworkCookieJar()
manager.setCookieJar(self.cookie_jar)
self.page = KitPage()
self.page.setNetworkAccessManager(manager)
self.view = KitWebView()
self.view.setPage(self.page)
self.view.setApplication(Kit._app)
if gui:
self.view.show()
def get_cookies(self):
cookies = {}
for cookie in self.cookie_jar.allCookies():
cookies[cookie.name().data()] = cookie.value().data()
return cookies
def request(self, url, user_agent='Mozilla', cookies=None, timeout=15,
method='get', data=None, headers=None):
if cookies is None:
cookies = {}
if headers is None:
headers = {}
url_info = urlsplit(url)
self.resource_list = []
loop = QEventLoop()
self.view.loadFinished.connect(loop.quit)
# Timeout
timer = QTimer()
timer.setSingleShot(True)
timer.timeout.connect(loop.quit)
timer.start(timeout * 1000)
# User-Agent
self.page.user_agent = user_agent
# Cookies
cookie_obj_list = []
for name, value in cookies.items():
domain = ('.' + url_info.netloc).split(':')[0]
#print 'CREATE COOKIE %s=%s' % (name, value)
#print 'DOMAIN = %s' % domain
cookie_obj = QNetworkCookie(name, value)
cookie_obj.setDomain(domain)
cookie_obj_list.append(cookie_obj)
self.cookie_jar.setAllCookies(cookie_obj_list)
# Method
method_obj = getattr(QNetworkAccessManager, '%sOperation'
% method.capitalize())
# Ensure that Content-Type is correct if method is post
if method == 'post':
headers['Content-Type'] = 'application/x-www-form-urlencoded'
# Post data
if data is None:
data = QByteArray()
# Request object
request_obj = QNetworkRequest(QUrl(url))
# Headers
for name, value in headers.items():
request_obj.setRawHeader(name, value)
# Make a request
self.view.load(request_obj, method_obj, data)
loop.exec_()
if timer.isActive():
request_resource = None
url = str(self.page.mainFrame().url().toString()).rstrip('/')
for res in self.resource_list:
if url == res.url or url == res.url.rstrip('/'):
request_resource = res
break
if request_resource:
return self.build_response(request_resource)
else:
raise KitError('Request was successful but it is not possible'
' to associate the request to one of received'
' responses')
else:
raise KitError('Timeout while loading %s' % url)
#.........这里部分代码省略.........
示例3: Ghost
# 需要导入模块: from PyQt4.QtNetwork import QNetworkCookieJar [as 别名]
# 或者: from PyQt4.QtNetwork.QNetworkCookieJar import allCookies [as 别名]
#.........这里部分代码省略.........
element.dispatchEvent(evt)
""" % selector)
class confirm:
"""Statement that tells Ghost how to deal with javascript confirm().
:param confirm: A boolean to set confirmation.
:param callable: A callable that returns a boolean for confirmation.
"""
def __init__(self, confirm=True, callback=None):
self.confirm = confirm
self.callback = callback
def __enter__(self):
Ghost._confirm_expected = (self.confirm, self.callback)
def __exit__(self, type, value, traceback):
Ghost._confirm_expected = None
@property
def content(self, to_unicode=True):
"""Returns current frame HTML as a string.
:param to_unicode: Whether to convert html to unicode or not
"""
if to_unicode:
return unicode(self.main_frame.toHtml())
else:
return self.main_frame.toHtml()
@property
def cookies(self):
"""Returns all cookies."""
return self.cookie_jar.allCookies()
def delete_cookies(self):
"""Deletes all cookies."""
self.cookie_jar.setAllCookies([])
def clear_alert_message(self):
"""Clears the alert message"""
self._alert = None
@can_load_page
def evaluate(self, script):
"""Evaluates script in page frame.
:param script: The script to evaluate.
"""
return (self.main_frame.evaluateJavaScript("%s" % script),
self._release_last_resources())
def evaluate_js_file(self, path, encoding='utf-8'):
"""Evaluates javascript file at given path in current frame.
Raises native IOException in case of invalid file.
:param path: The path of the file.
:param encoding: The file's encoding.
"""
self.evaluate(codecs.open(path, encoding=encoding).read())
def exists(self, selector):
"""Checks if element exists for given selector.
:param string: The element selector.
"""
示例4: allCookies
# 需要导入模块: from PyQt4.QtNetwork import QNetworkCookieJar [as 别名]
# 或者: from PyQt4.QtNetwork.QNetworkCookieJar import allCookies [as 别名]
def allCookies(self):
return QNetworkCookieJar.allCookies(self)
示例5: GRobot
# 需要导入模块: from PyQt4.QtNetwork import QNetworkCookieJar [as 别名]
# 或者: from PyQt4.QtNetwork.QNetworkCookieJar import allCookies [as 别名]
#.........这里部分代码省略.........
self.enable_javascript = javascript_enabled
#always open link in current window instead of new window
self.page.setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
self.page.linkClicked.connect(self._link_clicked)
#start the qt main loop
GRobot._loop = QtMainLoop(GRobot._app)
GRobot._loop.start()
GRobot.exit_lock.release()
@property
def popup_messages(self):
return self._popup_messages
@popup_messages.setter
def popup_messages(self, value):
self._popup_messages = unicode(value)
@property
def url(self):
return unicode(self.main_frame.url().toString())
@property
def content(self):
"""Returns current frame HTML as a string."""
return unicode(self.page.currentFrame().toHtml())
@property
def cookies(self):
"""Returns all cookies."""
return self.cookie_jar.allCookies()
@property
def enable_image(self):
"""Disable the page images can speed up page loading.
"""
return self._enable_image
@enable_image.setter
def enable_image(self, value):
self.page.settings().setAttribute(QWebSettings.AutoLoadImages, value)
self._enable_image = value
#TODO:It seems not work?
# @enable_image.deleter
# def enable_image(self):
# raise NotImplemented
@property
def enable_javascript(self):
"""Disable the page javascript can speed up page loading.
"""
return self._enable_javascript
@enable_javascript.setter
def enable_javascript(self, value):
self.page.settings().setAttribute(QWebSettings.JavascriptEnabled, value)
self._enable_javascript = value
示例6: Ghost
# 需要导入模块: from PyQt4.QtNetwork import QNetworkCookieJar [as 别名]
# 或者: from PyQt4.QtNetwork.QNetworkCookieJar import allCookies [as 别名]
#.........这里部分代码省略.........
raise Exception("Can't find element to click")
return self.evaluate("""
var element = document.querySelector("%s");
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 1, 1, 1, 1, 1,
false, false, false, false, 0, element);
element.dispatchEvent(evt)
""" % selector)
class confirm:
"""Statement that tells Ghost how to deal with javascript confirm().
:param confirm: A bollean that confirm.
:param callable: A callable that returns a boolean for confirmation.
"""
def __init__(self, confirm=True, callback=None):
self.confirm = confirm
self.callback = callback
def __enter__(self):
Ghost._confirm_expected = (self.confirm, self.callback)
def __exit__(self, type, value, traceback):
Ghost._confirm_expected = None
@property
def content(self):
"""Returns current frame HTML as a string."""
return unicode(self.main_frame.toHtml())
@property
def cookies(self):
"""Returns all cookies."""
return self.cookie_jar.allCookies()
def delete_cookies(self):
"""Deletes all cookies."""
self.cookie_jar.setAllCookies([])
@can_load_page
def evaluate(self, script):
"""Evaluates script in page frame.
:param script: The script to evaluate.
"""
return (self.main_frame.evaluateJavaScript("%s" % script),
self._release_last_resources())
def evaluate_js_file(self, path, encoding='utf-8'):
"""Evaluates javascript file at given path in current frame.
Raises native IOException in case of invalid file.
:param path: The path of the file.
:param encoding: The file's encoding.
"""
self.evaluate(codecs.open(path, encoding=encoding).read())
def exists(self, selector):
"""Checks if element exists for given selector.
:param string: The element selector.
"""
return not self.main_frame.findFirstElement(selector).isNull()
def exit(self):
"""Exits application and relateds."""
示例7: Browser
# 需要导入模块: from PyQt4.QtNetwork import QNetworkCookieJar [as 别名]
# 或者: from PyQt4.QtNetwork.QNetworkCookieJar import allCookies [as 别名]
class Browser(MouseMixin, PositionMixin, WaitMixin, JavaScriptMixin):
def __init__(self, gui=False, traffic_rules=None):
if not WKitScope.app:
raise InternalError('You should use Browser instance'
' inside `with WKitScope():` block')
self.app = WKitScope.app
self.manager = WKitNetworkAccessManager(traffic_rules=traffic_rules)
self.manager.finished.connect(self.handle_finished_network_reply)
self.cookie_jar = QNetworkCookieJar()
self.manager.setCookieJar(self.cookie_jar)
self.page = WKitWebPage()
self.page.setNetworkAccessManager(self.manager)
self.page.loadFinished.connect(self.handle_page_load_finished)
self.view = WKitWebView()
self.view.setPage(self.page)
self.view.setApplication(self.app)
self._response = None
self.gui = gui
if gui:
self.view.show()
#def __del__(self):
# self.view.close()
# self.view.setPage(None)
# del self.view
# del self.page
def get_cookies(self):
return self.cookie_jar.allCookies()
def get_simple_cookies(self):
res = {}
for cookie in self.cookie_jar.allCookies():
key = cookie.name().data().decode('latin')
val = cookie.value().data().decode('latin')
res[key] = val
return res
def go(self, url, **kwargs):
return self.request(url=url, **kwargs)
def request(self, url=None, user_agent=None, cookies=None,
timeout=DEFAULT_PAGE_LOAD_TIMEOUT,
referer=None, method='get', data=None,
headers=None, proxy=None, wait=True):
# Reset things bound to previous response
self._response = None
self.resource_list = []
self._page_loaded = False
#self.view.setHtml('', QUrl('blank://'))
# Proxy
if proxy:
self.manager.setup_proxy(proxy)
# User-Agent
if user_agent is None:
user_agent = DEFAULT_USER_AGENT
self.page.set_user_agent(user_agent)
# Cookies
if cookies is None:
cookies = {}
cookie_obj_list = []
for name, value in cookies.items():
domain = ('.' + urlsplit(url).netloc).split(':')[0]
#print 'CREATE COOKIE %s=%s' % (name, value)
#print 'DOMAIN = %s' % domain
cookie_obj = QNetworkCookie(name, value)
cookie_obj.setDomain(domain)
cookie_obj_list.append(cookie_obj)
#self.cookie_jar.setAllCookies(cookie_obj_list)
# HTTP Method
method_obj = getattr(QNetworkAccessManager, '%sOperation'
% method.capitalize())
# Ensure that Content-Type is correct if method is post
if method == 'post':
headers['Content-Type'] = 'application/x-www-form-urlencoded'
# POST Data
if data is None:
data = QByteArray()
# Build Request object
req = QNetworkRequest(QUrl(url))
# Referer
if referer:
req.setRawHeader('Referer', referer)
# Headers
if headers is None:
headers = {}
for name, value in headers.items():
req.setRawHeader(name, value)
self.content_type_stats = Counter()
#.........这里部分代码省略.........