Skip to content

Commit

Permalink
update: improved retrieval of cookies workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuangg committed Dec 6, 2024
1 parent 7844dfa commit 913a03f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
33 changes: 24 additions & 9 deletions weibo_spider/config_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,29 +174,44 @@ def add_user_uri_list(user_config_file_path, user_uri_list):
user_uri_list[0] = '\n' + user_uri_list[0]
with codecs.open(user_config_file_path, 'a', encoding='utf-8') as f:
f.write('\n'.join(user_uri_list))

def get_cookie():
"""Get weibo.cn cookie from Chrome browser"""
try:
chrome_cookies = browser_cookie3.chrome(domain_name='weibo.cn')
cookies_dict = {cookie.name: cookie.value for cookie in chrome_cookies}
cookie_string = '; '.join(f'{name}={value}' for name, value in cookies_dict.items())
return cookie_string
return cookies_dict
except Exception as e:
logger.error(u'Failed to obtain weibo.cn cookie from Chrome browser: %s', str(e))
raise

def update_cookie_config(user_config_file_path):
"Update cookie in config.json"
def update_cookie_config(cookie, user_config_file_path):
"""Update cookie in config.json"""
if not user_config_file_path:
user_config_file_path = os.getcwd() + os.sep + 'config.json'
try:
cookie = get_cookie()
with codecs.open(user_config_file_path, 'r', encoding='utf-8') as f:
config = json.load(f)
config['cookie'] = cookie
with codecs.open(user_config_file_path, 'w', encoding='utf-8') as f:
json.dump(config, f, indent=4, ensure_ascii=False)

cookie_string = '; '.join(f'{name}={value}' for name, value in cookie.items())

if config['cookie'] != cookie_string:
config['cookie'] = cookie_string
with codecs.open(user_config_file_path, 'w', encoding='utf-8') as f:
json.dump(config, f, indent=4, ensure_ascii=False)
except Exception as e:
logger.error(u'Failed to update cookie in config file: %s', str(e))
raise

def check_cookie(user_config_file_path):
"""Checks if user is logged in"""
try:
cookie = get_cookie()
if cookie["MLOGIN"] == '0':

This comment has been minimized.

Copy link
@songzy12

songzy12 Jan 2, 2025

Collaborator

这里的 cookie 有可能不含 "MLOGIN" 这个 key,例如此时我未登录的 cookie 为:

{'SUB': '_2A25I0S7kDeRhGeVP6lET9yjJzjmIHXVrry4srDV6PUJbktANLWXAkW1NTU8PBZ0pWcKBCiA1prnv4RhE1j-P_CWd', 'SUBP': '0033WrSXqPxfM725Ws9jqgMF55529P9D9WhxVcpPFHOwH1VA0_OSa3CL5JpX5KzhUgL.FoepeKeES0qfSK-2dJLoI7R_TPW0IPxQUPiDMcX_TBtt'}

在这种情况下会抛异常:

Check for cookie failed: 'MLOGIN'
'MLOGIN'
Traceback (most recent call last):
  File "/Users/songzy/weiboSpider/weibo_spider/spider.py", line 387, in main
    config = _get_config()
  File "/Users/songzy/weiboSpider/weibo_spider/spider.py", line 376, in _get_config
    config_util.check_cookie(config_path)
  File "/Users/songzy/weiboSpider/weibo_spider/config_util.py", line 211, in check_cookie
    if cookie["MLOGIN"] == '0':
KeyError: 'MLOGIN'

可修正为

        if cookie.get("MLOGIN", '0') == '0':
logger.warning("使用 Chrome 在此登录 %s", "https://passport.weibo.com/sso/signin?entry=wapsso&source=wapssowb&url=https://m.weibo.cn/")
sys.exit()
else:
update_cookie_config(cookie, user_config_file_path)
except Exception as e:
logger.error(u'Check for cookie failed: %s', str(e))
raise
2 changes: 1 addition & 1 deletion weibo_spider/spider.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,14 +366,14 @@ def _get_config():
config_path = FLAGS.config_path
elif not os.path.isfile(config_path):
shutil.copy(src, config_path)
config_util.update_cookie_config(config_path)
logger.info(u'请先配置当前目录(%s)下的config.json文件,'
u'如果想了解config.json参数的具体意义及配置方法,请访问\n'
u'https://github.com/dataabc/weiboSpider#2程序设置' %
os.getcwd())
sys.exit()
try:
with open(config_path) as f:
config_util.check_cookie(config_path)
config = json.loads(f.read())
return config
except ValueError:
Expand Down

0 comments on commit 913a03f

Please sign in to comment.