本文整理汇总了Python中PySide.QtWebKit.QWebView.settings方法的典型用法代码示例。如果您正苦于以下问题:Python QWebView.settings方法的具体用法?Python QWebView.settings怎么用?Python QWebView.settings使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PySide.QtWebKit.QWebView
的用法示例。
在下文中一共展示了QWebView.settings方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Browser
# 需要导入模块: from PySide.QtWebKit import QWebView [as 别名]
# 或者: from PySide.QtWebKit.QWebView import settings [as 别名]
class Browser(object):
def __init__(self, callback, logger, options=None):
self.logger = logger
options = options or dict()
self._request_ops = {'head': QNetworkAccessManager.HeadOperation,
'get': QNetworkAccessManager.GetOperation,
'put': QNetworkAccessManager.PutOperation,
'post': QNetworkAccessManager.PostOperation,
'delete': QNetworkAccessManager.DeleteOperation}
self._timeout = int(options.pop('timeout', 30)) * 1000
max_request_retries = options.pop('max_request_retries', 3)
self._network_manager = SmartNetworkAccessManager(logger,
max_request_retries)
self._web_page = CraftyWebPage()
self._web_page.setNetworkAccessManager(self._network_manager)
self._web_view = QWebView()
self._web_view.setPage(self._web_page)
# connect the loadFinished signal to a method defined by us.
# loadFinished is the signal which is triggered when a page is loaded
self._web_view.loadFinished.connect(self._load_finished)
settings = self._web_view.settings()
settings.setAttribute(QWebSettings.AutoLoadImages,
options.pop('images', False))
settings.setAttribute(QWebSettings.JavascriptEnabled,
options.pop('javascript', False))
settings.setAttribute(QWebSettings.JavascriptCanOpenWindows,
options.pop('popups', False))
settings.setAttribute(QWebSettings.PrivateBrowsingEnabled,
options.pop('private_browsing', False))
settings.setAttribute(QWebSettings.JavaEnabled, False)
settings.setAttribute(QWebSettings.PluginsEnabled, False)
settings.setAttribute(QWebSettings.DnsPrefetchEnabled, True)
# store the callback function which will be called when a request is
# finished
self._result_callback = callback
self._is_task_finished = False
self._destroyed_status = dict()
def _prepare_request(self, url, headers):
# create an empty request
request = QNetworkRequest()
# assign a url to it
request.setUrl(QUrl(url))
# add some custom headers to the request
for (header_name, header_value) in headers.items():
request.setRawHeader(header_name, QByteArray(header_value))
return request
def _urlencode_request_data(self, raw_data):
# the data which we want to send to the server must be urlencoded
request_data = QUrl()
for (name, value) in raw_data.items():
request_data.addQueryItem(name, unicode(value))
return request_data.encodedQuery()
def _load_finished(self, ok):
"""
Called when the page is fully loaded. It will get the html file of
the loaded page and call the callback function with that result.
"""
if self._is_task_finished:
# in case loadFinished fires more than once and we already
# reported back with a result, don't do that again
self.logger.info('loadFinished emitted, but task was already '
'finished.')
return
pending_requests = self._network_manager.active_requests
if ok == 'timed_out':
self.logger.info('loadFinished emitted, request timed out.')
self._network_manager.errors.append('Request timed out.')
# to avoid treating the request by the driver as successful
ok = False
elif len(pending_requests) > 0:
self.logger.info("loadFinished emitted, waiting for requests:"
" {0}".format(pending_requests))
loaded = partial(lambda x: self._load_finished(x), ok)
QTimer.singleShot(1000, loaded)
return
self.logger.info('loadFinshed emitted, returning result.')
frame = self._web_view.page().mainFrame()
url = smart_str(frame.url().toString())
html = frame.toHtml()
result = {'html': html,
'url': url,
'successful': ok}
#.........这里部分代码省略.........
示例2: Browser
# 需要导入模块: from PySide.QtWebKit import QWebView [as 别名]
# 或者: from PySide.QtWebKit.QWebView import settings [as 别名]
class Browser(object):
def __init__(self, callback, logger, options=None):
self.logger = logger
self._request_ops = {'head': QNetworkAccessManager.HeadOperation,
'get': QNetworkAccessManager.GetOperation,
'put': QNetworkAccessManager.PutOperation,
'post': QNetworkAccessManager.PostOperation,
'delete': QNetworkAccessManager.DeleteOperation}
self._network_manager = NetworkManager(logger)
self._web_page = CraftyWebPage()
self._web_page.setNetworkAccessManager(self._network_manager)
self._web_view = QWebView()
self._web_view.setPage(self._web_page)
# connect the loadFinished signal to a method defined by us.
# loadFinished is the signal which is triggered when a page is loaded
self._web_view.loadFinished.connect(self._load_finished)
options = options or dict()
settings = self._web_view.settings()
settings.setAttribute(QWebSettings.AutoLoadImages,
options.pop('images', False))
settings.setAttribute(QWebSettings.JavascriptEnabled,
options.pop('javascript', False))
settings.setAttribute(QWebSettings.JavascriptCanOpenWindows,
options.pop('popups', False))
settings.setAttribute(QWebSettings.PrivateBrowsingEnabled,
options.pop('private_browsing', False))
settings.setAttribute(QWebSettings.JavaEnabled, False)
settings.setAttribute(QWebSettings.PluginsEnabled, False)
settings.setAttribute(QWebSettings.DnsPrefetchEnabled, True)
# store the callback function which will be called when a request is
# finished
self._result_callback = callback
def _prepare_request(self, url, headers):
# create an empty request
request = QNetworkRequest()
# assign a url to it
request.setUrl(QUrl(url))
# add some custom headers to the request
for (header_name, header_value) in headers.items():
request.setRawHeader(header_name, QByteArray(header_value))
return request
def _urlencode_request_data(self, raw_data):
# the data which we want to send to the server must be urlencoded
request_data = QUrl()
for (name, value) in raw_data.items():
request_data.addQueryItem(name, unicode(value))
return request_data.encodedQuery()
def _load_finished(self, ok):
"""
Called when the page is fully loaded. It will get the html file of
the loaded page and call the callback function with that result.
"""
frame = self._web_view.page().mainFrame()
url = smart_str(frame.url().toString())
html = frame.toHtml()
result = {'html': html,
'url': url,
'successful': True}
if self._network_manager.errors:
result['errors'] = self._network_manager.errors
# calling the callback function which we passed upon instantiation to
# report the results there
self._result_callback(result)
def make(self, method, url, headers, raw_data=None):
request = self._prepare_request(url, headers)
operation = self._request_ops[method.lower()]
request_data = self._urlencode_request_data(raw_data or dict())
self._web_view.load(request, operation, request_data)
def _find_element(self, selector):
main_frame = self._web_page.mainFrame()
element = main_frame.findFirstElement(selector)
if element.isNull():
raise ElementNotFound(selector)
return element
def fill_input(self, selector, value):
js_fill_input = """
this.setAttribute("value", "{0}");
this.value = "{0}";
#.........这里部分代码省略.........
示例3: Browser
# 需要导入模块: from PySide.QtWebKit import QWebView [as 别名]
# 或者: from PySide.QtWebKit.QWebView import settings [as 别名]
class Browser(object):
def __init__(self, callback, logger, options=None):
self.logger = logger
self._request_ops = {'head': QNetworkAccessManager.HeadOperation,
'get': QNetworkAccessManager.GetOperation,
'put': QNetworkAccessManager.PutOperation,
'post': QNetworkAccessManager.PostOperation,
'delete': QNetworkAccessManager.DeleteOperation}
self._network_manager = NetworkManager(logger)
self._web_page = QWebPage()
self._web_page.setNetworkAccessManager(self._network_manager)
self._web_view = QWebView()
self._web_view.setPage(self._web_page)
# connect the loadFinished signal to a method defined by us.
# loadFinished is the signal which is triggered when a page is loaded
self._web_view.loadFinished.connect(self._load_finished)
options = options or dict()
settings = self._web_view.settings()
settings.setAttribute(QWebSettings.AutoLoadImages,
options.pop('images', False))
settings.setAttribute(QWebSettings.JavascriptEnabled,
options.pop('javascript', False))
settings.setAttribute(QWebSettings.JavascriptCanOpenWindows,
options.pop('popups', False))
settings.setAttribute(QWebSettings.PrivateBrowsingEnabled,
options.pop('private_browsing', False))
settings.setAttribute(QWebSettings.JavaEnabled, False)
settings.setAttribute(QWebSettings.PluginsEnabled, False)
settings.setAttribute(QWebSettings.DnsPrefetchEnabled, True)
# store the callback function which will be called when a request is
# finished
self._result_callback = callback
def _prepare_request(self, url, headers):
# create an empty request
request = QNetworkRequest()
# assign a url to it
request.setUrl(QUrl(url))
# add some custom headers to the request
for (header_name, header_value) in headers.items():
request.setRawHeader(header_name, QByteArray(header_value))
return request
def _urlencode_request_data(self, raw_data):
# the data which we want to send to the server must be urlencoded
request_data = QUrl()
for (name, value) in raw_data.items():
request_data.addQueryItem(name, unicode(value))
return request_data.encodedQuery()
def _load_finished(self, ok):
"""
Called when the page is fully loaded. It will get the html file of
the loaded page and call the callback function with that result.
"""
frame = self._web_view.page().mainFrame()
url = smart_str(frame.url().toString())
html = frame.toHtml()
result = {'html': html,
'url': url,
'successful': ok}
if self._network_manager.errors:
result['errors'] = self._network_manager.errors
# calling the callback function which we passed upon instantiation to
# report the results there
self._result_callback(result)
def make(self, method, url, headers, raw_data=None):
request = self._prepare_request(url, headers)
operation = self._request_ops[method.lower()]
request_data = self._urlencode_request_data(raw_data or dict())
self._web_view.load(request, operation, request_data)