From 0a7a8c7ce21d57f1bf233b7a126494a70c48c1e1 Mon Sep 17 00:00:00 2001 From: OSinoooO <345285987@qq.com> Date: Sat, 1 Sep 2018 18:36:22 +0800 Subject: [PATCH 1/2] v1.1 --- cookiespool/config.py | 8 ++++---- cookiespool/generator.py | 5 ++++- cookiespool/importer.py | 2 +- login/weibo/cookies.py | 34 ++++++++++++++++++---------------- 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/cookiespool/config.py b/cookiespool/config.py index 31a7f2d..167be25 100644 --- a/cookiespool/config.py +++ b/cookiespool/config.py @@ -5,7 +5,7 @@ REDIS_PORT = 6379 # Redis密码,如无填None -REDIS_PASSWORD = 'foobared' +REDIS_PASSWORD = None # 产生器使用的浏览器 BROWSER_TYPE = 'Chrome' @@ -28,12 +28,12 @@ CYCLE = 120 # API地址和端口 -API_HOST = '0.0.0.0' +API_HOST = '127.0.0.1' API_PORT = 5000 # 产生器开关,模拟登录添加Cookies -GENERATOR_PROCESS = False +GENERATOR_PROCESS = True # 验证器开关,循环检测数据库中Cookies是否可用,不可用删除 -VALID_PROCESS = False +VALID_PROCESS = True # API接口服务 API_PROCESS = True diff --git a/cookiespool/generator.py b/cookiespool/generator.py index 50c4527..cf82abc 100644 --- a/cookiespool/generator.py +++ b/cookiespool/generator.py @@ -1,6 +1,7 @@ import json from selenium import webdriver from selenium.webdriver import DesiredCapabilities +from selenium.webdriver.chrome.options import Options from cookiespool.config import * from cookiespool.db import RedisClient from login.weibo.cookies import WeiboCookies @@ -33,7 +34,9 @@ def init_browser(self): self.browser = webdriver.PhantomJS(desired_capabilities=caps) self.browser.set_window_size(1400, 500) elif BROWSER_TYPE == 'Chrome': - self.browser = webdriver.Chrome() + chrome_options = Options() + chrome_options.add_argument('--headless') + self.browser = webdriver.Chrome(chrome_options=chrome_options) def new_cookies(self, username, password): """ diff --git a/cookiespool/importer.py b/cookiespool/importer.py index 3400443..8dfd978 100644 --- a/cookiespool/importer.py +++ b/cookiespool/importer.py @@ -5,7 +5,7 @@ conn = RedisClient('accounts', 'weibo') def set(account, sep='----'): - username, password = account.split(sep) + username, password = account.split(' ') result = conn.set(username, password) print('账号', username, '密码', password) print('录入成功' if result else '录入失败') diff --git a/login/weibo/cookies.py b/login/weibo/cookies.py index 9e3f511..1b6c758 100644 --- a/login/weibo/cookies.py +++ b/login/weibo/cookies.py @@ -53,7 +53,7 @@ def login_successfully(self): """ try: return bool( - WebDriverWait(self.browser, 5).until(EC.presence_of_element_located((By.CLASS_NAME, 'drop-title')))) + WebDriverWait(self.browser, 5).until(EC.presence_of_element_located((By.CLASS_NAME, 'main-wrap')))) except TimeoutException: return False @@ -67,6 +67,7 @@ def get_position(self): except TimeoutException: print('未出现验证码') self.open() + return '' time.sleep(2) location = img.location size = img.size @@ -204,16 +205,6 @@ def main(self): 'content': '用户名或密码错误' } # 如果不需要验证码直接登录成功 - if self.login_successfully(): - cookies = self.get_cookies() - return { - 'status': 1, - 'content': cookies - } - # 获取验证码图片 - image = self.get_image('captcha.png') - numbers = self.detect_image(image) - self.move(numbers) if self.login_successfully(): cookies = self.get_cookies() return { @@ -221,12 +212,23 @@ def main(self): 'content': cookies } else: - return { - 'status': 3, - 'content': '登录失败' - } + # 获取验证码图片 + image = self.get_image('captcha.png') + numbers = self.detect_image(image) + self.move(numbers) + if self.login_successfully(): + cookies = self.get_cookies() + return { + 'status': 1, + 'content': cookies + } + else: + return { + 'status': 3, + 'content': '登录失败' + } if __name__ == '__main__': - result = WeiboCookies('14773427930', 'x6pybpakq1').main() + result = WeiboCookies('14773427930', 'x6pybpakq1', 'Chrome').main() print(result) From fba54d34cb4f308535bffbb539a3ed5c1c54ba19 Mon Sep 17 00:00:00 2001 From: OSinoooO <345285987@qq.com> Date: Sun, 2 Sep 2018 11:06:52 +0800 Subject: [PATCH 2/2] v1.2 --- cookiespool/config.py | 2 +- cookiespool/generator.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/cookiespool/config.py b/cookiespool/config.py index 167be25..af897d4 100644 --- a/cookiespool/config.py +++ b/cookiespool/config.py @@ -28,7 +28,7 @@ CYCLE = 120 # API地址和端口 -API_HOST = '127.0.0.1' +API_HOST = '127.0.0.2' API_PORT = 5000 # 产生器开关,模拟登录添加Cookies diff --git a/cookiespool/generator.py b/cookiespool/generator.py index cf82abc..a3b73ba 100644 --- a/cookiespool/generator.py +++ b/cookiespool/generator.py @@ -1,7 +1,7 @@ import json from selenium import webdriver from selenium.webdriver import DesiredCapabilities -from selenium.webdriver.chrome.options import Options +from selenium import webdriver from cookiespool.config import * from cookiespool.db import RedisClient from login.weibo.cookies import WeiboCookies @@ -34,8 +34,9 @@ def init_browser(self): self.browser = webdriver.PhantomJS(desired_capabilities=caps) self.browser.set_window_size(1400, 500) elif BROWSER_TYPE == 'Chrome': - chrome_options = Options() + chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless') + chrome_options.add_argument('--disable-gpu') self.browser = webdriver.Chrome(chrome_options=chrome_options) def new_cookies(self, username, password):