本文整理汇总了Python中requests.cookies方法的典型用法代码示例。如果您正苦于以下问题:Python requests.cookies方法的具体用法?Python requests.cookies怎么用?Python requests.cookies使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类requests
的用法示例。
在下文中一共展示了requests.cookies方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import requests [as 别名]
# 或者: from requests import cookies [as 别名]
def __init__(self, password='', proxies=None, username='', email='', cred_root='data', user_agent=None):
self.email = email
self.username = username
self.password = password
self.req_builder = RequestBuilder()
self.bookmark_manager = BookmarkManager()
self.http = requests.session()
self.proxies = proxies
self.user_agent = user_agent
data_path = os.path.join(cred_root, self.email) + os.sep
if not os.path.isdir(data_path):
os.makedirs(data_path)
self.registry = Registry('{}registry.dat'.format(data_path))
cookies = self.registry.get(Registry.Key.COOKIES)
if cookies is not None:
self.http.cookies.update(cookies)
if self.user_agent is None:
self.user_agent = AGENT_STRING
示例2: request
# 需要导入模块: import requests [as 别名]
# 或者: from requests import cookies [as 别名]
def request(self, method, url, data=None, files=None, extra_headers=None):
headers = CaseInsensitiveDict([
('Referer', HOME_PAGE),
('X-Requested-With', 'XMLHttpRequest'),
('Accept', 'application/json'),
('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'),
('User-Agent', self.user_agent)])
csrftoken = self.http.cookies.get('csrftoken')
if csrftoken:
headers.update([('X-CSRFToken', csrftoken)])
if extra_headers is not None:
for h in extra_headers:
headers.update([(h, extra_headers[h])])
response = self.http.request(method, url, data=data, headers=headers, files=files, proxies=self.proxies)
response.raise_for_status()
self.registry.update(Registry.Key.COOKIES, response.cookies)
return response
示例3: load_cookies
# 需要导入模块: import requests [as 别名]
# 或者: from requests import cookies [as 别名]
def load_cookies(self):
"""
Load any stored cookies for the plugin that have not expired.
:return: list of the restored cookie names
"""
if not self.session or not self.cache:
raise RuntimeError("Cannot loaded cached cookies in unbound plugin")
restored = []
for key, value in self.cache.get_all().items():
if key.startswith("__cookie"):
cookie = requests.cookies.create_cookie(**value)
self.session.http.cookies.set_cookie(cookie)
restored.append(cookie.name)
if restored:
self.logger.debug("Restored cookies: {0}".format(", ".join(restored)))
return restored
示例4: clear_cookies
# 需要导入模块: import requests [as 别名]
# 或者: from requests import cookies [as 别名]
def clear_cookies(self, cookie_filter=None):
"""
Removes all of the saved cookies for this Plugin. To filter the cookies that are deleted
specify the ``cookie_filter`` argument (see :func:`save_cookies`).
:param cookie_filter: a function to filter the cookies
:type cookie_filter: function
:return: list of the removed cookie names
"""
if not self.session or not self.cache:
raise RuntimeError("Cannot loaded cached cookies in unbound plugin")
cookie_filter = cookie_filter or (lambda c: True)
removed = []
for key, value in sorted(self.cache.get_all().items(), key=operator.itemgetter(0), reverse=True):
if key.startswith("__cookie"):
cookie = requests.cookies.create_cookie(**value)
if cookie_filter(cookie):
del self.session.http.cookies[cookie.name]
self.cache.set(key, None, 0)
removed.append(key)
return removed
示例5: _req_files_download_url
# 需要导入模块: import requests [as 别名]
# 或者: from requests import cookies [as 别名]
def _req_files_download_url(self, pickcode, proapi=False):
if '_115_curtime' not in self.cookies:
self._req_file_userfile()
if not proapi:
url = self.web_api_url + '/download'
params = {'pickcode': pickcode, '_': get_timestamp(13)}
else:
url = self.proapi_url
params = {'pickcode': pickcode, 'method': 'get_file_url'}
headers = {
'Referer': self.referer_url,
}
req = Request(method='GET', url=url, params=params,
headers=headers)
res = self.http.send(req)
if res.state:
if not proapi:
return res.content['file_url']
else:
fid = res.content['data'].keys()[0]
return res.content['data'][fid]['url']['url']
else:
raise RequestFailure('Failed to get download URL.')
示例6: create_session
# 需要导入模块: import requests [as 别名]
# 或者: from requests import cookies [as 别名]
def create_session(self):
"""
Creates a session and get some details on the machine.
"""
self.session = requests.Session()
self.session.auth = self.auth
# whether verify the SSL certificate or not
self.session.verify = self.ca_cert or False
if self.disable_warnings:
requests.packages.urllib3.disable_warnings()
if self.web_service == 'rpc':
self.session.cookies = requests.cookies.cookiejar_from_dict(
self._xml_rpc_auth())
else:
# cookie workaround - consider changing with _get_last_activity()
self.list_systems(limit=1)
self.details = self.get_system_details()
示例7: login
# 需要导入模块: import requests [as 别名]
# 或者: from requests import cookies [as 别名]
def login(self, username, password):
self.get('https://note.youdao.com/web/')
self.headers['Referer'] = 'https://note.youdao.com/web/'
self.get('https://note.youdao.com/signIn/index.html?&callback=https%3A%2F%2Fnote.youdao.com%2Fweb%2F&from=web')
self.headers['Referer'] = 'https://note.youdao.com/signIn/index.html?&callback=https%3A%2F%2Fnote.youdao.com%2Fweb%2F&from=web'
self.get('https://note.youdao.com/login/acc/pe/getsess?product=YNOTE&_=' + timestamp())
self.get('https://note.youdao.com/auth/cq.json?app=web&_=' + timestamp())
self.get('https://note.youdao.com/auth/urs/login.json?app=web&_=' + timestamp())
data = {
"username": username,
"password": hashlib.md5(password).hexdigest()
}
self.post('https://note.youdao.com/login/acc/urs/verify/check?app=web&product=YNOTE&tp=urstoken&cf=6&fr=1&systemName=&deviceType=&ru=https%3A%2F%2Fnote.youdao.com%2FsignIn%2F%2FloginCallback.html&er=https%3A%2F%2Fnote.youdao.com%2FsignIn%2F%2FloginCallback.html&vcode=&systemName=&deviceType=×tamp=' + timestamp(), data=data, allow_redirects=True)
self.get('https://note.youdao.com/yws/mapi/user?method=get&multilevelEnable=true&_=' + timestamp())
print(self.cookies)
self.cstk = self.cookies.get('YNOTE_CSTK')
示例8: _set_cookies_for_request
# 需要导入模块: import requests [as 别名]
# 或者: from requests import cookies [as 别名]
def _set_cookies_for_request(session, request_args):
"""
Possibly reset session cookies for a single request then set them back.
If no cookies were present in the request arguments, do nothing.
This does not use try/finally because if it fails then we don't care about
the cookies anyway
Args:
session (requests.Session): Current session
request_args (dict): current request arguments
"""
if "cookies" in request_args:
old_cookies = dict_from_cookiejar(session.cookies)
session.cookies = cookiejar_from_dict({})
yield
session.cookies = cookiejar_from_dict(old_cookies)
else:
yield
示例9: __save_cookie
# 需要导入模块: import requests [as 别名]
# 或者: from requests import cookies [as 别名]
def __save_cookie(self, username):
"""
存储cookies, username用于文件命名
Parameters
----------
username: str
用户账号
Returns
-------
None
"""
#实例化一个LWPcookiejar对象
new_cookie_jar = cookielib.LWPCookieJar(username + '.txt')
#将转换成字典格式的RequestsCookieJar(这里我用字典推导手动转的)保存到LWPcookiejar中
requests.utils.cookiejar_from_dict(
{c.name: c.value
for c in self.s.cookies}, new_cookie_jar)
#保存到本地文件
new_cookie_jar.save('cookies/' + username + '.txt',
ignore_discard=True,
ignore_expires=True)
示例10: __read_cookie
# 需要导入模块: import requests [as 别名]
# 或者: from requests import cookies [as 别名]
def __read_cookie(self, username):
"""
读取cookies, username用于文件命名
Parameters
----------
username: str
用户账号
Returns
-------
None
"""
#实例化一个LWPCookieJar对象
load_cookiejar = cookielib.LWPCookieJar()
#从文件中加载cookies(LWP格式)
load_cookiejar.load('cookies/' + username + '.txt',
ignore_discard=True,
ignore_expires=True)
#工具方法转换成字典
load_cookies = requests.utils.dict_from_cookiejar(load_cookiejar)
#工具方法将字典转换成RequestsCookieJar,赋值给session的cookies.
self.s.cookies = requests.utils.cookiejar_from_dict(load_cookies)
示例11: get_score
# 需要导入模块: import requests [as 别名]
# 或者: from requests import cookies [as 别名]
def get_score(cookies):
try:
jar = RequestsCookieJar()
for cookie in cookies:
jar.set(cookie['name'], cookie['value'])
total = requests.get("https://pc-api.xuexi.cn/open/api/score/get", cookies=jar).content.decode("utf8")
total = int(json.loads(total, encoding="utf8")["data"]["score"])
each = requests.get("https://pc-api.xuexi.cn/open/api/score/today/queryrate", cookies=jar).content.decode(
"utf8")
each = json.loads(each, encoding="utf8")["data"]["dayScoreDtos"]
each = [int(i["currentScore"]) for i in each if i["ruleId"] in [1, 2, 9, 1002, 1003]]
return total, each
except:
print("=" * 120)
print("get_video_links获取失败")
print("=" * 120)
raise
示例12: save_cookies
# 需要导入模块: import requests [as 别名]
# 或者: from requests import cookies [as 别名]
def save_cookies(self, cookie_filter=None, default_expires=60 * 60 * 24 * 7):
"""
Store the cookies from ``http`` in the plugin cache until they expire. The cookies can be filtered
by supplying a filter method. eg. ``lambda c: "auth" in c.name``. If no expiry date is given in the
cookie then the ``default_expires`` value will be used.
:param cookie_filter: a function to filter the cookies
:type cookie_filter: function
:param default_expires: time (in seconds) until cookies with no expiry will expire
:type default_expires: int
:return: list of the saved cookie names
"""
if not self.session or not self.cache:
raise RuntimeError("Cannot cache cookies in unbound plugin")
cookie_filter = cookie_filter or (lambda c: True)
saved = []
for cookie in filter(cookie_filter, self.session.http.cookies):
cookie_dict = {}
for attr in ("version", "name", "value", "port", "domain", "path", "secure", "expires", "discard",
"comment", "comment_url", "rfc2109"):
cookie_dict[attr] = getattr(cookie, attr, None)
cookie_dict["rest"] = getattr(cookie, "rest", getattr(cookie, "_rest", None))
expires = default_expires
if cookie_dict['expires']:
expires = int(cookie_dict['expires'] - time.time())
key = "__cookie:{0}:{1}:{2}:{3}".format(cookie.name,
cookie.domain,
cookie.port_specified and cookie.port or "80",
cookie.path_specified and cookie.path or "*")
self.cache.set(key, cookie_dict, expires)
saved.append(cookie.name)
if saved:
self.logger.debug("Saved cookies: {0}".format(", ".join(saved)))
return saved
示例13: login
# 需要导入模块: import requests [as 别名]
# 或者: from requests import cookies [as 别名]
def login(account=None, password=None):
if islogin() == True:
Logging.success(u"你已经登录过咯")
return True
if account == None:
(account, password) = read_account_from_config_file()
if account == None:
sys.stdout.write(u"请输入登录账号: ")
account = raw_input()
password = getpass("请输入登录密码: ")
form_data = build_form(account, password)
"""
result:
{"result": True}
{"error": {"code": 19855555, "message": "unknown.", "data": "data" } }
{"error": {"code": -1, "message": u"unknown error"} }
"""
result = upload_form(form_data)
if "error" in result:
if result["error"]['code'] == 1991829:
# 验证码错误
Logging.error(u"验证码输入错误,请准备重新输入。" )
return login()
elif result["error"]['code'] == 100005:
# 密码错误
Logging.error(u"密码输入错误,请准备重新输入。" )
return login()
else:
Logging.warn(u"unknown error." )
return False
elif "result" in result and result['result'] == True:
# 登录成功
Logging.success(u"登录成功!" )
requests.cookies.save()
return True
示例14: parse_cookies
# 需要导入模块: import requests [as 别名]
# 或者: from requests import cookies [as 别名]
def parse_cookies(self, cookies):
if not self.region:
self.region = _found_region(cookies)
new_cookies = {cookie["name"]: cookie["value"] for cookie in cookies}
return requests.cookies.cookiejar_from_dict(new_cookies)
示例15: create_session
# 需要导入模块: import requests [as 别名]
# 或者: from requests import cookies [as 别名]
def create_session(self):
self.session = requests.Session()
self.session.cookies = self.auth_data.cookie_jar
self.region = self.auth_data.region
self.session.max_redirects = 300
self.session.headers = {
"Authorization": f"Bearer {self.auth_data.access_token}",
"User-Agent": FIREFOX_AGENT
}