mirror of
https://github.com/NanmiCoder/MediaCrawler.git
synced 2026-06-09 03:17:25 +08:00
refactor: 优化抖音Crawler部分代码
fix: 日志初始化错误修复
This commit is contained in:
@@ -82,7 +82,7 @@ class XHSClient:
|
||||
|
||||
async def ping(self) -> bool:
|
||||
"""get a note to check if login state is ok"""
|
||||
logging.info("begin to ping xhs...")
|
||||
utils.logger.info("begin to ping xhs...")
|
||||
note_id = "5e5cb38a000000000100185e"
|
||||
try:
|
||||
note_card: Dict = await self.get_note_by_id(note_id)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import os
|
||||
import random
|
||||
import asyncio
|
||||
import logging
|
||||
@@ -8,6 +9,7 @@ from argparse import Namespace
|
||||
from playwright.async_api import Page
|
||||
from playwright.async_api import BrowserContext
|
||||
from playwright.async_api import async_playwright
|
||||
from playwright.async_api import BrowserType
|
||||
|
||||
import config
|
||||
from tools import utils
|
||||
@@ -31,8 +33,8 @@ class XiaoHongShuCrawler(AbstractCrawler):
|
||||
self.account_pool: Optional[AccountPool] = None
|
||||
|
||||
def init_config(self, **kwargs):
|
||||
for key in kwargs.keys():
|
||||
setattr(self, key, kwargs[key])
|
||||
for key, value in kwargs.items():
|
||||
setattr(self, key, value)
|
||||
|
||||
async def start(self):
|
||||
account_phone, playwright_proxy, httpx_proxy = self.create_proxy_info()
|
||||
@@ -66,13 +68,13 @@ class XiaoHongShuCrawler(AbstractCrawler):
|
||||
# Search for notes and retrieve their comment information.
|
||||
await self.search_posts()
|
||||
|
||||
logging.info("Xhs Crawler finished ...")
|
||||
utils.logger.info("Xhs Crawler finished ...")
|
||||
|
||||
async def search_posts(self):
|
||||
"""Search for notes and retrieve their comment information."""
|
||||
logging.info("Begin search xiaohongshu keywords")
|
||||
utils.logger.info("Begin search xiaohongshu keywords")
|
||||
for keyword in config.KEYWORDS.split(","):
|
||||
logging.info(f"Current keyword: {keyword}")
|
||||
utils.logger.info(f"Current keyword: {keyword}")
|
||||
note_list: List[str] = []
|
||||
max_note_len = config.MAX_PAGE_NUM
|
||||
page = 1
|
||||
@@ -88,13 +90,13 @@ class XiaoHongShuCrawler(AbstractCrawler):
|
||||
try:
|
||||
note_detail = await self.xhs_client.get_note_by_id(note_id)
|
||||
except DataFetchError as ex:
|
||||
logging.error(f"Get note detail error: {ex}")
|
||||
utils.logger.error(f"Get note detail error: {ex}")
|
||||
continue
|
||||
await xhs_model.update_xhs_note(note_detail)
|
||||
await asyncio.sleep(0.05)
|
||||
note_list.append(note_id)
|
||||
logging.info(f"keyword:{keyword}, note_list:{note_list}")
|
||||
# await self.batch_get_note_comments(note_list)
|
||||
utils.logger.info(f"keyword:{keyword}, note_list:{note_list}")
|
||||
await self.batch_get_note_comments(note_list)
|
||||
|
||||
async def batch_get_note_comments(self, note_list: List[str]):
|
||||
"""Batch get note comments"""
|
||||
@@ -106,7 +108,7 @@ class XiaoHongShuCrawler(AbstractCrawler):
|
||||
|
||||
async def get_comments(self, note_id: str):
|
||||
"""Get note comments"""
|
||||
logging.info(f"Begin get note id comments {note_id}")
|
||||
utils.logger.info(f"Begin get note id comments {note_id}")
|
||||
all_comments = await self.xhs_client.get_note_all_comments(note_id=note_id, crawl_interval=random.random())
|
||||
for comment in all_comments:
|
||||
await xhs_model.update_xhs_note_comment(note_id=note_id, comment_item=comment)
|
||||
@@ -143,12 +145,21 @@ class XiaoHongShuCrawler(AbstractCrawler):
|
||||
)
|
||||
return xhs_client_obj
|
||||
|
||||
async def launch_browser(self, chromium, playwright_proxy, user_agent, headless=True) -> BrowserContext:
|
||||
async def launch_browser(
|
||||
self,
|
||||
chromium: BrowserType,
|
||||
playwright_proxy: Optional[Dict],
|
||||
user_agent: Optional[str],
|
||||
headless: bool = True
|
||||
) -> BrowserContext:
|
||||
utils.logger.info("Begin create browser context ...")
|
||||
"""Launch browser and create browser context"""
|
||||
if config.SAVE_LOGIN_STATE:
|
||||
# feat issue #14
|
||||
# we will save login state to avoid login every time
|
||||
user_data_dir = os.path.join(os.getcwd(), "browser_data", config.USER_DATA_DIR % self.command_args.platform)
|
||||
browser_context = await chromium.launch_persistent_context(
|
||||
user_data_dir=config.USER_DATA_DIR % self.command_args.platform,
|
||||
user_data_dir=user_data_dir,
|
||||
accept_downloads=True,
|
||||
headless=headless,
|
||||
proxy=playwright_proxy,
|
||||
@@ -167,4 +178,4 @@ class XiaoHongShuCrawler(AbstractCrawler):
|
||||
async def close(self):
|
||||
"""Close browser context"""
|
||||
await self.browser_context.close()
|
||||
logging.info("Browser context closed ...")
|
||||
utils.logger.info("Browser context closed ...")
|
||||
|
||||
@@ -50,7 +50,7 @@ class XHSLogin(AbstractLogin):
|
||||
|
||||
async def begin(self):
|
||||
"""Start login xiaohongshu"""
|
||||
logging.info("Begin login xiaohongshu ...")
|
||||
utils.logger.info("Begin login xiaohongshu ...")
|
||||
if self.login_type == "qrcode":
|
||||
await self.login_by_qrcode()
|
||||
elif self.login_type == "phone":
|
||||
@@ -62,7 +62,7 @@ class XHSLogin(AbstractLogin):
|
||||
|
||||
async def login_by_mobile(self):
|
||||
"""Login xiaohongshu by mobile"""
|
||||
logging.info("Begin login xiaohongshu by mobile ...")
|
||||
utils.logger.info("Begin login xiaohongshu by mobile ...")
|
||||
await asyncio.sleep(1)
|
||||
try:
|
||||
# 小红书进入首页后,有可能不会自动弹出登录框,需要手动点击登录按钮
|
||||
@@ -79,21 +79,24 @@ class XHSLogin(AbstractLogin):
|
||||
)
|
||||
await element.click()
|
||||
except Exception as e:
|
||||
logging.info("have not found mobile button icon and keep going ...")
|
||||
utils.logger.info("have not found mobile button icon and keep going ...")
|
||||
|
||||
await asyncio.sleep(1)
|
||||
login_container_ele = await self.context_page.wait_for_selector("div.login-container")
|
||||
input_ele = await login_container_ele.query_selector("label.phone > input")
|
||||
await input_ele.fill(self.login_phone)
|
||||
await asyncio.sleep(0.5)
|
||||
|
||||
send_btn_ele = await login_container_ele.query_selector("label.auth-code > span")
|
||||
await send_btn_ele.click() # 点击发送验证码
|
||||
sms_code_input_ele = await login_container_ele.query_selector("label.auth-code > input")
|
||||
submit_btn_ele = await login_container_ele.query_selector("div.input-container > button")
|
||||
|
||||
redis_obj = aioredis.from_url(url=config.REDIS_DB_HOST, password=config.REDIS_DB_PWD, decode_responses=True)
|
||||
max_get_sms_code_time = 60 * 2 # 最长获取验证码的时间为2分钟
|
||||
no_logged_in_session = ""
|
||||
while max_get_sms_code_time > 0:
|
||||
logging.info(f"get sms code from redis remaining time {max_get_sms_code_time}s ...")
|
||||
utils.logger.info(f"get sms code from redis remaining time {max_get_sms_code_time}s ...")
|
||||
await asyncio.sleep(1)
|
||||
sms_code_key = f"xhs_{self.login_phone}"
|
||||
sms_code_value = await redis_obj.get(sms_code_key)
|
||||
@@ -119,17 +122,16 @@ class XHSLogin(AbstractLogin):
|
||||
try:
|
||||
await self.check_login_state(no_logged_in_session)
|
||||
except RetryError:
|
||||
logging.info("Login xiaohongshu failed by mobile login method ...")
|
||||
utils.logger.info("Login xiaohongshu failed by mobile login method ...")
|
||||
sys.exit()
|
||||
|
||||
wait_redirect_seconds = 5
|
||||
logging.info(f"Login successful then wait for {wait_redirect_seconds} seconds redirect ...")
|
||||
utils.logger.info(f"Login successful then wait for {wait_redirect_seconds} seconds redirect ...")
|
||||
await asyncio.sleep(wait_redirect_seconds)
|
||||
|
||||
async def login_by_qrcode(self):
|
||||
"""login xiaohongshu website and keep webdriver login state"""
|
||||
logging.info("Begin login xiaohongshu by qrcode ...")
|
||||
await asyncio.sleep(10)
|
||||
utils.logger.info("Begin login xiaohongshu by qrcode ...")
|
||||
# login_selector = "div.login-container > div.left > div.qrcode > img"
|
||||
qrcode_img_selector = "xpath=//img[@class='qrcode-img']"
|
||||
# find login qrcode
|
||||
@@ -138,7 +140,7 @@ class XHSLogin(AbstractLogin):
|
||||
selector=qrcode_img_selector
|
||||
)
|
||||
if not base64_qrcode_img:
|
||||
logging.info("login failed , have not found qrcode please check ....")
|
||||
utils.logger.info("login failed , have not found qrcode please check ....")
|
||||
# if this website does not automatically popup login dialog box, we will manual click login button
|
||||
await asyncio.sleep(0.5)
|
||||
login_button_ele = self.context_page.locator("xpath=//*[@id='app']/div[1]/div[2]/div[1]/ul/div[1]/button")
|
||||
@@ -162,20 +164,20 @@ class XHSLogin(AbstractLogin):
|
||||
partial_show_qrcode = functools.partial(utils.show_qrcode, base64_qrcode_img)
|
||||
asyncio.get_running_loop().run_in_executor(executor=None, func=partial_show_qrcode)
|
||||
|
||||
logging.info(f"waiting for scan code login, remaining time is 20s")
|
||||
utils.logger.info(f"waiting for scan code login, remaining time is 20s")
|
||||
try:
|
||||
await self.check_login_state(no_logged_in_session)
|
||||
except RetryError:
|
||||
logging.info("Login xiaohongshu failed by qrcode login method ...")
|
||||
utils.logger.info("Login xiaohongshu failed by qrcode login method ...")
|
||||
sys.exit()
|
||||
|
||||
wait_redirect_seconds = 5
|
||||
logging.info(f"Login successful then wait for {wait_redirect_seconds} seconds redirect ...")
|
||||
utils.logger.info(f"Login successful then wait for {wait_redirect_seconds} seconds redirect ...")
|
||||
await asyncio.sleep(wait_redirect_seconds)
|
||||
|
||||
async def login_by_cookies(self):
|
||||
"""login xiaohongshu website by cookies"""
|
||||
logging.info("Begin login xiaohongshu by cookie ...")
|
||||
utils.logger.info("Begin login xiaohongshu by cookie ...")
|
||||
for key, value in utils.convert_str_cookie_to_dict(self.cookie_str).items():
|
||||
await self.browser_context.add_cookies([{
|
||||
'name': key,
|
||||
|
||||
Reference in New Issue
Block a user