本文整理汇总了Python中grab.cookie.CookieManager.clear方法的典型用法代码示例。如果您正苦于以下问题:Python CookieManager.clear方法的具体用法?Python CookieManager.clear怎么用?Python CookieManager.clear使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类grab.cookie.CookieManager
的用法示例。
在下文中一共展示了CookieManager.clear方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Grab
# 需要导入模块: from grab.cookie import CookieManager [as 别名]
# 或者: from grab.cookie.CookieManager import clear [as 别名]
#.........这里部分代码省略.........
thread_name = '-%s' % thread_name
file_name = os.path.join(self.config['log_dir'], '%02d%s.log' % (
self.request_counter, thread_name))
with open(file_name, 'wb') as out:
out.write(b'Request headers:\n')
out.write(self.request_head)
out.write(b'\n')
out.write(b'Request body:\n')
out.write(self.request_body)
out.write(b'\n\n')
out.write(b'Response headers:\n')
out.write(self.doc.head if (self.doc and self.doc.head)
else b'')
file_extension = 'html'
file_name = os.path.join(self.config['log_dir'], '%02d%s.%s' % (
self.request_counter, thread_name, file_extension))
self.doc.save(file_name)
def make_url_absolute(self, url, resolve_base=False):
"""
Make url absolute using previous request url as base url.
"""
if self.config['url']:
if resolve_base:
ubody = self.doc.unicode_body()
base_url = find_base_url(ubody)
if base_url:
return urljoin(base_url, url)
return urljoin(self.config['url'], url)
else:
return url
def detect_request_method(self):
"""
Analyze request config and find which
request method will be used.
Returns request method in upper case
This method needs simetime when `process_config` method
was not called yet.
"""
method = self.config['method']
if method:
method = method.upper()
else:
if self.config['post'] or self.config['multipart_post']:
method = 'POST'
else:
method = 'GET'
return method
def clear_cookies(self):
"""
Clear all remembered cookies.
"""
self.config['cookies'] = {}
self.cookies.clear()
def setup_with_proxyline(self, line, proxy_type='http'):
# TODO: remove from base class
# maybe to proxylist?
host, port, user, pwd = parse_proxy_line(line)
server_port = '%s:%s' % (host, port)
self.setup(proxy=server_port, proxy_type=proxy_type)
if user:
userpwd = '%s:%s' % (user, pwd)
self.setup(proxy_userpwd=userpwd)
def __getstate__(self):
"""
Reset cached lxml objects which could not be pickled.
"""
state = {}
for cls in type(self).mro():
cls_slots = getattr(cls, '__slots__', ())
for slot in cls_slots:
if slot != '__weakref__':
if hasattr(self, slot):
state[slot] = getattr(self, slot)
if state['_doc']:
state['_doc'].grab = weakref.proxy(self)
return state
def __setstate__(self, state):
for slot, value in state.items():
setattr(self, slot, value)
@property
def request_headers(self):
first_head = self.request_head.decode('utf-8').split('\r\n\r\n')[0]
lines = first_head.split('\r\n')
lines = [x for x in lines if ':' in x]
return email.message_from_string('\n'.join(lines))
示例2: Grab
# 需要导入模块: from grab.cookie import CookieManager [as 别名]
# 或者: from grab.cookie.CookieManager import clear [as 别名]
#.........这里部分代码省略.........
self.request_counter, thread_name, file_extension))
self.doc.save(file_name)
def make_url_absolute(self, url, resolve_base=False):
"""
Make url absolute using previous request url as base url.
"""
if self.config['url']:
if resolve_base:
ubody = self.doc.unicode_body()
base_url = find_base_url(ubody)
if base_url:
return urljoin(base_url, url)
return urljoin(self.config['url'], url)
else:
return url
def detect_request_method(self):
"""
Analyze request config and find which
request method will be used.
Returns request method in upper case
This method needs simetime when `process_config` method
was not called yet.
"""
method = self.config['method']
if method:
method = method.upper()
else:
if self.config['post'] or self.config['multipart_post']:
method = 'POST'
else:
method = 'GET'
return method
def clear_cookies(self):
"""
Clear all remembered cookies.
"""
self.config['cookies'] = {}
self.cookies.clear()
def setup_with_proxyline(self, line, proxy_type='http'):
# TODO: remove from base class
# maybe to proxylist?
host, port, user, pwd = parse_proxy_line(line)
server_port = '%s:%s' % (host, port)
self.setup(proxy=server_port, proxy_type=proxy_type)
if user:
userpwd = '%s:%s' % (user, pwd)
self.setup(proxy_userpwd=userpwd)
def __getstate__(self):
"""
Reset cached lxml objects which could not be pickled.
"""
state = {}
for cls in type(self).mro():
cls_slots = getattr(cls, '__slots__', ())
for slot in cls_slots:
if slot != '__weakref__':
if hasattr(self, slot):
state[slot] = getattr(self, slot)
if state['_doc']:
state['_doc'].grab = weakref.proxy(self)
return state
def __setstate__(self, state):
for slot, value in state.items():
setattr(self, slot, value)
@property
def request_headers(self):
"""
Temporary hack till the time I'll understand
where to store request details.
"""
try:
first_head = self.request_head.split('\r\n\r\n')[0]
lines = first_head.split('\r\n')
lines = [x for x in lines if ':' in x]
headers = email.message_from_string('\n'.join(lines))
return headers
except Exception as ex:
logging.error('Could not parse request headers', exc_info=ex)
return {}
def dump(self):
"""
Shortcut for real-time debugging.
"""
self.doc.save('/tmp/x.html')