From 2b857b26855bc8183de3c8d9e83ba46d1d19209c Mon Sep 17 00:00:00 2001 From: work Date: Fri, 23 May 2025 15:20:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=95=E7=8B=AC=E5=B0=81=E8=A3=85=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E6=98=AF=E5=90=A6=E5=86=BB=E7=BB=93=E5=87=BD=E6=95=B0?= =?UTF-8?q?,=20=E7=99=BB=E5=BD=95=E5=89=8D=E6=A0=A1=E9=AA=8C=E8=B4=A6?= =?UTF-8?q?=E5=8F=B7=E6=98=AF=E5=90=A6=E5=86=BB=E7=BB=93.=20=E5=87=8F?= =?UTF-8?q?=E5=B0=91=E7=99=BB=E5=BD=95=E6=97=B6=E9=97=B4=EF=BC=8C=20?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E5=90=8E=E5=88=A4=E6=96=ADcookies=E4=B8=AD?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E6=9C=89c=5Fuser=E5=AD=97=E6=AE=B5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spider/task.py | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/spider/task.py b/spider/task.py index 872100b..e2c5f75 100644 --- a/spider/task.py +++ b/spider/task.py @@ -782,7 +782,7 @@ def parse_cookies(cookies): return cookies -def check_account_status(page, cookies): +def check_freeze_account(uid): # 检查是否冻结 headers = { 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7', @@ -806,14 +806,20 @@ def check_account_status(page, cookies): 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36', 'viewport-width': '743', } - cookies = {i['name']: i['value'] for i in cookies} - uid = cookies['c_user'] url = f"https://graph.facebook.com/{uid}/picture?type=normal" response = requests.get(url, headers=headers, allow_redirects=False) if response.status_code == 302: if response.headers.get('Location') == 'https://static.xx.fbcdn.net/rsrc.php/v1/yh/r/C5yt7Cqf3zU.jpg': raise AuthException('该账号已被冻结', 'frozen') + +def check_account_status(page, cookies): + # 检查是否冻结 + cookies = {i['name']: i['value'] for i in cookies} + uid = cookies['c_user'] + + check_freeze_account(uid) + # 校验Cookies 是否失效 # 登录页面判断 login_btn = page.query_selector_all('//button[@name="login"]') @@ -1164,6 +1170,9 @@ def get_login_continue_btn(page): def playwright_login(username, password, code_2fa=None): + # 检查是否冻结 + check_freeze_account(username) + path = os.path.join(BASE_PATH, 'chrome', '130-0008', 'chrome.exe') with lock: with sync_playwright() as playwright: @@ -1177,14 +1186,14 @@ def playwright_login(username, password, code_2fa=None): url = 'https://www.facebook.com' page.goto(url) time.sleep(random.randint(1, 10)) - page.locator('//input[@id="email"]').type(username, delay=100) - time.sleep(random.randint(1, 10)) - page.locator('//input[@id="pass"]').type(password, delay=100) - time.sleep(random.randint(1, 10)) + page.locator('//input[@id="email"]').type(username, delay=30) + time.sleep(random.randint(1, 3)) + page.locator('//input[@id="pass"]').type(password, delay=30) + time.sleep(random.randint(1, 3)) page.click('//button[@name="login"]') page.wait_for_load_state() - time.sleep(random.randint(3, 10)) + time.sleep(random.randint(3, 5)) # 设置语言为英文 context.add_cookies([ { @@ -1197,9 +1206,9 @@ def playwright_login(username, password, code_2fa=None): "secure": False, }, ]) - time.sleep(random.randint(1, 10)) + time.sleep(random.randint(1, 3)) page.reload() - time.sleep(random.randint(1, 10)) + time.sleep(random.randint(1, 3)) captcha_img = page.query_selector_all('//img[contains(@src, "captcha")]') if captcha_img: @@ -1221,8 +1230,9 @@ def playwright_login(username, password, code_2fa=None): page.locator('//img[contains(@src, "captcha")]/parent::div/parent::div/div').nth(4).click() else: raise OperationFailed('验证码解析错误') - time.sleep(8) + time.sleep(3) page.wait_for_load_state() + # 检查是否还有验证码 h2 = page.query_selector("//h2/span") if h2 is None: raise OperationFailed('页面有误') @@ -1251,7 +1261,8 @@ def playwright_login(username, password, code_2fa=None): time.sleep(1) page.locator('//label[text()="Code"]/preceding-sibling::input').fill(auth_code) page.click('//span[text()="Continue"]') - + # 这里验证可能会很慢, 做显示等待 + time.sleep(10) save_profile = page.query_selector('//span[text()="Save"]') if save_profile: save_profile.click() @@ -1259,8 +1270,11 @@ def playwright_login(username, password, code_2fa=None): if trust_device_select: trust_device_select.click() - time.sleep(10) + time.sleep(3) c = {i['name']: i['value'] for i in context.cookies()} + if c["c_user"] is None: + raise OperationFailed("操作失败") + context.close() browser.close() return {'cookies': json.dumps(c)} @@ -1301,6 +1315,6 @@ if __name__ == '__main__': # 视频链接的帖子点赞 print(playwright_like(cookies, "https://www.facebook.com/watch/?v=693587939886449")) - # cookies = playwright_login('61575975148121', 'Mu1711rstu1999', 'JNCCTXB34EP5ME6RU6RND6SHBQ6KATAR') + cookies = playwright_login('61575975148121', 'Mu1711rstu1999', 'JNCCTXB34EP5ME6RU6RND6SHBQ6KATAR') # print(cookies) pass