diff --git a/cookiespool/config.py b/cookiespool/config.py index 31a7f2d..af897d4 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.2' 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..a3b73ba 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 import webdriver from cookiespool.config import * from cookiespool.db import RedisClient from login.weibo.cookies import WeiboCookies @@ -33,7 +34,10 @@ 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 = 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): """ 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)