Browse Source

v1.1

pull/4/head
OSinoooO 6 years ago
parent
commit
0a7a8c7ce2
  1. 8
      cookiespool/config.py
  2. 5
      cookiespool/generator.py
  3. 2
      cookiespool/importer.py
  4. 34
      login/weibo/cookies.py

8
cookiespool/config.py

@ -5,7 +5,7 @@ REDIS_HOST = 'localhost'
REDIS_PORT = 6379 REDIS_PORT = 6379
# Redis密码,如无填None # Redis密码,如无填None
REDIS_PASSWORD = 'foobared' REDIS_PASSWORD = None
# 产生器使用的浏览器 # 产生器使用的浏览器
BROWSER_TYPE = 'Chrome' BROWSER_TYPE = 'Chrome'
@ -28,12 +28,12 @@ TEST_URL_MAP = {
CYCLE = 120 CYCLE = 120
# API地址和端口 # API地址和端口
API_HOST = '0.0.0.0' API_HOST = '127.0.0.1'
API_PORT = 5000 API_PORT = 5000
# 产生器开关,模拟登录添加Cookies # 产生器开关,模拟登录添加Cookies
GENERATOR_PROCESS = False GENERATOR_PROCESS = True
# 验证器开关,循环检测数据库中Cookies是否可用,不可用删除 # 验证器开关,循环检测数据库中Cookies是否可用,不可用删除
VALID_PROCESS = False VALID_PROCESS = True
# API接口服务 # API接口服务
API_PROCESS = True API_PROCESS = True

5
cookiespool/generator.py

@ -1,6 +1,7 @@
import json import json
from selenium import webdriver from selenium import webdriver
from selenium.webdriver import DesiredCapabilities from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.chrome.options import Options
from cookiespool.config import * from cookiespool.config import *
from cookiespool.db import RedisClient from cookiespool.db import RedisClient
from login.weibo.cookies import WeiboCookies from login.weibo.cookies import WeiboCookies
@ -33,7 +34,9 @@ class CookiesGenerator(object):
self.browser = webdriver.PhantomJS(desired_capabilities=caps) self.browser = webdriver.PhantomJS(desired_capabilities=caps)
self.browser.set_window_size(1400, 500) self.browser.set_window_size(1400, 500)
elif BROWSER_TYPE == 'Chrome': 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): def new_cookies(self, username, password):
""" """

2
cookiespool/importer.py

@ -5,7 +5,7 @@ from cookiespool.db import RedisClient
conn = RedisClient('accounts', 'weibo') conn = RedisClient('accounts', 'weibo')
def set(account, sep='----'): def set(account, sep='----'):
username, password = account.split(sep) username, password = account.split(' ')
result = conn.set(username, password) result = conn.set(username, password)
print('账号', username, '密码', password) print('账号', username, '密码', password)
print('录入成功' if result else '录入失败') print('录入成功' if result else '录入失败')

34
login/weibo/cookies.py

@ -53,7 +53,7 @@ class WeiboCookies():
""" """
try: try:
return bool( 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: except TimeoutException:
return False return False
@ -67,6 +67,7 @@ class WeiboCookies():
except TimeoutException: except TimeoutException:
print('未出现验证码') print('未出现验证码')
self.open() self.open()
return ''
time.sleep(2) time.sleep(2)
location = img.location location = img.location
size = img.size size = img.size
@ -204,16 +205,6 @@ class WeiboCookies():
'content': '用户名或密码错误' '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(): if self.login_successfully():
cookies = self.get_cookies() cookies = self.get_cookies()
return { return {
@ -221,12 +212,23 @@ class WeiboCookies():
'content': cookies 'content': cookies
} }
else: else:
return { # 获取验证码图片
'status': 3, image = self.get_image('captcha.png')
'content': '登录失败' 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__': if __name__ == '__main__':
result = WeiboCookies('14773427930', 'x6pybpakq1').main() result = WeiboCookies('14773427930', 'x6pybpakq1', 'Chrome').main()
print(result) print(result)

Loading…
Cancel
Save