Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

主要修改了账号登录成功的判断条件 #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cookiespool/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
REDIS_PORT = 6379

# Redis密码,如无填None
REDIS_PASSWORD = 'foobared'
REDIS_PASSWORD = None

# 产生器使用的浏览器
BROWSER_TYPE = 'Chrome'
Expand All @@ -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
6 changes: 5 additions & 1 deletion cookiespool/generator.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down
2 changes: 1 addition & 1 deletion cookiespool/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '录入失败')
Expand Down
34 changes: 18 additions & 16 deletions login/weibo/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -67,6 +67,7 @@ def get_position(self):
except TimeoutException:
print('未出现验证码')
self.open()
return ''
time.sleep(2)
location = img.location
size = img.size
Expand Down Expand Up @@ -204,29 +205,30 @@ 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 {
'status': 1,
'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)