class CookiesGenerator(object): def __init__(self, website='default'): """ 父类, 初始化一些对象 :param website: 名称 :param browser: 浏览器, 若不使用浏览器则可设置为 None """ self.website = website self.cookies_db = RedisClient('cookies', self.website) self.accounts_db = RedisClient('accounts', self.website) def new_cookies(self, username, password): """ 新生成Cookies,子类一定需要重写 :param username: 用户名 :param password: 密码 :return: """ raise NotImplementedError def process_cookies(self, cookies): """ 处理Cookies :param cookies: :return: """ dict = {} for cookie in cookies: dict[cookie['name']] = cookie['value'] return dict def run(self): """ 运行, 得到所有账户, 然后顺次模拟登录 :return: """ accounts_usernames = self.accounts_db.usernames() cookies_usernames = self.cookies_db.usernames() for username in accounts_usernames: if not username in cookies_usernames: password = self.accounts_db.get(username) print('正在生成Cookies', '账号', username, '密码', password) result = self.new_cookies(username, password) # 成功获取 if result.get('status') == 1: # cookies = self.process_cookies(result.get('content')) cookies = result.get('content') print('成功获取到Cookies', cookies) if self.cookies_db.set(username, json.dumps(cookies)): print('成功保存Cookies') # 密码错误,移除账号 elif result.get('status') == 2: print("reason: {}".format(result.get('content'))) if self.accounts_db.delete(username): print('成功删除账号: {}'.format(username)) else: print("其他错误: {}".format(result.get('content'))) else: print('所有账号都已经成功获取Cookies')
class CookiesGenerator(object): def __init__(self, website='default'): self.website = website self.cookies_db = RedisClient('cookies', self.website) self.accounts_db = RedisClient('accounts', self.website) def new_cookies(self, *args): raise NotImplementedError def run(self): accounts_usernames = self.accounts_db.usernames() for username in accounts_usernames: if username: password = self.accounts_db.get(username) print('正在生成Cookies', '账号', username, '密码', password) result = self.new_cookies(username, password) else: print('正在生成Cookies') result = self.new_cookies() # 成功获取 if result.get('status') == 1: content_data = result.get('content') print('成功获取到随机请求头', content_data) if self.cookies_db.set(username, content_data): print('成功保存随机请求头') # 密码错误,移除账号 elif result.get('status') == 2: print(result.get('content')) if self.accounts_db.delete(username): print('成功删除账号') else: print(result.get('content'))
class CookiesGenerator(object): def __init__(self, website='default'): """ 父类, 初始化一些对象 :param website: 名称 :param browser: 浏览器, 若不使用浏览器则可设置为 None """ self.website = website self.cookies_db = RedisClient('cookies', self.website) self.accounts_db = RedisClient('accounts', self.website) self.init_browser() def __del__(self): self.close() def init_browser(self): """ 通过browser参数初始化全局浏览器供模拟登录使用 :return: """ if BROWSER_TYPE == 'PhantomJS': caps = DesiredCapabilities.PHANTOMJS caps[ "phantomjs.page.settings.userAgent"] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36' self.browser = webdriver.PhantomJS(desired_capabilities=caps) self.browser.set_window_size(1400, 500) elif BROWSER_TYPE == 'Chrome': self.browser = webdriver.Chrome() def new_cookies(self, username, password): """ 新生成Cookies,子类需要重写 :param username: 用户名 :param password: 密码 :return: """ raise NotImplementedError def process_cookies(self, cookies): """ 处理Cookies :param cookies: :return: """ dict = {} for cookie in cookies: dict[cookie['name']] = cookie['value'] return dict def run(self): """ 运行, 得到所有账户, 然后顺次模拟登录 :return: """ accounts_usernames = self.accounts_db.usernames() cookies_usernames = self.cookies_db.usernames() for username in accounts_usernames: if not username in cookies_usernames: password = self.accounts_db.get(username) print('正在生成Cookies', '账号', username, '密码', password) result = self.new_cookies(username, password) # 成功获取 if result.get('status') == 1: cookies = self.process_cookies(result.get('content')) print('成功获取到Cookies', cookies) if self.cookies_db.set(username, json.dumps(cookies)): print('成功保存Cookies') # 密码错误,移除账号 elif result.get('status') == 2: print(result.get('content')) if self.accounts_db.delete(username): print('成功删除账号', username) else: print(result.get('content')) else: print('所有账号都已经成功获取Cookies') def close(self): """ 关闭 :return: """ try: print('Closing Browser') self.browser.close() del self.browser except TypeError: print('Browser not opened')
class CookiesGenerator(object): def __init__(self, website='default'): """ 父类, 初始化一些对象 :param website: 名称 :param browser: 浏览器, 若不使用浏览器则可设置为 None """ self.website = website self.cookies_db = RedisClient('cookies', self.website) self.accounts_db = RedisClient('accounts', self.website) self.init_browser() def __del__(self): self.close() def init_browser(self): """ 通过browser参数初始化全局浏览器供模拟登录使用 :return: """ if BROWSER_TYPE == 'PhantomJS': caps = DesiredCapabilities.PHANTOMJS caps[ "phantomjs.page.settings.userAgent"] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36' self.browser = webdriver.PhantomJS(desired_capabilities=caps) self.browser.set_window_size(1400, 500) elif BROWSER_TYPE == 'Chrome': # chrome_driver = r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe" options = webdriver.ChromeOptions() # 此步骤很重要,设置为开发者模式,防止被各大网站识别出来使用了Selenium options.add_experimental_option('excludeSwitches', ['enable-automation']) options.add_argument('--headless') options.add_argument('--disable-gpu') options.add_argument("window-size=1024,768") options.add_argument("--no-sandbox") # driver = webdriver.Chrome(chromedriver, options=options) # self.browser = webdriver.Chrome(executable_path=chrome_driver, options=options) self.browser = webdriver.Chrome(options=options) def new_cookies(self, username, password): """ 新生成Cookies,子类需要重写 :param username: 用户名 :param password: 密码 :return: """ raise NotImplementedError def process_cookies(self, cookies): """ 处理Cookies :param cookies: :return: """ dict = {} for cookie in cookies: dict[cookie['name']] = cookie['value'] return dict def run(self): """ 运行, 得到所有账户, 然后顺次模拟登录 :return: """ accounts_usernames = self.accounts_db.usernames() cookies_usernames = self.cookies_db.usernames() for username in accounts_usernames: if not username in cookies_usernames: password = self.accounts_db.get(username) print('正在生成Cookies', '账号', username, '密码', password) result = self.new_cookies(username, password) # 成功获取 if result.get('status') == 1: cookies = self.process_cookies(result.get('content')) print('成功获取到Cookies', cookies) if self.cookies_db.set(username, json.dumps(cookies)): print('成功保存Cookies') # 密码错误,移除账号 elif result.get('status') == 2: print(result.get('content')) if self.accounts_db.delete(username): print('成功删除账号') else: print(result.get('content')) else: print('所有账号都已经成功获取Cookies') def close(self): """ 关闭 :return: """ try: print('Closing Browser') self.browser.close() del self.browser except TypeError: print('Browser not opened')
class CookiesGenerator(object): #初始化一些对象 def __init__(self, website='default'): self.website = website self.cookies_db = RedisClient('cookies', self.website) self.accounts_db = RedisClient('accounts', self.website) self.init_browser() #关闭Browser def __del__(self): try: print('Closing Browser') self.browser.close() del self.browser except TypeError: print('Browser not opened') #利用Browser参数初始化全局浏览器供模拟登录使用 def init_browser(self): if BROWSER_TYPE == 'PhantomJS': caps = DesiredCapabilities.PHANTOMJS caps[ 'phantomjs.page.settings.userAgent']='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36' \ ' (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36' self.browser = webdriver.PhantomJS(desired_capabilities=caps) self.browser.set_window_size(1400, 500) if BROWSER_TYPE == 'Chrome': chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless') self.browser = webdriver.Chrome(chrome_options=chrome_options) #新生成cookies,子类需要重写 def new_cookies(self, username, password): raise NotImplementedError #处理cookies, self.browser.get_cookies()此方法获得的cookies是个列表,其中每个元素都是个字典,我们需要取出字典中的name和value键所对应的值组成新的字典 def process_cookies(self, cookies): dict = {} for cookie in cookies: dict[cookie['name']] = cookie['value'] return dict #运行,得到所有账户,然后顺次模拟登陆 def run(self): cookies_usernames = self.cookies_db.usernames() accounts_usernames = self.accounts_db.usernames() for username in accounts_usernames: if not username in cookies_usernames: password = self.accounts_db.get(username) print('正在生成Cookies', '账号', username, '密码', password) result = self.new_cookies(username, password) #成功获取 if result.get('status') == 1: cookies = self.process_cookies(result.get('content')) print('成功获取到Cookies', cookies) if self.cookies_db.set(username, json.dumps(cookies)): print('成功保存Cookies') #密码错误,移除账号 elif result.get('status') == 2: print(result.get('content')) if self.accounts_db.delete(username): print('成功删除账号') else: print(result.get('content')) else: print('所有账号都已成功获取Cookies')
class CookiesGenerator(object): def __init__(self, website='default'): """ 父类, 初始化一些对象 :param website: 名称 :param browser: 浏览器, 若不使用浏览器则可设置为 None """ self.website = website self.cookies_db = RedisClient('cookies', self.website) self.accounts_db = RedisClient('accounts', self.website) self.init_browser() def __del__(self): #self.close() pass def init_browser(self): """ 通过browser参数初始化全局浏览器供模拟登录使用 :return: """ if BROWSER_TYPE == 'PhantomJS': caps = DesiredCapabilities.PHANTOMJS caps[ "phantomjs.page.settings.userAgent"] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36' self.browser = webdriver.PhantomJS( '/Users/ahua/Desktop/phantomjs/bin/phantomjs', desired_capabilities=caps) self.browser.set_window_size(500, 1400) elif BROWSER_TYPE == 'Chrome': self.display = Display(visible=0, size=(800, 800)) self.display.start() self.option = webdriver.ChromeOptions() self.option.add_argument('--no-sandbox') self.browser = webdriver.Chrome( executable_path= '/users/lichao/Toolkit/driver/chromedriver_wechat', chrome_options=self.option) def new_cookies(self, username, password): """ 新生成Cookies,子类需要重写 :param username: 用户名 :param password: 密码 :return: """ raise NotImplementedError def process_cookies(self, cookies): """ 处理Cookies :param cookies: :return: """ return cookies def run(self): """ 运行, 得到所有账户, 然后顺次模拟登录 :return: """ accounts_usernames = self.accounts_db.usernames() # print(accounts_usernames) cookies_usernames = self.cookies_db.usernames() # print(cookies_usernames) for username in accounts_usernames: if not username in cookies_usernames: password = self.accounts_db.get(username) print('正在生成Cookies', '账号', username, '密码', password) result = self.new_cookies(username, password) # 成功获取 if result.get('status') == 1: cookies = self.process_cookies(result.get('content')) print('成功获取到Cookies', cookies) if self.cookies_db.set(username, json.dumps(cookies)): print('成功保存Cookies') # 密码错误,移除账号 elif result.get('status') == 2: print(result.get('content')) if self.accounts_db.delete(username): print('成功删除账号') else: print(result.get('content')) else: print('所有账号都已经成功获取Cookies') def close(self): """ 关闭 :return: """ try: print('Closing Browser') self.browser.close() del self.browser os.system('pkill -f "/users/lichao/Toolkit/driver/chromedriver"') os.system('pkill -f "/opt/google/chrome/chrome"') os.system('pkill -f "/usr/bin/google-chrome"') except TypeError: print('Browser not opened') try: print('Closing Display') self.display.stop() del self.display except TypeError: print('Display not opened')
class CookiesGenerator(object): def __init__(self, website='default'): """ 父类, 初始化一些对象 :param website: 名称 :param browser: 浏览器, 若不使用浏览器则可设置为 None """ self.website = website self.cookies_db = RedisClient('cookies', self.website) self.accounts_db = RedisClient('accounts', self.website) self.init_browser() def __del__(self): self.close() def init_browser(self): """ 通过browser参数初始化全局浏览器供模拟登录使用 :return: """ self.browser = webdriver.Chrome() def new_cookies(self, username, password): """ 新生成Cookies,子类需要重写 :param username: 用户名 :param password: 密码 :return: """ raise NotImplementedError def process_cookies(self, cookies): """ 处理Cookies :param cookies: :return: """ dict = {} for cookie in cookies: dict[cookie['name']] = cookie['value'] return dict def run(self): """ 运行, 得到所有账户, 然后顺次模拟登录 :return: """ accounts_usernames = self.accounts_db.usernames() cookies_usernames = self.cookies_db.usernames() for username in accounts_usernames: if not username in cookies_usernames: password = self.accounts_db.get(username) print('正在生成Cookies', '账号', username, '密码', password) result = self.new_cookies(username, password) # 成功获取 if result.get('status') == 1: cookies = self.process_cookies(result.get('content')) print('成功获取到Cookies', cookies) if self.cookies_db.set(username, json.dumps(cookies)): print('成功保存Cookies') # 密码错误,移除账号 elif result.get('status') == 2: print(result.get('content')) if self.accounts_db.delete(username): print('成功删除账号') else: print(result.get('content')) else: print('所有账号都已经成功获取Cookies') def close(self): """ 关闭 :return: """ try: print('Closing Browser') self.browser.close() del self.browser except TypeError: print('Browser not opened')
class CookiesGenerator(object): def __init__(self,website='default'): ''' 检测模块 获取新增用户cookies,保存 :param website: 名称 ''' self.website = website self.cookies_db = RedisClient('cookies',self.website) self.accounts_db = RedisClient('accounts',self.website) self.driver = webdriver.Chrome() def __del__(self): '''销毁登录''' self.close() # self.driver.close() # del self.driver def new_cookies(self,username,password): '''子类定义方法''' raise NotImplementedError def process_cookeis(self,cookies): ''' 处理cookies :param cookies: :return: ''' dict = {} for cookie in cookies: dict[cookie['name']] = cookie['value'] return dict def run(self): ''' 得到所有用户,依次模拟登录 :return: ''' accounts_usernames = self.accounts_db.username() cookies_usernames = self.cookies_db.username() for username in accounts_usernames: if not username in cookies_usernames: password = self.accounts_db.get(username) print(f'正在生成{username}账号,密码是{password}') result = self.new_cookies(username,password) # 成功获取 if result.get('status') == 1: cookies = self.process_cookeis(result.get('content')) print(f'成功获取cookies:{cookies}') if self.cookies_db.set(username,json.dumps(cookies)): print('cookies以保存') elif result.get('status') == 2: print(result.get('content')) if self.accounts_db.delete(username): print('成功删除账号') else: print(result.get('content')) print('所有账号已获取cookies') def close(self): '''关闭驱动''' try: print('关闭浏览器...') self.driver.close() del self.driver except TypeError: print('浏览器未关闭')
class CookiesGenerator(object): def __init__(self, website='default'): """ 父类, 初始化一些对象 :param website: 名称 :param browser: 浏览器, 若不使用浏览器则可设置为 None """ # 浏览器对象 self.website = website # Redis 数据库连接对象---cookies 池 self.cookies_db = RedisClient('cookies', self.website) # Redis 数据库连接对象--- 用户池 self.accounts_db = RedisClient('accounts', self.website) # 初始化浏览器设置 self.init_browser() def __del__(self): """ 不论是手动关闭浏览器还是python自动回收都会触发这个方法 :return: """ self.close() def init_browser(self): """ 通过browser参数初始化全局浏览器供模拟登录使用 :return: """ if BROWSER_TYPE == 'PhantomJS': caps = DesiredCapabilities.PHANTOMJS caps[ "phantomjs.page.settings.userAgent"] = 'Mozilla/5.0 (Macintosh; ' \ 'Intel Mac OS X 10_12_3) AppleWebKit/537.36 ' \ '(KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36' self.browser = webdriver.PhantomJS(desired_capabilities=caps) self.browser.set_window_size(1400, 500) elif BROWSER_TYPE == 'Chrome': # 为满足在Linux下达到无界面的效果而增加代码 # self.display = Display(visible=0, size=(1920, 1080)) # self.display.start() # self.options = Options() # self.options.add_argument('--headless') # self.options.add_argument('--no-sandbox') # self.browser = webdriver.Chrome(chrome_options=self.options) self.browser = webdriver.Chrome() def new_cookies(self, username, password): """ 新生成Cookies,子类需要重写 :param username: 用户名 :param password: 密码 :return: """ raise NotImplementedError def process_cookies(self, cookies): """ 处理Cookies :param cookies: :return: """ dict = {} for cookie in cookies: dict[cookie['name']] = cookie['value'] return dict def run(self): """ 运行, 得到所有账户, 然后顺次模拟登录 :return: """ accounts_usernames = self.accounts_db.usernames() cookies_usernames = self.cookies_db.usernames() for username in accounts_usernames: if not username in cookies_usernames: password = self.accounts_db.get(username) print('正在生成Cookies', '账号', username, '密码', password) result = self.new_cookies(username, password) # 成功获取 if result.get('status') == 1: cookies = self.process_cookies(result.get('content')) print('成功获取到Cookies', cookies) if self.cookies_db.set( username, json.dumps(cookies, ensure_ascii=False)): print('成功保存Cookies') # 密码错误,移除账号 elif result.get('status') == 2: print(result.get('content')) if self.accounts_db.delete(username): print('成功删除账号') else: print(result.get('content')) else: print('所有账号都已经成功获取Cookies') def close(self): """ 关闭 :return: """ try: print('Closing Browser') self.browser.close() del self.browser except TypeError: print('Browser not opened')