Skip to content

Commit 7844dfa

Browse files
authored
Merge pull request #614 from ahuangg/alan-branch
feat: automated cookie grabbing for config.json (must be logged in on chrome)
2 parents 4c9c8bd + ea620ed commit 7844dfa

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
lxml==4.9.1
22
requests==2.32.0
33
tqdm==4.66.3
4-
absl-py==0.12.0
4+
absl-py==0.12.0
5+
git+https://github.com/borisbabic/browser_cookie3.git@refs/pull/215/head

weibo_spider/config_util.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
import logging
33
import os
44
import sys
5+
import browser_cookie3
56
from datetime import datetime
7+
import json
68

79
logger = logging.getLogger('spider.config_util')
810

@@ -172,3 +174,29 @@ def add_user_uri_list(user_config_file_path, user_uri_list):
172174
user_uri_list[0] = '\n' + user_uri_list[0]
173175
with codecs.open(user_config_file_path, 'a', encoding='utf-8') as f:
174176
f.write('\n'.join(user_uri_list))
177+
178+
def get_cookie():
179+
"""Get weibo.cn cookie from Chrome browser"""
180+
try:
181+
chrome_cookies = browser_cookie3.chrome(domain_name='weibo.cn')
182+
cookies_dict = {cookie.name: cookie.value for cookie in chrome_cookies}
183+
cookie_string = '; '.join(f'{name}={value}' for name, value in cookies_dict.items())
184+
return cookie_string
185+
except Exception as e:
186+
logger.error(u'Failed to obtain weibo.cn cookie from Chrome browser: %s', str(e))
187+
raise
188+
189+
def update_cookie_config(user_config_file_path):
190+
"Update cookie in config.json"
191+
if not user_config_file_path:
192+
user_config_file_path = os.getcwd() + os.sep + 'config.json'
193+
try:
194+
cookie = get_cookie()
195+
with codecs.open(user_config_file_path, 'r', encoding='utf-8') as f:
196+
config = json.load(f)
197+
config['cookie'] = cookie
198+
with codecs.open(user_config_file_path, 'w', encoding='utf-8') as f:
199+
json.dump(config, f, indent=4, ensure_ascii=False)
200+
except Exception as e:
201+
logger.error(u'Failed to update cookie in config file: %s', str(e))
202+
raise

weibo_spider/spider.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ def _get_config():
366366
config_path = FLAGS.config_path
367367
elif not os.path.isfile(config_path):
368368
shutil.copy(src, config_path)
369+
config_util.update_cookie_config(config_path)
369370
logger.info(u'请先配置当前目录(%s)下的config.json文件,'
370371
u'如果想了解config.json参数的具体意义及配置方法,请访问\n'
371372
u'https://github.com/dataabc/weiboSpider#2程序设置' %

0 commit comments

Comments
 (0)