def proxy(targetUrl, num): url = getConfig.ReadConfig(confname, "换ip地址", 'ipUrl') data = request.getIP(url) print(data) PROXY = data['data'][num]['IP'] # 设置有账号密码的代理 # proxyauth_plugin_path = create_proxyauth_extension( # proxy_host='host', # proxy_port='port', # proxy_username="******", # proxy_password="******" # ) # options.add_extension(proxyauth_plugin_path) # 设置无账号密码的代理 # chromeOptions = webdriver.ChromeOptions() # chromeOptions.add_argument('--proxy-server=http://'+PROXY) # driver = webdriver.Chrome(chrome_options=chromeOptions) desired_capabilities = options.to_capabilities() desired_capabilities['proxy'] = { "httpProxy": PROXY, "ftpProxy": PROXY, "sslProxy": PROXY, "noProxy": None, "proxyType": "MANUAL", "class": "org.openqa.selenium.Proxy", "autodetect": False } webdriver.Chrome(desired_capabilities=desired_capabilities) request.checkIP() driver.get(targetUrl) print(driver.title)
class Log: global logPath logPath = getConfig.ReadConfig().get_logPath() def __init__(self): self.logger = logging.getLogger() self.logger.setLevel(logging.INFO) handler = logging.FileHandler(os.path.join(logPath, "output.log")) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) self.logger.addHandler(handler) def get_logger(self): return self.logger
def user(): numbers = getConfig.ReadConfig(confname, "IP次数", 'number') num = 1 user = getConfig.getText(filename) # print("user~~",user) for i in user: if len(i) >= 4: print("跳过当前账号,原因:", i[3]) else: print("账号", str(i[0]), "大区", str(i[2])) if int(numbers) != 0: if num < int(numbers): main(i[0], i[1], i[2], 1) else: main(i[0], i[1], i[2], num % int(numbers)) else: main(i[0], i[1], i[2], 1) num += 1
def main(user, password, area, number): driver.delete_all_cookies() areas = getConfig.ReadConfig(district, "大区配置", area) if areas: print("获取大区成功:", areas) else: pass print("获取大区失败:", areas) return # print("路径!!!",str(getConfig.ReadConfig(confname,"链接路径",'url'))) if number == 0: proxy( 'https://ui.ptlogin2.qq.com/cgi-bin/login?style=9&appid=549000929&pt_ttype=1&daid=5&pt_no_auth=1&pt_hide_ad=1&s_url=https%3A%2F%2Fact.qzone.qq.com%2Fvip%2F2019%2Fxcardv2%3F_wv%3D4%26zz%3D9%26from%3Darksend&pt_no_onekey=1', number) else: driver.get( 'https://ui.ptlogin2.qq.com/cgi-bin/login?style=9&appid=549000929&pt_ttype=1&daid=5&pt_no_auth=1&pt_hide_ad=1&s_url=https%3A%2F%2Fact.qzone.qq.com%2Fvip%2F2019%2Fxcardv2%3F_wv%3D4%26zz%3D9%26from%3Darksend&pt_no_onekey=1' ) print(time.strftime('%Y-%m-%d-%H:%M:%S', time.localtime(time.time()))) sleep(1) # 输入用户名和密码 driver.find_element_by_id('u').clear() driver.find_element_by_id('u').send_keys(user) sleep(1) driver.find_element_by_id('p').clear() driver.find_element_by_id('p').send_keys(password) sleep(1) # 触摸touch事件,登录 go = driver.find_element_by_id('go') Action = TouchActions(driver) Action.tap(go).perform() sleep(5) # 切换iframe try: iframe = driver.find_element_by_xpath('//*[@id="tcaptcha_iframe"]') except Exception as e: print('get iframe failed: ', e) sleep(2) # 等待资源加载 try: driver.switch_to.frame(iframe) except Exception as na: screeshot = os.getcwd() + "\\screenshot\\" if not screeshot: os.makedir(screeshot) else: print( "当前截图路径", screeshot, "保存图片的路径", screeshot + time.strftime( '%Y-%m-%d_%H_%M_%S_', time.localtime(time.time())) + '.png') driver.get_screenshot_as_file(screeshot + time.strftime( '%Y-%m-%d_%H_%M_%S_', time.localtime(time.time())) + '_' + user + '.png') return button = driver.find_element_by_id('tcaptcha_drag_button') # 寻找滑块 sleep(1) # 开始拖动 perform()用来执行ActionChains中存储的行为 flag = 0 distance = 200 offset = 5 times = 1 while 1: action = ActionChains(driver) action.click_and_hold(button).perform() action.reset_actions() # 清除之前的action print(distance) track = get_track(distance) for i in track: action.move_by_offset(xoffset=i, yoffset=0).perform() action.reset_actions() sleep(0.5) action.release().perform() sleep(3) # 判断某元素是否被加载到DOM树里,并不代表该元素一定可见 try: alert = '' message = '' alert = driver.find_element_by_id('tcaptcha_note').get_attribute( 'textContent') message = driver.find_element_by_id('guideText').text print(alert, message) except Exception as e: if alert: pass else: alert = '' if message: pass else: message = '' if alert and message: print('滑块位移需要调整: %s' % alert) distance -= offset times += 1 sleep(5) if times == 4: distance = 197 if times > 9: flag = 1 break else: print('滑块验证通过') flag = 1 driver.switch_to.parent_frame() # 验证成功后跳回最外层页面 break sleep(4) # 判断成功登录到活动页还是失败并且把失败原因打印出来 success = 0 try: error_message = '' error_message_open = '' error_message = driver.find_element_by_id( 'error_message').get_attribute('textContent') error_message_open = driver.find_element_by_class_name( 'qui-dialog-content').text except Exception as e: if error_message: pass else: error_message = '' if error_message_open: pass else: error_message_open = '' if error_message or error_message_open: print('登陆失败原因: %s' % error_message, error_message_open) sleep(1) getConfig.setText(filename, user, area, '登陆失败原因:' + error_message + error_message_open) getConfig.setText2(statusFail, user, password, area, '登陆失败原因:' + error_message + error_message_open) success = 1 else: print('成功登陆到活动页') sleep(2) success = 0 js = """ var hello = window.syncData; return hello; """ con = driver.execute_script(js) if con: card = 0 html = '' for key, value in con["actCount"].items(): for key, value in value.items(): for key, value in value.items(): card += 1 if card >= 11: html += " 名称:", value[0][ 'name'], "剩余:", value[0]['add'] + '\r\n' getConfig.setText2(statusFail, user, password, area, '未到达活动页,失败原因未知') else: print("活动页不存在", con) try: screeshot = os.getcwd() + "\\screenshot\\" if not screeshot: os.makedir(screeshot) else: print( "当前截图路径", screeshot, "保存图片的路径", screeshot + time.strftime('%Y-%m-%d_%H_%M_%S_', time.localtime(time.time())) + '.png') driver.get_screenshot_as_file(screeshot + time.strftime( '%Y-%m-%d_%H_%M_%S_', time.localtime(time.time())) + '_' + user + '.png') except NameError as na: logger.info("截图失败:%s" % na) getConfig.setText(filename, user, area, '未到达活动页,失败原因未知') getConfig.setText2(statusFail, user, password, area, '未到达活动页,失败原因未知') return if success == 1: return else: pass # 获取cookie参数,拼装参数写入文件并且带入到接口中请求 print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))) skey = driver.get_cookie("skey") uin = driver.get_cookie("uin") p_skey = driver.get_cookie("p_skey") # print("cookiestr","uin="+str(uin)+"; "+"skey="+str(skey)+";") cookiestr = "uin="+str(uin['value'])+"; "+"skey=" + \ str(skey['value'])+"; "+"p_skey="+str(p_skey['value'])+";" print("cookiestr", cookiestr) with open('cookie.text', 'a+', encoding='utf-8') as f: f.write(user + '----' + cookiestr + '\r\n') # 获取用户角当前大区角色信息 roleid = request.getUserInfo(cookiestr, areas) print("roleid", roleid) key = str(skey['value']) if p_skey: print("key==p_skey") key = str(p_skey['value']) # 看视频 request.getLookVideo(cookiestr, str(key), user) # 每日登录 request.getEveryDayLogin(cookiestr, str(key), user, areas, roleid) # 抽奖 count = 0 while count == 0: data = request.getLotteryDraw(cookiestr, str(key), user) if data < 0: count = count + 1 else: request.getLotteryDraw(cookiestr, str(key), user) print("继续抽奖") print("完成抽奖") getConfig.setText(filename, user, area, '完成抽奖') getConfig.setText2(statusSuccess, user, password, area, '完成抽奖')
from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait import getConfig # 引用获取配置文件模块 import request # 引入请求接口模块 filename = os.getcwd() + "\\user\\账号.txt" confname = os.getcwd() + "\\conf\\通用配置.ini" district = os.getcwd() + "\\conf\\大区配置.ini" statusSuccess = os.getcwd() + "\\status\\成功.txt" statusFail = os.getcwd() + "\\status\\成功.txt" statusFail = os.getcwd() + "\\status\\成功.txt" PROXY = '' options = webdriver.ChromeOptions() options.add_experimental_option('w3c', False) options.add_argument('user-agent=' + getConfig.ReadConfig(confname, "用户代理", 'ua')) driver = webdriver.Chrome(options=options) def proxy(targetUrl, num): url = getConfig.ReadConfig(confname, "换ip地址", 'ipUrl') data = request.getIP(url) print(data) PROXY = data['data'][num]['IP'] # 设置有账号密码的代理 # proxyauth_plugin_path = create_proxyauth_extension( # proxy_host='host', # proxy_port='port', # proxy_username="******", # proxy_password="******" # )