本帖最后由 mikeee 于 2020-3-3 12:50 编辑
[Python] 纯文本查看 复制代码
'''
fetch_bduss_id.py
https://github.com/ffreemt/fetch-bduss-baiduid
'''
from typing import Union, List, Optional
import browser_cookie3
import pyperclip
from logzero import logger
# pylint: disable=too-many-arguments,
def fetch_bduss_id(
names: Optional[Union[List[str], str]] = None, # fetch all if names is None
attach_cj: bool = False, # attach raw cookiejar
copyto: bool = True, # copy to system clipboard
bduss_only: bool = True, # only copy bduss to clipboard
domain_name: str = '.baidu.com',
browser: str = 'chrome',
) -> dict:
''' fetch_bduss_id
names: None, default to ['BDUSS', 'BAIDUID']
'*', all
'''
if names is None:
names = ['BDUSS', 'BAIDUID']
if browser not in ['chrome']:
logger.warning(' 除Chrome以外的浏览器未测试过。')
# fmt: off black
# yapf: disable
try:
cj_ = getattr(browser_cookie3, browser)(domain_name=domain_name)
if names == '*':
cj_dict = dict([elm.name, elm.value] for elm in cj_) # type: ignore # noqa
else:
cj_dict = dict([elm.name, elm.value] for elm in cj_ if elm.name in names) # type: ignore # noqa
# yapf: enable
# fmt: on black
except Exception as exc:
logger.error('exc: %s', exc)
cj_ = {}
cj_dict = {'errors': str(exc)}
if attach_cj:
cj_dict = {**cj_dict, **{'cookiejar': cj_}}
if copyto:
if bduss_only and cj_dict.get('BDUSS'):
try:
pyperclip.copy(cj_dict.get('BDUSS'))
except Exception as exc:
logger.error('Unable to copy to clipboard: %s', exc)
else:
try:
pyperclip.copy(cj_dict)
except Exception as exc:
logger.error('Unable to copy to clipboard: %s', exc)
return cj_dict
if __name__ == '__main__':
try:
_ = fetch_bduss_id()
print(_)
print('\nCtrl-v 拷出 BDUSS')
_ = _.get('BDUSS')
if _ is None:
_ = 0
else:
_ = len(_)
except Exception as exc:
logger.error('%s', exc)
_ = 0
finally:
if _ < 150: # 192
logger.warning(' 如果没有用Chrome登录百度的话,先登录百度... ')
有些程序(如BaiduPCS或百度翻译程序)需要百度的cookies或 BDUSS,这个小程序可以简单方便的获取BDUSS:
[Bash shell] 纯文本查看 复制代码 python fetch_bduss_baiduid.py
运行后BDUSS存在系统剪贴板里,可用 ctrl-v 拷出。
也可以用直接在 python程序里调用,请参考程序定义。
觉得有用就评个分什么的。
打了个exe包(只在Win10测试过,64位win7里应该是可以运行的), 移步此处下载: https://www.52pojie.cn/forum.php?mod=viewthread&tid=1121615&page=1&extra=#pid30373770 |