def AliAPI(base64, appcode): req = showapi.ShowapiRequest("http://ali-checkcode2.showapi.com/checkcode", appcode) json_res= req.addTextPara("typeId","38")\ .addTextPara("img_base64",base64)\ .addTextPara("convert_to_jpg","1")\ .post() print('json_res data is:', json_res) print json_res print json_res print 'jsonres[2][0]', json_res['showapi_res_body']['ret_code'] print 'jsonres[2][1]', json_res['showapi_res_body']['Result'] return json_res['showapi_res_body']['ret_code'], json_res[ 'showapi_res_body']['Result']
#裁切图片 img = Image.open(picName) region = (1070, 480, 1150, 515) cropImg = img.crop(region) #保存裁切后的图片 picNameCut=os.path.abspath('.')+'\\'+re.sub(r'[^0-9]','',str(datetime.datetime.now()))+'.png' cropImg.save(picNameCut) time.sleep(2) #进行验证码验证 f=open(picNameCut,'rb') b_64=base64.b64encode(f.read()) f.close() req=showapi.ShowapiRequest( "http://ali-checkcode.showapi.com/checkcode","4e5510e696c748ca8d5033dd595bfbbc" ) json_res=req.addTextPara("typeId","3040") \ .addTextPara("img_base64",b_64) \ .addTextPara("convert_to_jpg","1") \ .post() #print ('1') #print ('json_res data is:', json_res) print (json_res) json_res #str="{\"showapi_res_code\":0,\"showapi_res_error\":\"\",\"showapi_res_body\":{\"Result\":\"28ht\",\"ret_code\":0,\"Id\":\"adb1c363-d566-48a6-820e-55859428599d\"}}" int=json_res.find('Result') yanzhengma=json_res[int+11:int+15] print(yanzhengma)
def main(taskUrl, account, password, text): ''' 通过微博账号登录 ''' #args = Param() driver = webdriver.Firefox() # driver = webdriver.Chrome() # driver.get("http://news.sohu.com/20170717/n502205097.shtml") #driver.get("http://www.sohu.com/a/158321994_162702?loc=1&focus_pic=0") driver.get(taskUrl) driver.maximize_window() time.sleep(2) print('wait') # driver.execute_script("window.scrollBy(0,200)","") #向下滚动200px # driver.execute_script("window.scrollBy(0,document.body.scrollHeight)","") #向下滚动到页面底部 # driver.switch_to.frame('iframe') ############################################################## # 将页面滚动条拖到底部 # js = "var q=document.body.scrollTop=100000" js = "var q=document.documentElement.scrollTop=10000" # 该方法用来确认元素是否存在,如果存在返回flag=true,否则返回false def isElementExist(element): flag = True try: driver.find_element_by_xpath(element) return flag except: flag = False # driver.execute_script(js) return flag target = driver.find_element_by_xpath('//*[@id="mpbox"]/div[2]/div/div[1]/textarea') driver.execute_script("arguments[0].scrollIntoView();", target) # 拖动到可见的元素去 # 点击登录按钮 driver.find_element_by_xpath('//*[@id="mpbox"]/div[2]/div/div[3]').click() time.sleep(2) # 点击微博登录 driver.find_element_by_xpath('/html/body/div[4]/div[4]/ul/li[2]/a').click() time.sleep(2) # 获取当前窗口句柄 ch = driver.current_window_handle time.sleep(2) # 获取所有窗口句柄 wh = driver.window_handles # 在所有窗口中查找弹出窗口 for line in wh: if line != ch: driver.switch_to_window(line) time.sleep(2) driver.maximize_window() time.sleep(2) driver.find_element_by_xpath('//*[@id="userId"]').clear() time.sleep(2) driver.find_element_by_xpath('//*[@id="userId"]').send_keys(account) #driver.find_element_by_xpath('//*[@id="userId"]').send_keys("*****@*****.**") time.sleep(2) driver.find_element_by_xpath('//*[@id="passwd"]').clear() time.sleep(2) driver.find_element_by_xpath('//*[@id="passwd"]').send_keys(password) #driver.find_element_by_xpath('//*[@id="passwd"]').send_keys("7B83Od9c9UOB") time.sleep(2) if driver.find_element_by_xpath('//*[@id="outer"]/div/div[2]/form/div/div[1]/div[1]/p[3]/input').is_displayed(): # 判断是否有验证码 picName = os.path.abspath('.') + '\\' + re.sub(r'[^0-9]', '',str(datetime.datetime.now())) + '.png' driver.save_screenshot(picName) time.sleep(1) # 裁切图片 img = Image.open(picName) region = (1096, 207, 1171, 238) cropImg = img.crop(region) # 保存裁切后的图片 picNameCut = os.path.abspath('.') + '\\' + re.sub(r'[^0-9]', '',str(datetime.datetime.now())) + '.png' cropImg.save(picNameCut) time.sleep(2) # 进行验证码验证 f = open(picNameCut, 'rb') b_64 = base64.b64encode(f.read()) f.close() req = showapi.ShowapiRequest("http://ali-checkcode.showapi.com/checkcode","4e5510e696c748ca8d5033dd595bfbbc") json_res = req.addTextPara("typeId", "3050") \ .addTextPara("img_base64", b_64) \ .addTextPara("convert_to_jpg", "1") \ .post() # print ('1') # print ('json_res data is:', json_res) print (json_res) json_res # str="{\"showapi_res_code\":0,\"showapi_res_error\":\"\",\"showapi_res_body\":{\"Result\":\"28ht\",\"ret_code\":0,\"Id\":\"adb1c363-d566-48a6-820e-55859428599d\"}}" int = json_res.find('Result') yanzhengma = json_res[int + 11:int + 16] print(yanzhengma) driver.find_element_by_xpath('//*[@id="outer"]/div/div[2]/form/div/div[1]/div[1]/p[3]/input').send_keys(yanzhengma) time.sleep(1) # 点击登录 driver.find_element_by_xpath('//*[@id="outer"]/div/div[2]/form/div/div[2]/div/p/a[1]').click() time.sleep(2) if isElementExist('//*[@id="outer"]/div/div[2]/form/div/div[2]/div/p/a[1]'): # 点击授权 driver.find_element_by_xpath('//*[@id="outer"]/div/div[2]/form/div/div[2]/div/p/a[1]').click() time.sleep(2) # driver.switch_to_window(ch) target = driver.find_element_by_xpath('//*[@id="mpbox"]/div[2]/div/div[1]/textarea') driver.execute_script("arguments[0].scrollIntoView();", target) # 拖动到可见的元素去 print('找到评论输入框') driver.find_element_by_xpath('//*[@id="mpbox"]/div[2]/div/div[1]/textarea').clear() time.sleep(2) driver.find_element_by_xpath('//*[@id="mpbox"]/div[2]/div/div[1]/textarea').send_keys(text.decode()) time.sleep(2) driver.find_element_by_xpath('//*[@id="mpbox"]/div[2]/div/div[3]').click() print('end')
def getCode(driver, APP_KEY, CALLBACK_URL, username, password): #driver.get('https://api.weibo.com/oauth2/authorize?response_type=code&client_id=1851011061 # &redirect_uri=https://api.weibo.com/oauth2/default.html') nickName = username driver.get( 'https://api.weibo.com/oauth2/authorize?response_type=code&client_id=' + APP_KEY + '&redirect_uri=' + CALLBACK_URL) driver.maximize_window() time.sleep(2) WebDriverWait(driver, 30).until( lambda x: x.find_element_by_xpath('//*[@id="userId"]')).click() #driver.find_element_by_xpath('//*[@id="userId"]').click() time.sleep(1) driver.find_element_by_xpath('//*[@id="userId"]').clear() time.sleep(1) driver.find_element_by_xpath('//*[@id="userId"]').send_keys(username) time.sleep(2) driver.find_element_by_xpath('//*[@id="passwd"]').click() time.sleep(1) driver.find_element_by_xpath('//*[@id="passwd"]').clear() time.sleep(1) driver.find_element_by_xpath('//*[@id="passwd"]').send_keys(password) time.sleep(2) #点击登录 driver.find_element_by_xpath( '//*[@id="outer"]/div/div[2]/form/div/div[2]/div/p/a[1]').click() time.sleep(8) if driver.current_url == 'https://api.weibo.com/oauth2/authorize': print('点击授权') logger.info('点击授权') time.sleep(1) nickName = driver.find_element_by_xpath( '/html/body/div/div/div[1]/p/span[1]').text print nickName logger.info(nickName) time.sleep(1) driver.find_element_by_xpath( '//*[@id="outer"]/div/div[2]/form/div/div[2]/div/p/a[1]').click() time.sleep(4) if isElementExist( '//*[@id="outer"]/div/div[2]/form/div/div[1]/div[1]/p[3]/span/img', driver): if driver.find_element_by_xpath( '//*[@id="outer"]/div/div[2]/form/div/div[1]/div[1]/p[3]/span/img' ).is_displayed(): # 如果存在验证码图片 i = 0 while i < 6: print('有验证码') logger.info('有验证码') print(i) logger.info(i) picName = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' driver.save_screenshot(picName) time.sleep(1) # 裁切图片 img = Image.open(picName) region = (1095, 208, 1169, 240) cropImg = img.crop(region) # 保存裁切后的图片 picNameCut = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' cropImg.save(picNameCut) time.sleep(2) # 进行验证码验证 f = open(picNameCut, 'rb') b_64 = base64.b64encode(f.read()) f.close() req = showapi.ShowapiRequest( "http://ali-checkcode.showapi.com/checkcode", "4e5510e696c748ca8d5033dd595bfbbc") json_res = req.addTextPara("typeId", "3050") \ .addTextPara("img_base64", b_64) \ .addTextPara("convert_to_jpg", "1") \ .post() # print ('1') # print ('json_res data is:', json_res) print(json_res) logger.info(json_res) json_res # str="{\"showapi_res_code\":0,\"showapi_res_error\":\"\",\"showapi_res_body\":{\"Result\":\"28ht\",\"ret_code\":0,\"Id\":\"adb1c363-d566-48a6-820e-55859428599d\"}}" result = json.loads(str(json_res[1:-3]).replace('\\', '')) yanzhengma = result['showapi_res_body']['Result'] print(yanzhengma) logger.info(yanzhengma) time.sleep(1) os.remove(picName) time.sleep(1) os.remove(picNameCut) time.sleep(1) driver.find_element_by_xpath( '//*[@id="outer"]/div/div[2]/form/div/div[1]/div[1]/p[3]/input' ).click() time.sleep(1) driver.find_element_by_xpath( '//*[@id="outer"]/div/div[2]/form/div/div[1]/div[1]/p[3]/input' ).clear() time.sleep(1) driver.find_element_by_xpath( '//*[@id="outer"]/div/div[2]/form/div/div[1]/div[1]/p[3]/input' ).send_keys(yanzhengma) time.sleep(3) #点击登录 driver.find_element_by_xpath( '//*[@id="outer"]/div/div[2]/form/div/div[2]/div/p/a[1]' ).click() time.sleep(8) if driver.current_url == 'https://api.weibo.com/oauth2/authorize': print('点击授权') logger.info('点击授权') time.sleep(1) nickName = driver.find_element_by_xpath( '/html/body/div/div/div[1]/p/span[1]').text print nickName logger.info(nickName) time.sleep(1) driver.find_element_by_xpath( '//*[@id="outer"]/div/div[2]/form/div/div[2]/div/p/a[1]' ).click() time.sleep(4) if driver.current_url.find('code=') >= 0: print('授权回调页返回code') logger.info('授权回调页返回code') break if i == 5: print('验证码识别次数超过三次,取消本次授权') logger.info('验证码识别次数超过三次,取消本次授权') break i = i + 1 # else: # print('没有验证码') # #点击登录 # # driver.find_element_by_xpath('//*[@id="outer"]/div/div[2]/form/div/div[2]/div/p/a[1]').click() # time.sleep(8) # if driver.current_url == 'https://api.weibo.com/oauth2/authorize': # print('点击授权') # time.sleep(1) # driver.find_element_by_xpath('//*[@id="outer"]/div/div[2]/form/div/div[2]/div/p/a[1]').click() # time.sleep(4) # 获取页面title current_title = driver.title # 获取页面url current_url = driver.current_url print current_title logger.info(current_title) print current_url logger.info(current_url) code = current_url[current_url.index('code=') + 5:] return code, nickName
def main(): for num in range(1, 51): try: print(num) # 格式化成2016-03-20 11:45:39形式 print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) time.sleep(2) driver = webdriver.Firefox() driver.get('http://www.acfun.cn/reg/') driver.maximize_window() time.sleep(2) while True: token = getToken() print(token) phone = getPhone(token) #phone = '17097517691' print(phone) print('开始注册,本次手机号为' + phone) # 输入手机号 driver.find_element_by_xpath( '//*[@id="ipt-mobile-reg"]').clear() time.sleep(2) driver.find_element_by_xpath( '//*[@id="ipt-mobile-reg"]').send_keys(phone) time.sleep(2) #点击其他输入框会显示手机号是否可用,为后面判断做准备 driver.find_element_by_xpath( '//*[@id="ipt-username-reg"]').click() time.sleep(2) if (driver.find_element_by_xpath( '//*[@id="ipt-mobile-reg"]').get_attribute('class') ) == 'mobile l success'.decode("utf-8"): print('该手机号可用') break print('------该手机号已注册过------') # 输入昵称 driver.find_element_by_xpath( '//*[@id="ipt-username-reg"]').send_keys(phone + token[:5]) time.sleep(2) # if (driver.find_element_by_xpath('//*[@id="win-hint"]/div[1]').text)==None: # break # else: # driver.find_element_by_xpath('//*[@id="ipt-username-reg"]').click() # time.sleep(2) # driver.find_element_by_xpath('//*[@id="ipt-username-reg"]').clear() # time.sleep(2) # driver.find_element_by_xpath('//*[@id="ipt-username-reg"]').send_keys(token[:10]) # time.sleep(2) # 输入密码 driver.find_element_by_xpath('//*[@id="ipt-pwd-reg"]').send_keys( 'abc' + phone) time.sleep(2) driver.find_element_by_xpath('//*[@id="ipt-pwd-reg"]').click() time.sleep(2) # 确认密码 driver.find_element_by_xpath('//*[@id="ipt-repwd-reg"]').send_keys( 'abc' + phone) time.sleep(2) driver.find_element_by_xpath('//*[@id="ipt-repwd-reg"]').click() time.sleep(2) # if not isElementExist('/html/body/div[1]/div[1]/div/form/div[1]/span[2]/text()',driver): # print('该账号可用') # break # 点击免费获取短信动态码 driver.find_element_by_xpath('//*[@id="send-mobile-code"]').click() time.sleep(2) i = 1 while True: driver.find_element_by_xpath('//*[@id="popupCode"]').clear() time.sleep(2) driver.find_element_by_xpath('//*[@id="popupCode"]').click() time.sleep(3) # 截图验证码 picName = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' driver.save_screenshot(picName) time.sleep(1) # 裁切图片 img = Image.open(picName) region = (870, 400, 950, 440) cropImg = img.crop(region) # 保存裁切后的图片 picNameCut = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' cropImg.save(picNameCut) time.sleep(2) # 进行验证码验证 f = open(picNameCut, 'rb') b_64 = base64.b64encode(f.read()) f.close() req = showapi.ShowapiRequest( "http://ali-checkcode.showapi.com/checkcode", "4e5510e696c748ca8d5033dd595bfbbc") json_res = req.addTextPara("typeId", "3040") \ .addTextPara("img_base64", b_64) \ .addTextPara("convert_to_jpg", "1") \ .post() # print ('1') # print ('json_res data is:', json_res) print(json_res) # str="{\"showapi_res_code\":0,\"showapi_res_error\":\"\",\"showapi_res_body\":{\"Result\":\"28ht\",\"ret_code\":0,\"Id\":\"adb1c363-d566-48a6-820e-55859428599d\"}}" int = json_res.find('Result') yanzhengma = json_res[int + 11:int + 15] print(yanzhengma) time.sleep(2) driver.find_element_by_xpath('//*[@id="popupCode"]').send_keys( yanzhengma) time.sleep(2) os.remove(picName) time.sleep(2) os.remove(picNameCut) time.sleep(2) # 点击确定 driver.find_element_by_xpath( '//*[@id="win-verifycode"]/div[2]/div/div[1]/a[2]').click( ) time.sleep(5) if not isElementExist('//*[@id="popupCode"]', driver): break if i > 3: print('已重试三次,退出') driver.quit() i = i + 1 time.sleep(30) getMessage(token, phone, driver) time.sleep(5) driver.find_element_by_xpath( '//*[@id="form-reg"]/div[7]/a').click() time.sleep(8) if not isElementExist('//*[@id="form-reg"]/div[7]/a', driver): addCount(token, phone) time.sleep(1) print('手机号' + phone + '注册成功') black(token, phone) print('已将该号码拉入黑名单') else: print('注册有误,退出') except Exception as e: print(e) finally: print('本次注册结束') print('---------我是分割线------------') driver.quit()
def main(): for num in range(1, 51): try: print(num) # 格式化成2016-03-20 11:45:39形式 print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) driver = webdriver.Firefox() driver.get('https://passport.bilibili.com/register/phone.html') driver.maximize_window() time.sleep(2) while True: token = getToken() # print(token) phone = getPhone(token) # phone = '17173754143' # print(phone) print('开始注册,本次手机号为' + phone) # 输入手机号 driver.find_element_by_xpath('//*[@id="new_phone"]').send_keys( phone) time.sleep(2) # 点击免费获取短信动态码 driver.find_element_by_xpath('//*[@id="getCaptch"]').click() time.sleep(5) i = 1 while True: driver.find_element_by_xpath( '//*[@id="login-app"]/div/div/div[3]/div/div/div[2]/div[1]/input' ).click() time.sleep(3) driver.find_element_by_xpath( '//*[@id="login-app"]/div/div/div[3]/div/div/div[2]/div[1]/input' ).clear() time.sleep(2) # 截图验证码 picName = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' driver.save_screenshot(picName) time.sleep(1) # 裁切图片 img = Image.open(picName) region = (900, 440, 1010, 470) cropImg = img.crop(region) # 保存裁切后的图片 picNameCut = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' cropImg.save(picNameCut) time.sleep(2) # 进行验证码验证 f = open(picNameCut, 'rb') b_64 = base64.b64encode(f.read()) f.close() req = showapi.ShowapiRequest( "http://ali-checkcode.showapi.com/checkcode", "4e5510e696c748ca8d5033dd595bfbbc") json_res = req.addTextPara("typeId", "3050") \ .addTextPara("img_base64", b_64) \ .addTextPara("convert_to_jpg", "1") \ .post() # print ('1') # print ('json_res data is:', json_res) print(json_res) # str="{\"showapi_res_code\":0,\"showapi_res_error\":\"\",\"showapi_res_body\":{\"Result\":\"28ht\",\"ret_code\":0,\"Id\":\"adb1c363-d566-48a6-820e-55859428599d\"}}" int = json_res.find('Result') yanzhengma = json_res[int + 11:int + 16] print(yanzhengma) time.sleep(2) driver.find_element_by_xpath( '//*[@id="login-app"]/div/div/div[3]/div/div/div[2]/div[1]/input' ).send_keys(yanzhengma) time.sleep(2) os.remove(picName) time.sleep(2) os.remove(picNameCut) time.sleep(2) # 点击确定 driver.find_element_by_xpath( '//*[@id="login-app"]/div/div/div[3]/div/div/div[2]/div[2]/input[1]' ).click() time.sleep(5) if not isElementExist( '//*[@id="login-app"]/div/div/div[3]/div/div/div[2]/div[' '1]/img', driver): # 判断验证码是否存在 break if i > 3: print('已重试三次,退出') driver.quit() i = i + 1 if (driver.find_element_by_xpath( '//*[@id="registerForm"]/div[6]/p').text) == ( '验证码已发到你手机上了,5分钟内有效。'.decode("utf-8")): print('该手机号可用') break print('------该手机号已注册过,重新获取------') driver.find_element_by_xpath('//*[@id="new_phone"]').click() time.sleep(2) driver.find_element_by_xpath('//*[@id="new_phone"]').clear() time.sleep(2) # 输入昵称 driver.find_element_by_xpath( '//*[@id="registerForm"]/div[1]/input[2]').send_keys(phone + token[:5]) time.sleep(2) # if (driver.find_element_by_xpath('//*[@id="unameTip"]').text)!=None: # # # driver.find_element_by_xpath('//*[@id="registerForm"]/div[1]/input[2]').click() # time.sleep(2) # driver.find_element_by_xpath('//*[@id="registerForm"]/div[1]/input[2]').clear() # time.sleep(2) # driver.find_element_by_xpath('//*[@id="registerForm"]/div[1]/input[' # '2]').send_keys(token[:10]) # time.sleep(2) # 输入密码 driver.find_element_by_xpath( '//*[@id="registerForm"]/div[3]/input').send_keys('abc' + phone) time.sleep(2) time.sleep(30) getMessage(token, phone, driver) time.sleep(5) driver.find_element_by_xpath('//*[@id="agree"]').click() time.sleep(2) driver.find_element_by_xpath( '//*[@id="registerForm"]/input').click() time.sleep(8) if not isElementExist('//*[@id="registerForm"]/input', driver): addCount(token, phone) time.sleep(1) print('手机号' + phone + '注册成功') time.sleep(2) black(token, phone) print('已将该号码拉入黑名单') else: print('注册有误,退出') except Exception as e: print(e) finally: print('本次注册结束') print('---------我是分割线------------') driver.quit()
def main(): for num in range(1, 2): try: print(num) print('开始注册') # 格式化成2016-03-20 11:45:39形式 print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) #print('开始注册,本次手机号为' + phone) iplist = ['47.93.113.175:5818', '59.110.159.237:5818', '123.56.77.123:5818', '123.56.76.207:5818', '123.56.72.115:5818', '123.56.154.24:5818', '123.56.44.11:5818', '123.56.228.93:5818', '123.57.48.138:5818', '101.200.76.126:5818'] #proxy_ip = iplist[random.randint(0, len(iplist)-1)] proxy_ip = random.choice(iplist) ip_ip = proxy_ip.split(":")[0] ip_port = (proxy_ip.split(":")[1]) if num%10 < 7: i = 1 #1使用代理 print(requests.get('http://ip.chinaz.com/getip.aspx', proxies={"http":'http://'+proxy_ip}).text) else: i = 0 print(requests.get('http://ip.chinaz.com/getip.aspx').text) profile = webdriver.FirefoxProfile() profile.set_preference('network.proxy.type', i) profile.set_preference('network.proxy.http', ip_ip) profile.set_preference('network.proxy.http_port', ip_port) # int profile.update_preferences() driver = webdriver.Firefox(firefox_profile=profile) #driver = webdriver.Firefox() driver.get('https://passport.sohu.com/signup') driver.maximize_window() time.sleep(2) i = 0 while i < 21: token = getToken() #print(token) time.sleep(1) phone = getPhone(token) #phone = 17088927105 #print(phone) time.sleep(1) driver.find_element_by_xpath('//*[@id="regForm"]/div[2]/label/span[2]/input').clear() time.sleep(1) driver.find_element_by_xpath('//*[@id="regForm"]/div[2]/label/span[2]/input').send_keys(phone) time.sleep(2) driver.find_element_by_xpath('//*[@id="regForm"]/div[3]/label/span[2]/input').click() time.sleep(2) if not isElementExist('//*[@id="regForm"]/div[2]/div/a', driver): print(phone + '该手机号可注册') break #if not driver.find_element_by_xpath('//*[@id="regForm"]/div[2]/div').is_displayed(): # break i = i+1 if i == 20: driver.quit() driver.find_element_by_xpath('//*[@id="regForm"]/div[3]/label/span[2]/input').clear() driver.find_element_by_xpath('//*[@id="regForm"]/div[3]/label/span[2]/input').send_keys( 'abc' + phone) time.sleep(2) # 获取图片验证码 driver.find_element_by_xpath('//*[@id="regForm"]/div[4]/label/a').click() time.sleep(2) #print('已经点击验证码') ######################循环输入验证码 j = 0 while j < 6: if not driver.find_element_by_xpath('/html/body/div[3]/div[2]/div[' '1]/span/a/img').is_displayed(): print('输入正确,退出') break #print('进入验证码') driver.find_element_by_xpath('/html/body/div[3]/div[2]/div[1]/label/span/input').clear() picName = os.path.abspath('.') + '\\' + re.sub(r'[^0-9]', '', str(datetime.datetime.now())) + '.png' driver.save_screenshot(picName) time.sleep(1) # 裁切图片 img = Image.open(picName) region = (988, 337, 1116, 383) cropImg = img.crop(region) # 保存裁切后的图片 picNameCut = os.path.abspath('.') + '\\' + re.sub(r'[^0-9]', '', str(datetime.datetime.now())) + '.png' cropImg.save(picNameCut) time.sleep(2) # 进行验证码验证 f = open(picNameCut, 'rb') b_64 = base64.b64encode(f.read()) f.close() req = showapi.ShowapiRequest("http://ali-checkcode.showapi.com/checkcode", "4e5510e696c748ca8d5033dd595bfbbc") json_res = req.addTextPara("typeId", "3040") \ .addTextPara("img_base64", b_64) \ .addTextPara("convert_to_jpg", "1") \ .post() # print ('1') # print ('json_res data is:', json_res) print (json_res) json_res # str="{\"showapi_res_code\":0,\"showapi_res_error\":\"\",\"showapi_res_body\":{\"Result\":\"28ht\",\"ret_code\":0,\"Id\":\"adb1c363-d566-48a6-820e-55859428599d\"}}" int = json_res.find('Result') yanzhengma = json_res[int + 11:int + 15] print(yanzhengma) driver.find_element_by_xpath('/html/body/div[3]/div[2]/div[1]/label/span/input').send_keys( yanzhengma) time.sleep(1) # 点击确认 driver.find_element_by_xpath('/html/body/div[3]/div[2]/div[3]/input').click() time.sleep(2) #print(i) os.remove(picName) time.sleep(2) os.remove(picNameCut) time.sleep(2) j = j + 1 if j == 5: driver.quit() ######################### time.sleep(35) getMessage(token, phone, driver) time.sleep(2) #同意条款 driver.find_element_by_xpath('//*[@id="regForm"]/div[5]/div/span/label/input').click() time.sleep(2) #注册 driver.find_element_by_xpath('//*[@id="regForm"]/div[6]/div/span/input').click() time.sleep(2) if not isElementExist('//*[@id="regForm"]/div[6]/div/span/input', driver): addCount(phone) time.sleep(2) print('手机号' + phone + '注册成功') time.sleep(2) black(token, phone) time.sleep(2) else: print('注册有误,退出') except Exception as e: print (e) finally: print('---------我是分割线------------') driver.quit()
def main(taskUrl, data_id, accountId, password, count): for num in range(1, int(count) + 1): try: print(num) # 格式化成2016-03-20 11:45:39形式 print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) js = "var q=document.documentElement.scrollTop=-50" print('开始点赞') ''' iplist = ['123.56.154.24:5818', '59.110.159.237:5818', '47.93.113.175:5818', '123.56.44.11:5818', '101.200.76.126:5818', '123.56.228.93:5818', '123.57.48.138:5818', '123.56.72.115:5818', '123.56.77.123:5818', '123.56.76.207:5818', '47.93.85.217:5818', '59.110.23.162:5818', '47.92.32.50:5818', '47.91.241.124:5818'] #proxy_ip1 = iplist[random.randint(0, len(iplist)-1)] proxy_ip = random.choice(iplist) ip_ip = proxy_ip.split(":")[0] ip_port = int(proxy_ip.split(":")[1]) num = random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) if num < 15: isDaili = 1 # 1使用代理 print(requests.get('http://ip.chinaz.com/getip.aspx', proxies={"http": 'http://' + proxy_ip}).text) else: isDaili = 0 print(requests.get('http://ip.chinaz.com/getip.aspx').text) profile = webdriver.FirefoxProfile() profile.set_preference('network.proxy.type', isDaili) profile.set_preference('network.proxy.http', ip_ip) profile.set_preference('network.proxy.http_port', ip_port) # int profile.update_preferences() driver = webdriver.Firefox(firefox_profile=profile) ''' driver = webdriver.Firefox() driver.get(taskUrl) driver.maximize_window() time.sleep(2) # m = driver.find_element_by_xpath("//*[contains(@class,'c-comment-more')]").text # n = m.find(u'人参与') # i = m[2:n] target = driver.find_element_by_xpath( "//*[contains(@class,'c-comment-more')]") driver.execute_script("arguments[0].scrollIntoView();", target) # 拖动到可见的元素去 time.sleep(3) if driver.find_element_by_xpath( '//*[@id="sohu-mod"]').is_displayed(): print('点击关闭返回主页按钮') # driver.execute_script('var aaa=document.getElementById( # "sohu-mod").style.display="none"') # driver.execute_script('document.getElementById("sohu-mod").firstElementChild.lastElementChild.childNodes[3].firstElementChild.href="baidu.com"') driver.find_element_by_xpath( '//*[@id="sohu-mod"]/div/div[2]/a').click() while isElementExist("//*[contains(@class,'c-comment-more')]", driver): time.sleep(2) driver.execute_script(js) time.sleep(2) driver.find_element_by_xpath( "//*[contains(@class,'c-comment-more')]").click() time.sleep(2) if isElementExist( "//*[contains(@data-id," + data_id + ")]/div[2]/div[2]/div[2]/a[3]/i", driver): print('找到指定评论') break target = driver.find_element_by_xpath( "//*[contains(@class,'c-comment-more')]") driver.execute_script("arguments[0].scrollIntoView();", target) # 拖动到可见的元素去 driver.find_element_by_xpath( "//*[contains(@data-id," + data_id + ")]/div[2]/div[2]/div[2]/a[3]/i").click() time.sleep(2) driver.find_element_by_xpath( '/html/body/div[4]/div[3]/ul/li[1]/input').send_keys(accountId) time.sleep(2) driver.find_element_by_xpath( '/html/body/div[4]/div[3]/ul/li[2]/input').send_keys(password) time.sleep(2) driver.find_element_by_xpath( '/html/body/div[4]/div[3]/div[2]/input').click() time.sleep(2) while driver.find_element_by_xpath( '/html/body/div[4]/div[3]/ul/li[3]/img').is_displayed(): print('有验证码') driver.find_element_by_xpath( '/html/body/div[4]/div[3]/ul/li[3]/img').click() time.sleep(2) picName = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' driver.save_screenshot(picName) time.sleep(1) # 裁切图片 img = Image.open(picName) region = (955, 520, 1058, 556) cropImg = img.crop(region) # 保存裁切后的图片 picNameCut = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' cropImg.save(picNameCut) time.sleep(2) # 进行验证码验证 f = open(picNameCut, 'rb') b_64 = base64.b64encode(f.read()) f.close() req = showapi.ShowapiRequest( "http://ali-checkcode.showapi.com/checkcode", "4e5510e696c748ca8d5033dd595bfbbc") json_res = req.addTextPara("typeId", "3040") \ .addTextPara("img_base64", b_64) \ .addTextPara("convert_to_jpg", "1") \ .post() # print ('1') # print ('json_res data is:', json_res) print(json_res) json_res # str="{\"showapi_res_code\":0,\"showapi_res_error\":\"\",\"showapi_res_body\":{\"Result\":\"28ht\",\"ret_code\":0,\"Id\":\"adb1c363-d566-48a6-820e-55859428599d\"}}" f = json_res.find('Result') yanzhengma = json_res[f + 11:f + 15] print(yanzhengma) time.sleep(3) driver.find_element_by_xpath( '/html/body/div[4]/div[3]/ul/li[3]/input').click() time.sleep(3) driver.find_element_by_xpath( '/html/body/div[4]/div[3]/ul/li[3]/input').clear() time.sleep(3) driver.find_element_by_xpath( '/html/body/div[4]/div[3]/ul/li[3]/input').send_keys( yanzhengma) time.sleep(3) driver.find_element_by_xpath( '/html/body/div[4]/div[3]/div[2]/input').click() time.sleep(8) os.remove(picName) time.sleep(2) os.remove(picNameCut) time.sleep(2) driver.find_element_by_xpath( "//*[contains(@data-id," + data_id + ")]/div[2]/div[2]/div[2]/a[3]/i").click() time.sleep(2) except Exception as e: print(e) finally: print('---------我是分割线------------') driver.quit()
def main(taskUrl, accountId, password, content): try: print('开始本次评论任务') # 格式化成2016-03-20 11:45:39形式 print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) ''' iplist = ['123.56.154.24:5818', '59.110.159.237:5818', '47.93.113.175:5818', '123.56.44.11:5818', '101.200.76.126:5818', '123.56.228.93:5818', '123.57.48.138:5818', '123.56.72.115:5818', '123.56.77.123:5818', '123.56.76.207:5818', '47.93.85.217:5818', '59.110.23.162:5818', '47.92.32.50:5818', '47.91.241.124:5818'] # proxy_ip1 = iplist[random.randint(0, len(iplist)-1)] proxy_ip = random.choice(iplist) ip_ip = proxy_ip.split(":")[0] ip_port = proxy_ip.split(":")[1] num = random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) if num < 15: isDaili = 1 # 1使用代理 print(requests.get('http://ip.chinaz.com/getip.aspx', proxies={"http": 'http://' + proxy_ip}).text) else: isDaili = 0 print(requests.get('http://ip.chinaz.com/getip.aspx').text) ip_ip = httpIp.split(":")[0] ip_port = int(httpIp.split(":")[1]) profile = webdriver.FirefoxProfile() profile.set_preference('network.proxy.type', 0) profile.set_preference('network.proxy.http', ip_ip) profile.set_preference('network.proxy.http_port', ip_port) # int profile.update_preferences() driver = webdriver.Firefox(firefox_profile=profile) ''' driver = webdriver.Firefox() # args = Param() # driver = webdriver.Chrome() # driver.get("http://www.iqiyi.com/v_19rr80hvpc.html#vfrm=24-9-0-1") driver.get(taskUrl) driver.maximize_window() time.sleep(2) # print('wait') # driver.execute_script("window.scrollBy(0,200)","") #向下滚动200px # driver.execute_script("window.scrollBy(0,document.body.scrollHeight)","") #向下滚动到页面底部 # driver.switch_to.frame('iframe') ############################################################## # 将页面滚动条拖到底部 # js = "var q=document.body.scrollTop=100000" js = "var q=document.documentElement.scrollTop=10000" # 该方法用来确认元素是否存在,如果存在返回flag=true,否则返回false def isElementExist(element): flag = True try: driver.find_element_by_xpath(element) return flag except: flag = False # driver.execute_script(js) return flag i = 0 while i < 30: if isElementExist( '//*[@id="qitancommonarea"]/div/div[2]/div/div/div/div[2]/div/div[2]/div[1]/div/div[1]/div[1]/div/textarea' ) or isElementExist("//*[contains(@id, 'arguments')]"): # exist=False print('找到评论框') break driver.execute_script(js) i += 1 time.sleep(2) print(i) if (driver.find_element_by_class_name('csPp_textarea').get_attribute( 'placeholder')) == '我来说两句...'.decode("utf-8"): print('进入1') # target = driver.find_element_by_xpath( # '//*[@id="qitancommonarea"]/div/div[2]/div/div/div/div[2]/div/div[2]/div[1]/div/div[1]/div[1]/div/textarea') # driver.execute_script("arguments[0].scrollIntoView();", target) # 拖动到可见的元素去 # 输入评论 driver.find_element_by_xpath( '//*[@id="qitancommonarea"]/div/div[2]/div/div/div/div[2]/div/div[2]/div[1]/div/div[1]/div[1]/div/textarea' ).clear() time.sleep(2) driver.find_element_by_xpath( '//*[@id="qitancommonarea"]/div/div[2]/div/div/div/div[2]/div/div[2]/div[1]/div/div[1]/div[1]/div/textarea' ).send_keys(content) # 打印评论内容 # print(driver.find_element_by_xpath('//*[@id="qitancommonarea"]/div[2]/div/div/div/div[ # 2]/div/div[1]/div/div/div[2]/div[1]/textarea').get_attribute('value')) # 点击评论 driver.find_element_by_xpath( '//*[@id="qitancommonarea"]/div/div[2]/div/div/div/div[2]/div/div[2]/div[1]/div/div[1]/div[2]/div/div[2]/a' ).click() time.sleep(2) # # 点击登录 # driver.find_element_by_xpath( # '//*[@id="widget-userregistlogin"]/span[2]/div[1]/a').click() # time.sleep(5) # 截图验证码并进行剪裁 if isElementExist( '/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div'): driver.switch_to.frame('login_frame') time.sleep(2) # 如果点击登陆之后要求扫码登录,选择点击账号密码登录 if (driver.find_element_by_xpath( '/html/body/div[2]/div[1]/div/div/div[6]' ).get_attribute('class')) == 'login-frame'.decode("utf-8"): driver.find_element_by_xpath( '/html/body/div[2]/div[1]/div/div/div[6]/div[2]/p/span/a[1]' ).click() # 输入账号 driver.find_element_by_xpath( '/html/body/div[2]/div[1]/div/div/div[1]/div[1]/div/div[1]/div[' '2]/input').clear() time.sleep(2) driver.find_element_by_xpath( '/html/body/div[2]/div[1]/div/div/div[1]/div[1]/div/div[1]/div[' '2]/input').send_keys(accountId) # 输入密码 # driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div/div[1]/div[1]/div/div[2]/div/p').clear() time.sleep(2) driver.find_element_by_xpath( '/html/body/div[2]/div[1]/div/div/div[1]/div[' '1]/div/div[2]/div/input[1]').send_keys(password) # 点击登录 time.sleep(2) driver.find_element_by_xpath( '/html/body/div[2]/div[1]/div/div/div[1]/div[1]/div/a[2]' ).click() time.sleep(6) while isElementExist( '/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/img' ): # 如果存在验证码图片 print('有验证码') picName = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' driver.save_screenshot(picName) time.sleep(1) # 裁切图片 img = Image.open(picName) # 改版前 2017-8-10之前 # region = (73, 110, 220, 160) region = (124, 163, 284, 213) cropImg = img.crop(region) # 保存裁切后的图片 picNameCut = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' cropImg.save(picNameCut) time.sleep(2) # 进行验证码验证 f = open(picNameCut, 'rb') b_64 = base64.b64encode(f.read()) f.close() req = showapi.ShowapiRequest( "http://ali-checkcode.showapi.com/checkcode", "4e5510e696c748ca8d5033dd595bfbbc") json_res = req.addTextPara("typeId", "3040") \ .addTextPara("img_base64", b_64) \ .addTextPara("convert_to_jpg", "1") \ .post() # print ('1') # print ('json_res data is:', json_res) print(json_res) json_res # str="{\"showapi_res_code\":0,\"showapi_res_error\":\"\",\"showapi_res_body\":{\"Result\":\"28ht\",\"ret_code\":0,\"Id\":\"adb1c363-d566-48a6-820e-55859428599d\"}}" int = json_res.find('Result') yanzhengma = json_res[int + 11:int + 15] print(yanzhengma) time.sleep(3) driver.find_element_by_xpath( '/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input' ).click() time.sleep(3) driver.find_element_by_xpath( '/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input' ).clear() time.sleep(3) driver.find_element_by_xpath( '/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input' ).send_keys(yanzhengma) time.sleep(3) driver.find_element_by_xpath( '/html/body/div[2]/div[1]/div/div/div[2]/div/div/a[2]' ).click() time.sleep(8) os.remove(picName) time.sleep(2) os.remove(picNameCut) time.sleep(2) print('登录成功') driver.switch_to_default_content() j = 0 while j < 30: if isElementExist( '//*[@id="qitancommonarea"]/div/div[2]/div/div/div/div[2]/div/div[2]/div[1]/div/div[1]/div[1]/div/textarea') \ or isElementExist( '//*[@id="arguments"]/div/div[1]/div/div/div/div[1]/div/textarea'): # exist=False print('找到评论框') break driver.execute_script(js) j += 1 time.sleep(2) # print(j) # target = driver.find_element_by_xpath( # '//*[@id="qitancommonarea"]/div[2]/div/div/div/div[' # '2]/div/div[1]/div/div/div[2]/div[1]/textarea') # driver.execute_script("arguments[0].scrollIntoView();", target) # 拖动到可见的元素去 # # time.sleep(5) # 输入评论 driver.find_element_by_xpath( '//*[@id="qitancommonarea"]/div/div[2]/div/div/div/div[2]/div/div[2]/div[1]/div/div[1]/div[1]/div/textarea' ).clear() time.sleep(2) driver.find_element_by_xpath( '//*[@id="qitancommonarea"]/div/div[2]/div/div/div/div[2]/div/div[2]/div[1]/div/div[1]/div[1]/div/textarea' ).send_keys(content) # 打印评论内容 # print(driver.find_element_by_xpath('//*[@id="qitancommonarea"]/div[2]/div/div/div/div[ # 2]/div/div[1]/div/div/div[2]/div[1]/textarea').get_attribute('value')) # 点击评论 driver.find_element_by_xpath( '//*[@id="qitancommonarea"]/div/div[2]/div/div/div/div[2]/div/div[2]/div[1]/div/div[1]/div[2]/div/div[2]/a' ).click() time.sleep(2) print('本次评论任务成功') #之前的定位出现问题2018-01-10 # if isElementExist('//*[@id="qitancommonarea"]/div[2]/div/div/div/div[2]/div/div[1]/div/div/div[''2]/div[1]/textarea'): # # print('进入1') # # target = driver.find_element_by_xpath( # '//*[@id="qitancommonarea"]/div/div[2]/div/div/div/div[2]/div/div[2]/div[1]/div/div[1]/div[1]/div/textarea') # driver.execute_script("arguments[0].scrollIntoView();", target) # 拖动到可见的元素去 # # # 输入评论 # driver.find_element_by_xpath( # '//*[@id="qitancommonarea"]/div/div[2]/div/div/div/div[2]/div/div[2]/div[1]/div/div[1]/div[1]/div/textarea').clear() # time.sleep(2) # driver.find_element_by_xpath('//*[@id="qitancommonarea"]/div[2]/div/div/div/div[' # '2]/div/div[1]/div/div/div[2]/div[1]/textarea').send_keys( # content) # # 打印评论内容 # # print(driver.find_element_by_xpath('//*[@id="qitancommonarea"]/div[2]/div/div/div/div[ # # 2]/div/div[1]/div/div/div[2]/div[1]/textarea').get_attribute('value')) # # 点击评论 # driver.find_element_by_xpath( # '//*[@id="qitancommonarea"]/div[2]/div/div/div/div[2]/div/div[1]/div/div/div[3]/div/div[3]/a[2]').click() # time.sleep(2) # # # 点击登录 # driver.find_element_by_xpath( # '//*[@id="widget-userregistlogin"]/span[2]/div[1]/a').click() # time.sleep(5) # # # 截图验证码并进行剪裁 # if isElementExist('/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div'): # driver.switch_to.frame('login_frame') # time.sleep(2) # # # 如果点击登陆之后要求扫码登录,选择点击账号密码登录 # if (driver.find_element_by_xpath( # '/html/body/div[2]/div[1]/div/div/div[6]').get_attribute( # 'class')) == 'login-frame'.decode("utf-8"): # driver.find_element_by_xpath( # '/html/body/div[2]/div[1]/div/div/div[6]/div[2]/p/span/a[1]').click() # # # 输入账号 # driver.find_element_by_xpath( # '/html/body/div[2]/div[1]/div/div/div[1]/div[1]/div/div[1]/div[' # '2]/input').clear() # time.sleep(2) # driver.find_element_by_xpath( # '/html/body/div[2]/div[1]/div/div/div[1]/div[1]/div/div[1]/div[' # '2]/input').send_keys(accountId) # # 输入密码 # # driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div/div[1]/div[1]/div/div[2]/div/p').clear() # time.sleep(2) # driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div/div[1]/div[' # '1]/div/div[2]/div/input[1]').send_keys(password) # # 点击登录 # time.sleep(2) # driver.find_element_by_xpath( # '/html/body/div[2]/div[1]/div/div/div[1]/div[1]/div/a[2]').click() # time.sleep(6) # # while isElementExist( # '/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/img'): # 如果存在验证码图片 # print('有验证码') # # picName = os.path.abspath('.') + '\\' + re.sub(r'[^0-9]', '', str( # datetime.datetime.now())) + '.png' # driver.save_screenshot(picName) # time.sleep(1) # # # 裁切图片 # img = Image.open(picName) # # 改版前 2017-8-10之前 # # region = (73, 110, 220, 160) # region = (124, 163, 284, 213) # cropImg = img.crop(region) # # # 保存裁切后的图片 # picNameCut = os.path.abspath('.') + '\\' + re.sub(r'[^0-9]', '', str( # datetime.datetime.now())) + '.png' # cropImg.save(picNameCut) # time.sleep(2) # # # 进行验证码验证 # f = open(picNameCut, 'rb') # b_64 = base64.b64encode(f.read()) # f.close() # req = showapi.ShowapiRequest("http://ali-checkcode.showapi.com/checkcode", # "4e5510e696c748ca8d5033dd595bfbbc") # json_res = req.addTextPara("typeId", "3040") \ # .addTextPara("img_base64", b_64) \ # .addTextPara("convert_to_jpg", "1") \ # .post() # # # print ('1') # # print ('json_res data is:', json_res) # print (json_res) # json_res # # str="{\"showapi_res_code\":0,\"showapi_res_error\":\"\",\"showapi_res_body\":{\"Result\":\"28ht\",\"ret_code\":0,\"Id\":\"adb1c363-d566-48a6-820e-55859428599d\"}}" # # int = json_res.find('Result') # yanzhengma = json_res[int + 11:int + 15] # print(yanzhengma) # # time.sleep(3) # driver.find_element_by_xpath( # '/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input').click() # time.sleep(3) # driver.find_element_by_xpath( # '/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input').clear() # time.sleep(3) # driver.find_element_by_xpath( # '/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input').send_keys( # yanzhengma) # time.sleep(3) # # # driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input[4]').clear() # # time.sleep(1) # # driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input[3]').clear() # # time.sleep(1) # # driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input[2]').clear() # # time.sleep(1) # # driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input[1]').clear() # # time.sleep(1) # # # # # # driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input[1]').send_keys(yanzhengma[0:1]) # # time.sleep(1) # # driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input[2]').send_keys(yanzhengma[1:2]) # # time.sleep(1) # # driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input[3]').send_keys(yanzhengma[2:3]) # # time.sleep(1) # # driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input[4]').send_keys(yanzhengma[3:4]) # # time.sleep(1) # # driver.find_element_by_xpath( # '/html/body/div[2]/div[1]/div/div/div[2]/div/div/a[2]').click() # time.sleep(8) # os.remove(picName) # time.sleep(2) # os.remove(picNameCut) # time.sleep(2) # # print('登录成功') # driver.switch_to_default_content() # # j = 0 # while j < 30: # # if isElementExist( # '//*[@id="qitancommonarea"]/div/div[2]/div/div/div/div[2]/div/div[2]/div[1]/div/div[1]/div[1]/div/textarea') \ # or isElementExist( # '//*[@id="arguments"]/div/div[1]/div/div/div/div[1]/div/textarea'): # # exist=False # print('找到评论框') # break # driver.execute_script(js) # j += 1 # time.sleep(2) # # print(j) # # target = driver.find_element_by_xpath( # '//*[@id="qitancommonarea"]/div[2]/div/div/div/div[' # '2]/div/div[1]/div/div/div[2]/div[1]/textarea') # driver.execute_script("arguments[0].scrollIntoView();", target) # 拖动到可见的元素去 # # time.sleep(5) # # 输入评论 # driver.find_element_by_xpath( # '//*[@id="qitancommonarea"]/div/div[2]/div/div/div/div[2]/div/div[2]/div[1]/div/div[1]/div[1]/div/textarea').clear() # time.sleep(2) # driver.find_element_by_xpath('//*[@id="qitancommonarea"]/div[2]/div/div/div/div[' # '2]/div/div[1]/div/div/div[2]/div[1]/textarea').send_keys( # content) # # 打印评论内容 # # print(driver.find_element_by_xpath('//*[@id="qitancommonarea"]/div[2]/div/div/div/div[ # # 2]/div/div[1]/div/div/div[2]/div[1]/textarea').get_attribute('value')) # # 点击评论 # driver.find_element_by_xpath( # '//*[@id="qitancommonarea"]/div[2]/div/div/div/div[2]/div/div[1]/div/div/div[3]/div/div[3]/a[2]').click() # time.sleep(2) # # print('本次评论任务成功') # # else: # # print('进入2') # target = driver.find_element_by_xpath( # '//*[@id="arguments"]/div/div[1]/div/div/div/div[1]/div/textarea') # driver.execute_script("arguments[0].scrollIntoView();", target) # 拖动到可见的元素去 # # # 输入评论 # driver.find_element_by_xpath( # '//*[@id="arguments"]/div/div[1]/div/div/div/div[1]/div/textarea').clear() # time.sleep(2) # driver.find_element_by_xpath( # '//*[@id="arguments"]/div/div[1]/div/div/div/div[1]/div/textarea').send_keys( # content) # # 打印评论内容 # # print(driver.find_element_by_xpath('//*[@id="arguments"]/div/div[1]/div/div/div/div[1]/div/textarea').get_attribute('value')) # # 点击评论 # driver.find_element_by_xpath( # '//*[@id="arguments"]/div/div[1]/div/div/div/div[2]/div/div[2]/a').click() # time.sleep(2) # # # 点击登录 # driver.find_element_by_xpath( # '//*[@id="widget-userregistlogin"]/span[2]/div[1]/a').click() # time.sleep(5) # # # 截图验证码并进行剪裁 # if isElementExist('/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div'): # driver.switch_to.frame('login_frame') # time.sleep(2) # # # 如果点击登陆之后要求扫码登录,选择点击账号密码登录 # if (driver.find_element_by_xpath( # '/html/body/div[2]/div[1]/div/div/div[6]').get_attribute( # 'class')) == 'login-frame'.decode("utf-8"): # driver.find_element_by_xpath( # '/html/body/div[2]/div[1]/div/div/div[6]/div[2]/p/span/a[1]').click() # # # 输入账号 # driver.find_element_by_xpath( # '/html/body/div[2]/div[1]/div/div/div[1]/div[1]/div/div[1]/div[' # '2]/input').clear() # time.sleep(2) # driver.find_element_by_xpath( # '/html/body/div[2]/div[1]/div/div/div[1]/div[1]/div/div[1]/div[' # '2]/input').send_keys(accountId) # # 输入密码 # # driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div/div[1]/div[1]/div/div[2]/div/p').clear() # time.sleep(2) # driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div/div[1]/div[' # '1]/div/div[2]/div/input[1]').send_keys(password) # # 点击登录 # time.sleep(2) # driver.find_element_by_xpath( # '/html/body/div[2]/div[1]/div/div/div[1]/div[1]/div/a[2]').click() # time.sleep(6) # # while isElementExist( # '/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/img'): # 如果存在验证码图片 # print('有验证码') # # picName = os.path.abspath('.') + '\\' + re.sub(r'[^0-9]', '', str( # datetime.datetime.now())) + '.png' # driver.save_screenshot(picName) # time.sleep(1) # # # 裁切图片 # img = Image.open(picName) # # 改版前 2017-8-10之前 # # region = (73, 110, 220, 160) # region = (124, 163, 284, 213) # cropImg = img.crop(region) # # # 保存裁切后的图片 # picNameCut = os.path.abspath('.') + '\\' + re.sub(r'[^0-9]', '', str( # datetime.datetime.now())) + '.png' # cropImg.save(picNameCut) # time.sleep(2) # # # 进行验证码验证 # f = open(picNameCut, 'rb') # b_64 = base64.b64encode(f.read()) # f.close() # req = showapi.ShowapiRequest("http://ali-checkcode.showapi.com/checkcode", # "4e5510e696c748ca8d5033dd595bfbbc") # json_res = req.addTextPara("typeId", "3040") \ # .addTextPara("img_base64", b_64) \ # .addTextPara("convert_to_jpg", "1") \ # .post() # # # print ('1') # # print ('json_res data is:', json_res) # print (json_res) # json_res # # str="{\"showapi_res_code\":0,\"showapi_res_error\":\"\",\"showapi_res_body\":{\"Result\":\"28ht\",\"ret_code\":0,\"Id\":\"adb1c363-d566-48a6-820e-55859428599d\"}}" # # int = json_res.find('Result') # yanzhengma = json_res[int + 11:int + 15] # print(yanzhengma) # # time.sleep(3) # driver.find_element_by_xpath( # '/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input').click() # time.sleep(3) # driver.find_element_by_xpath( # '/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input').clear() # time.sleep(3) # driver.find_element_by_xpath( # '/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input').send_keys( # yanzhengma) # time.sleep(3) # # # driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input[4]').clear() # # time.sleep(1) # # driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input[3]').clear() # # time.sleep(1) # # driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input[2]').clear() # # time.sleep(1) # # driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input[1]').clear() # # time.sleep(1) # # # # # # driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input[1]').send_keys(yanzhengma[0:1]) # # time.sleep(1) # # driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input[2]').send_keys(yanzhengma[1:2]) # # time.sleep(1) # # driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input[3]').send_keys(yanzhengma[2:3]) # # time.sleep(1) # # driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div/div[2]/div/div/div/div/input[4]').send_keys(yanzhengma[3:4]) # # time.sleep(1) # # driver.find_element_by_xpath( # '/html/body/div[2]/div[1]/div/div/div[2]/div/div/a[2]').click() # time.sleep(8) # os.remove(picName) # time.sleep(2) # os.remove(picNameCut) # time.sleep(2) # # print('登录成功') # driver.switch_to_default_content() # # j = 0 # while j < 30: # # if isElementExist( # '//*[@id="qitancommonarea"]/div/div[2]/div/div/div/div[2]/div/div[2]/div[1]/div/div[1]/div[1]/div/textarea') \ # or isElementExist( # '//*[@id="arguments"]/div/div[1]/div/div/div/div[1]/div/textarea'): # # exist=False # print('找到评论框') # break # driver.execute_script(js) # j += 1 # time.sleep(2) # # print(j) # # target = driver.find_element_by_xpath( # '//*[@id="arguments"]/div/div[1]/div/div/div/div[1]/div/textarea') # driver.execute_script("arguments[0].scrollIntoView();", target) # 拖动到可见的元素去 # # time.sleep(5) # # 输入评论 # driver.find_element_by_xpath( # '//*[@id="arguments"]/div/div[1]/div/div/div/div[1]/div/textarea').clear() # time.sleep(2) # driver.find_element_by_xpath( # '//*[@id="arguments"]/div/div[1]/div/div/div/div[1]/div/textarea').send_keys( # content) # # 打印评论内容 # # print(driver.find_element_by_xpath('//*[@id="arguments"]/div/div[1]/div/div/div/div[1]/div/textarea').get_attribute('value')) # # 点击评论 # driver.find_element_by_xpath( # '//*[@id="arguments"]/div/div[1]/div/div/div/div[2]/div/div[2]/a').click() # time.sleep(2) # # print('本次评论任务成功') except Exception as e: print('本次评论任务失败') print(e) finally: print('over') driver.quit()
def main(taskUrl, account, password, text): # try: #args = Param() print('开始本次评论任务') # 格式化成2016-03-20 11:45:39形式 print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) driver = webdriver.Firefox() driver.get(taskUrl) driver.maximize_window() time.sleep(2) # 点击登录 driver.find_element_by_xpath( '//*[@id="global_check_login"]/div/a[1]').click() time.sleep(5) #输入账号 driver.find_element_by_xpath('//*[@id="loginname"]').send_keys(account) time.sleep(2) #输入密码 driver.find_element_by_xpath('//*[@id="loginpassword"]').send_keys( password) time.sleep(2) driver.find_element_by_xpath('//*[@id="dialogLoginCaptchaInput"]').click() ################验证码 i = 0 while i < 5: driver.find_element_by_xpath( '//*[@id="dialogLoginCaptchaInput"]').clear() if i == 0: #故意输错误一次,使得验证码输入框和登录按钮索引+1 driver.find_element_by_xpath( '//*[@id="dialogLoginCaptchaInput"]').send_keys(u'验证码') time.sleep(1) driver.find_element_by_xpath( '//*[@id="eventAccount"]/form/div[5]/input').click() time.sleep(2) picName = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' driver.save_screenshot(picName) time.sleep(1) #裁切图片 img = Image.open(picName) region = (1070, 480, 1150, 515) cropImg = img.crop(region) #保存裁切后的图片 picNameCut = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' cropImg.save(picNameCut) time.sleep(2) #进行验证码验证 f = open(picNameCut, 'rb') b_64 = base64.b64encode(f.read()) f.close() req = showapi.ShowapiRequest( "http://ali-checkcode.showapi.com/checkcode", "4e5510e696c748ca8d5033dd595bfbbc") json_res=req.addTextPara("typeId","3040") \ .addTextPara("img_base64",b_64) \ .addTextPara("convert_to_jpg","1") \ .post() #print ('1') #print ('json_res data is:', json_res) print(json_res) json_res #str="{\"showapi_res_code\":0,\"showapi_res_error\":\"\",\"showapi_res_body\":{\"Result\":\"28ht\",\"ret_code\":0,\"Id\":\"adb1c363-d566-48a6-820e-55859428599d\"}}" int = json_res.find('Result') yanzhengma = json_res[int + 11:int + 15] print(yanzhengma) driver.find_element_by_xpath( '//*[@id="dialogLoginCaptchaInput"]').send_keys(yanzhengma) time.sleep(1) os.remove(picName) time.sleep(1) os.remove(picNameCut) time.sleep(1) i = i + 1 #点击登录 driver.find_element_by_xpath( '//*[@id="eventAccount"]/form/div[6]/input').click() time.sleep(5) #(driver.find_element_by_xpath('//*[@id="eventAccount"]/form/div[ # 4]').text).__contains__('验证码错误!'.decode("utf-8")) #因为这个节点在错误时才会出现,且会出现账户不存在和验证码错误等几种情况,不好判断,故根据登录按钮是否可点进行判断 if not isElementExist('//*[@id="eventAccount"]/form/div[6]/input', driver): break if i == 4: print('验证码判断超过5次,退出') driver.quit() ################验证码 target = driver.find_element_by_xpath( '//*[@id="s_ui_walaList"]/div/div[1]/img') driver.execute_script("arguments[0].scrollIntoView();", target) # 拖动到可见的元素去 time.sleep(3) #点击输入框 driver.find_element_by_xpath( '//*[@id="wala_off"]/div/div[2]/div/div[1]/p').click() time.sleep(2) #点击评分 有10个等级 driver.find_element_by_xpath('//*[@id="10"]').click() time.sleep(2) #输入标题 driver.find_element_by_xpath('//*[@id="walaTitle"]').send_keys(text) time.sleep(2) driver.switch_to.frame( driver.find_element_by_xpath( '//*[@id="publish"]/div[2]/div[1]/div[6]/div/div[2]/iframe')) driver.find_element_by_xpath('/html/body').click() time.sleep(2) #输入评论内容 driver.find_element_by_xpath('/html/body').send_keys(text) time.sleep(2) driver.find_element_by_xpath('/html/body').get_attribute('value') time.sleep(1) driver.switch_to_default_content() time.sleep(2) #点击发布 driver.find_element_by_xpath('//*[@id="publish"]/div[2]/div[2]/a').click() time.sleep(2) print('本次评论任务成功')
def main(taskUrl, account, password, text): try: #args = Param() print('开始本次评论任务') # 格式化成2016-03-20 11:45:39形式 print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) ''' iplist = ['123.56.154.24:5818', '59.110.159.237:5818', '47.93.113.175:5818', '123.56.44.11:5818', '101.200.76.126:5818', '123.56.228.93:5818', '123.57.48.138:5818', '123.56.72.115:5818', '123.56.77.123:5818', '123.56.76.207:5818', '47.93.85.217:5818', '59.110.23.162:5818', '47.92.32.50:5818', '47.91.241.124:5818'] #proxy_ip1 = iplist[random.randint(0, len(iplist)-1)] proxy_ip = random.choice(iplist) ip_ip = proxy_ip.split(":")[0] ip_port = int(proxy_ip.split(":")[1]) num = random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) if num < 15: isDaili = 1 # 1使用代理 print(requests.get('http://ip.chinaz.com/getip.aspx', proxies={"http": 'http://' + proxy_ip}).text) else: isDaili = 0 print(requests.get('http://ip.chinaz.com/getip.aspx').text) profile = webdriver.FirefoxProfile() profile.set_preference('network.proxy.type', isDaili) profile.set_preference('network.proxy.http', ip_ip) profile.set_preference('network.proxy.http_port', ip_port) # int profile.update_preferences() driver = webdriver.Firefox(firefox_profile=profile) ip_ip = httpIp.split(":")[0] ip_port = int(httpIp.split(":")[1]) profile = webdriver.FirefoxProfile() profile.set_preference('network.proxy.type', isDaili) profile.set_preference('network.proxy.http', ip_ip) profile.set_preference('network.proxy.http_port', ip_port) # int profile.update_preferences() driver = webdriver.Firefox(firefox_profile=profile) ''' driver = webdriver.Firefox() # driver = webdriver.Chrome() # driver.get("http://comment.news.163.com/news2_bbs/CPISM0FT000189FH.html") driver.get(taskUrl) driver.maximize_window() time.sleep(2) #快速登录 driver.find_element_by_xpath( '/html/body/div[8]/div[1]/div[1]/div[1]/div[1]/a').click() time.sleep(2) # 输入账号 # driver.find_element_by_xpath('//*[@id="YT-normalLogin"]/div[1]/label/span[1]').clear() driver.find_element_by_xpath( '//*[@id="quickPostLogin"]/label[1]/input').send_keys(account) # 输入密码 # driver.find_element_by_xpath('/html/body/div[2]/div[1]/div/div/div[1]/div[1]/div/div[2]/div/p').clear() time.sleep(2) driver.find_element_by_xpath( '//*[@id="quickPostLogin"]/label[2]/input').send_keys(password) time.sleep(2) driver.find_element_by_xpath('//*[@id="quickPostLogin"]/div/a').click() time.sleep(2) # 该方法用来确认元素是否存在,如果存在返回flag=true,否则返回false def isElementExist(element): flag = True try: driver.find_element_by_class_name(element) return flag except: flag = False # driver.execute_script(js) return flag while driver.find_element_by_xpath( "//*[contains(@id, 'x-URS-iframe')]").is_displayed(): print('有验证码') driver.switch_to.frame( driver.find_element_by_xpath( "//iframe[contains(@id,'x-URS-iframe')]")) picName = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' driver.save_screenshot(picName) time.sleep(1) # 裁切图片 img = Image.open(picName) #region = (985, 522, 1097, 563) region = (220, 245, 335, 287) cropImg = img.crop(region) # 保存裁切后的图片 picNameCut = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' cropImg.save(picNameCut) time.sleep(2) # 进行验证码验证 f = open(picNameCut, 'rb') b_64 = base64.b64encode(f.read()) f.close() req = showapi.ShowapiRequest( "http://ali-checkcode.showapi.com/checkcode", "4e5510e696c748ca8d5033dd595bfbbc") json_res = req.addTextPara("typeId", "3040") \ .addTextPara("img_base64", b_64) \ .addTextPara("convert_to_jpg", "1") \ .post() # print ('1') # print ('json_res data is:', json_res) print(json_res) json_res # str="{\"s大家都在发howapi_res_code\":0,\"showapi_res_error\":\"\",\"showapi_res_body\":{\"Result\":\"28ht\",\"ret_code\":0,\"Id\":\"adb1c363-d566-48a6-820e-55859428599d\"}}" int = json_res.find('Result') yanzhengma = json_res[int + 11:int + 15] print(yanzhengma) time.sleep(3) driver.find_element_by_xpath( "//input[contains(@class, 'j-inputtext cktext')]").click() time.sleep(3) driver.find_element_by_xpath( "//input[contains(@class, 'j-inputtext cktext')]").clear() time.sleep(3) driver.find_element_by_xpath( "//input[contains(@class, 'j-inputtext cktext')]").send_keys( yanzhengma) time.sleep(3) driver.find_element_by_xpath('//*[@id="dologin"]').click() time.sleep(8) os.remove(picName) time.sleep(2) os.remove(picNameCut) time.sleep(2) if not isElementExist('//*[@id="dologin"]'): print('验证码正确') break driver.switch_to.default_content() #快速登录 driver.find_element_by_xpath( '/html/body/div[4]/div[1]/div[1]/div[1]/div[1]/a').click() time.sleep(2) # 输入评论 driver.find_element_by_xpath('//*[@id="quickPostBody"]').clear() time.sleep(2) driver.find_element_by_xpath('//*[@id="quickPostBody"]').send_keys( text) time.sleep(2) # 打印评论内容 print( driver.find_element_by_xpath( '//*[@id="quickPostBody"]').get_attribute('value')) # 马上发表 driver.find_element_by_xpath('//*[@id="quickPostForm"]/div/a').click() time.sleep(2) print('本次评论任务成功') except Exception as e: print('本次评论任务失败') print(e) finally: print('over') driver.quit()
# -*- coding: utf-8 -*- from com.aliyun.api.gateway.sdk.util import showapi import json import base64 #get req = showapi.ShowapiRequest( "请求地址,比如http://ali-weather.showapi.com/area-to-weather", "appcode") json_res=req.addTextPara("para1_name","para1_value")\ .addTextPara("para2_name","para2_value")\ .addTextPara("para3_name","para3_value")\ .get() # #最后如果是post提交则换成.post() print('json_res data is:', json_res) #post form # f=open(r'c:\a.jpg','rb') # b_64=base64.b64encode(f.read()) # f.close() # req=showapi.ShowapiRequest( "请求地址,比如http://ali-checkcode.showapi.com/checkcode","appcode" ) # json_res= req.addTextPara("typeId","3040")\ # .addTextPara("img_base64",b_64)\ # .addTextPara("convert_to_jpg","1")\ # .post() # print ('json_res data is:', json_res)
def login(driver, username, password): #登录按钮 #WebDriverWait(driver, 20).until(lambda x: x.find_element_by_id('com.sina.weibo:id/titleSave')).click() WebDriverWait(driver, 60).until( lambda x: x.find_element_by_accessibility_id('我的资料')).click() time.sleep(2) if isElementExist('com.sina.weibo:id/btn_login', driver): print '未登录状态' driver.find_element_by_id('com.sina.weibo:id/btn_login').click() time.sleep(2) driver.find_element_by_id('com.sina.weibo:id/etLoginUsername').clear() time.sleep(2) driver.find_element_by_id( 'com.sina.weibo:id/etLoginUsername').send_keys(username) time.sleep(2) driver.find_element_by_id('com.sina.weibo:id/etPwd').clear() time.sleep(2) driver.find_element_by_id('com.sina.weibo:id/etPwd').send_keys( password) time.sleep(2) driver.find_element_by_id('com.sina.weibo:id/bnLogin').click() time.sleep(10) if True: if driver.find_element_by_id('com.sina.weibo:id/iv_access_image' ).is_displayed(): # 如果存在验证码图片 i = 0 while i < 6: print('有验证码') print(i) picName = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' driver.get_screenshot_as_file(picName) time.sleep(1) # 裁切图片 img = Image.open(picName) region = (120, 567, 420, 647) cropImg = img.crop(region) # 保存裁切后的图片 picNameCut = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' cropImg.save(picNameCut) time.sleep(2) # 进行验证码验证 f = open(picNameCut, 'rb') b_64 = base64.b64encode(f.read()) f.close() req = showapi.ShowapiRequest( "http://ali-checkcode.showapi.com/checkcode", "4e5510e696c748ca8d5033dd595bfbbc") json_res = req.addTextPara("typeId", "3040") \ .addTextPara("img_base64", b_64) \ .addTextPara("convert_to_jpg", "1") \ .post() # print ('1') # print ('json_res data is:', json_res) print(json_res) json_res # str="{\"showapi_res_code\":0,\"showapi_res_error\":\"\",\"showapi_res_body\":{\"Result\":\"28ht\",\"ret_code\":0,\"Id\":\"adb1c363-d566-48a6-820e-55859428599d\"}}" result = json.loads(str(json_res[1:-3]).replace('\\', '')) yanzhengma = result['showapi_res_body']['Result'] print(yanzhengma) time.sleep(1) os.remove(picName) time.sleep(1) os.remove(picNameCut) time.sleep(1) driver.find_element_by_id( 'com.sina.weibo:id/et_input').click() time.sleep(1) driver.find_element_by_id( 'com.sina.weibo:id/et_input').clear() time.sleep(1) driver.find_element_by_id( 'com.sina.weibo:id/et_input').send_keys(yanzhengma) time.sleep(3) #点击登录 driver.find_element_by_name('确定').click() time.sleep(5) if not isElementExist('com.sina.weibo:id/iv_access_image', driver): print '验证码输入正确' break else: print '已登录状态' driver.find_element_by_id('com.sina.weibo:id/titleSave').click() time.sleep(2) driver.find_element_by_id('com.sina.weibo:id/accountContent').click() time.sleep(2) driver.find_element_by_id( 'com.sina.weibo:id/exitAccountContent').click() time.sleep(2) driver.find_element_by_name('确定').click() time.sleep(2) driver.find_element_by_id('com.sina.weibo:id/etLoginUsername').clear() time.sleep(2) driver.find_element_by_id( 'com.sina.weibo:id/etLoginUsername').send_keys(username) time.sleep(2) driver.find_element_by_id('com.sina.weibo:id/etPwd').clear() time.sleep(2) driver.find_element_by_id('com.sina.weibo:id/etPwd').send_keys( password) time.sleep(2) driver.find_element_by_id('com.sina.weibo:id/bnLogin').click() time.sleep(10) if True: if driver.find_element_by_id('com.sina.weibo:id/iv_access_image' ).is_displayed(): # 如果存在验证码图片 i = 0 while i < 6: print('有验证码') print(i) picName = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' driver.get_screenshot_as_file(picName) time.sleep(1) # 裁切图片 img = Image.open(picName) region = (120, 567, 420, 647) cropImg = img.crop(region) # 保存裁切后的图片 picNameCut = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' cropImg.save(picNameCut) time.sleep(2) # 进行验证码验证 f = open(picNameCut, 'rb') b_64 = base64.b64encode(f.read()) f.close() req = showapi.ShowapiRequest( "http://ali-checkcode.showapi.com/checkcode", "4e5510e696c748ca8d5033dd595bfbbc") json_res = req.addTextPara("typeId", "3040") \ .addTextPara("img_base64", b_64) \ .addTextPara("convert_to_jpg", "1") \ .post() # print ('1') # print ('json_res data is:', json_res) print(json_res) json_res # str="{\"showapi_res_code\":0,\"showapi_res_error\":\"\",\"showapi_res_body\":{\"Result\":\"28ht\",\"ret_code\":0,\"Id\":\"adb1c363-d566-48a6-820e-55859428599d\"}}" result = json.loads(str(json_res[1:-3]).replace('\\', '')) yanzhengma = result['showapi_res_body']['Result'] print(yanzhengma) time.sleep(1) os.remove(picName) time.sleep(1) os.remove(picNameCut) time.sleep(1) driver.find_element_by_id( 'com.sina.weibo:id/et_input').click() time.sleep(1) driver.find_element_by_id( 'com.sina.weibo:id/et_input').clear() time.sleep(1) driver.find_element_by_id( 'com.sina.weibo:id/et_input').send_keys(yanzhengma) time.sleep(3) #点击登录 driver.find_element_by_name('确定').click() time.sleep(5) if not isElementExist('com.sina.weibo:id/iv_access_image', driver): print '验证码输入正确' break print '登录成功' time.sleep(15)
def main(taskUrl, account, password, text): try: #args = Param() # driver = webdriver.Chrome() print('开始本次评论任务') # 格式化成2016-03-20 11:45:39形式 print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) ''' iplist = ['47.93.113.175:5818', '59.110.159.237:5818', '123.56.77.123:5818', '123.56.76.207:5818', '123.56.72.115:5818', '123.56.154.24:5818', '123.56.44.11:5818', '123.56.228.93:5818', '123.57.48.138:5818', '101.200.76.126:5818'] # proxy_ip1 = iplist[random.randint(0, len(iplist)-1)] proxy_ip = random.choice(iplist) ip_ip = proxy_ip.split(":")[0] ip_port = proxy_ip.split(":")[1] num = random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) if num < 11: i = 1 # 1使用代理 print(requests.get('http://ip.chinaz.com/getip.aspx', proxies={"http": 'http://' + proxy_ip}).text) else: i = 0 print(requests.get('http://ip.chinaz.com/getip.aspx').text) ip_ip = httpIp.split(":")[0] ip_port = int(httpIp.split(":")[1]) profile = webdriver.FirefoxProfile() profile.set_preference('network.proxy.type', isDaili) profile.set_preference('network.proxy.http', ip_ip) profile.set_preference('network.proxy.http_port', ip_port) # int profile.update_preferences() driver = webdriver.Firefox(firefox_profile=profile) ''' driver = webdriver.Firefox() driver.get(taskUrl) driver.maximize_window() time.sleep(2) # driver.execute_script("window.scrollBy(0,200)","") #向下滚动200px # driver.execute_script("window.scrollBy(0,document.body.scrollHeight)","") #向下滚动到页面底部 # driver.switch_to.frame('iframe') # 将页面滚动条拖到底部 # js = "var q=document.body.scrollTop=100000" js = "var q=document.documentElement.scrollTop=10000" # 该方法用来确认元素是否存在,如果存在返回flag=true,否则返回false def isElementExist(element): flag = True try: driver.find_element_by_xpath(element) return flag except: flag = False # driver.execute_script(js) return flag #点击登录 driver.find_element_by_xpath('//*[@id="navLoginBt"]').click() time.sleep(2) driver.find_element_by_xpath( '//*[@id="loginform"]/div/div[1]/div[1]/p/input').clear() time.sleep(2) driver.find_element_by_xpath( '//*[@id="loginform"]/div/div[1]/div[1]/p/input').send_keys( account) time.sleep(2) time.sleep(2) driver.find_element_by_xpath( '//*[@id="loginform"]/div/div[1]/div[2]/p/input').clear() time.sleep(2) driver.find_element_by_xpath( '//*[@id="loginform"]/div/div[1]/div[2]/p/input').send_keys( password) time.sleep(2) #点击登录判断是否有验证码 driver.find_element_by_xpath( '//*[@id="loginform"]/div/div[3]/input').click() time.sleep(2) #验证码存在,判断是否可见 while driver.find_element_by_xpath( '//*[@id="verifyCode_img"]').is_displayed(): driver.find_element_by_xpath( '//*[@id="verifyCodeBox"]/div/input').clear() time.sleep(1) picName = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' driver.save_screenshot(picName) time.sleep(1) # 裁切图片 img = Image.open(picName) region = (757, 490, 857, 524) cropImg = img.crop(region) # 保存裁切后的图片 picNameCut = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' cropImg.save(picNameCut) time.sleep(2) # 进行验证码验证 f = open(picNameCut, 'rb') b_64 = base64.b64encode(f.read()) f.close() req = showapi.ShowapiRequest( "http://ali-checkcode.showapi.com/checkcode", "4e5510e696c748ca8d5033dd595bfbbc") json_res = req.addTextPara("typeId", "3040") \ .addTextPara("img_base64", b_64) \ .addTextPara("convert_to_jpg", "1") \ .post() # print ('1') # print ('json_res data is:', json_res) print(json_res) json_res # str="{\"showapi_res_code\":0,\"showapi_res_error\":\"\",\"showapi_res_body\":{\"Result\":\"28ht\",\"ret_code\":0,\"Id\":\"adb1c363-d566-48a6-820e-55859428599d\"}}" int = json_res.find('Result') yanzhengma = json_res[int + 11:int + 15] print(yanzhengma) driver.find_element_by_xpath( '//*[@id="verifyCodeBox"]/div/input').send_keys(yanzhengma) time.sleep(1) os.remove(picName) time.sleep(2) os.remove(picNameCut) time.sleep(2) #点击登录 driver.find_element_by_xpath( '//*[@id="loginform"]/div/div[3]/input').click() time.sleep(2) target = driver.find_element_by_xpath('//*[@id="commentTextarea"]') driver.execute_script("arguments[0].scrollIntoView();", target) # 拖动到可见的元素去 #输入评论内容 driver.find_element_by_xpath('//*[@id="commentTextarea"]').clear() time.sleep(2) driver.find_element_by_xpath('//*[@id="commentTextarea"]').send_keys( text) time.sleep(2) driver.find_element_by_xpath('//*[@id="commentSubmit"]').click() time.sleep(2) print('本次评论任务成功') time.sleep(2) except Exception as e: print('本次评论任务失败') print(e) finally: print('over') driver.quit()
def main(): # for num in range(1, 51): # try: #print(num) #print('开始注册,本次手机号为' + phone) driver = webdriver.Firefox() driver.get('https://passport.meituan.com/account/unitivesignup') driver.maximize_window() time.sleep(2) while True: token = getToken() print(token) phone = getPhone(token) #phone = 13420600715 print(phone) driver.find_element_by_xpath( '/html/body/div[1]/div[1]/div/form/div[1]/input').clear() time.sleep(2) #输入手机号 driver.find_element_by_xpath( '/html/body/div[1]/div[1]/div/form/div[1]/input').send_keys(phone) time.sleep(2) #点击其他输入框 判断手机号是否可用 driver.find_element_by_xpath( '/html/body/div[1]/div[1]/div/form/div[2]/div[2]/input').click() time.sleep(5) if not isElementExist( '/html/body/div[1]/div[1]/div/form/div[1]/span[2]/text()', driver): print('该账号可用') break #点击免费获取短信动态码,第一次判断手机号是否注册过,第二次弹出图片验证码 driver.find_element_by_xpath( '/html/body/div[1]/div[1]/div/form/div[2]/div[2]/input').click() time.sleep(2) #截图验证码 picName = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' driver.save_screenshot(picName) time.sleep(1) # 裁切图片 img = Image.open(picName) region = (720, 143, 780, 177) cropImg = img.crop(region) # 保存裁切后的图片 picNameCut = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' cropImg.save(picNameCut) time.sleep(2) # 进行验证码验证 f = open(picNameCut, 'rb') b_64 = base64.b64encode(f.read()) f.close() req = showapi.ShowapiRequest("http://ali-checkcode.showapi.com/checkcode", "4e5510e696c748ca8d5033dd595bfbbc") json_res = req.addTextPara("typeId", "3040") \ .addTextPara("img_base64", b_64) \ .addTextPara("convert_to_jpg", "1") \ .post() # print ('1') # print ('json_res data is:', json_res) print(json_res) # str="{\"showapi_res_code\":0,\"showapi_res_error\":\"\",\"showapi_res_body\":{\"Result\":\"28ht\",\"ret_code\":0,\"Id\":\"adb1c363-d566-48a6-820e-55859428599d\"}}" int = json_res.find('Result') yanzhengma = json_res[int + 11:int + 15] print(yanzhengma) time.sleep(5) print(1) driver.find_element_by_xpath('//*[@id="captcha-mobile"]').send_keys( yanzhengma) time.sleep(5) print(1) # 点击获取短信验证码 driver.find_element_by_xpath( '/html/body/div[1]/div[1]/div/form/div[2]/div[2]/input').click() time.sleep(30) getMessage(token, phone, driver)
def main(): for num in range(1, 2): try: print(num) # 格式化成2016-03-20 11:45:39形式 print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) driver = webdriver.Firefox() driver.get('http://www.gewara.com/register.xhtml') driver.maximize_window() time.sleep(2) #args = Param() #输入手机号 #driver.find_element_by_xpath('//*[@id="phoneLogin_content"]/div[1]/label/label').send_keys( # phone) ######################循环输入验证码 #exist = True #i = 1 #while exist: #print('进入手机号和图片验证码') time.sleep(2) token = getToken() #print(token) phone = getPhone(token) #phone = 17088927105 print(phone) #输入手机号 driver.find_element_by_xpath('//*[@id="mobile"]').send_keys(phone) time.sleep(2) i = 0 while i < 11: driver.find_element_by_xpath( '//*[@id="captchaMobileInput"]').clear() picName = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' driver.save_screenshot(picName) time.sleep(1) # 裁切图片 img = Image.open(picName) region = (920, 168, 1000, 208) cropImg = img.crop(region) # 保存裁切后的图片 picNameCut = os.path.abspath('.') + '\\' + re.sub( r'[^0-9]', '', str(datetime.datetime.now())) + '.png' cropImg.save(picNameCut) time.sleep(2) # 进行验证码验证 f = open(picNameCut, 'rb') b_64 = base64.b64encode(f.read()) f.close() req = showapi.ShowapiRequest( "http://ali-checkcode.showapi.com/checkcode", "4e5510e696c748ca8d5033dd595bfbbc") json_res = req.addTextPara("typeId", "3040") \ .addTextPara("img_base64", b_64) \ .addTextPara("convert_to_jpg", "1") \ .post() # print ('1') # print ('json_res data is:', json_res) print(json_res) # str="{\"showapi_res_code\":0,\"showapi_res_error\":\"\",\"showapi_res_body\":{\"Result\":\"28ht\",\"ret_code\":0,\"Id\":\"adb1c363-d566-48a6-820e-55859428599d\"}}" int = json_res.find('Result') yanzhengma = json_res[int + 11:int + 15] print(yanzhengma) driver.find_element_by_xpath( '//*[@id="captchaMobileInput"]').send_keys(yanzhengma) time.sleep(1) os.remove(picName) time.sleep(1) os.remove(picNameCut) time.sleep(1) i = i + 1 # 点击获取短信验证码 driver.find_element_by_xpath( '//*[@id="sendDTPassword"]').click() time.sleep(1) if not isElementExist( '//*[@id="phoneLogin_content"]/div[1]/div[2]', driver): print('验证码正确') break if i == 10: print('验证码判断超过十次,退出') driver.quit() #print('当前尝试次数:'+i) #首次打开网页时 元素不存在, 手机号或验证码输错一次就存在了,但是如果输入正确,变成存在不可现 if (driver.find_element_by_xpath('//*[@id="sendDTPassword"]').text ).__contains__('秒后重新获取'.decode("utf-8")): print('手机号和图片验证码输入正确,点击获取验证码') time.sleep(30) getMessage(token, phone, driver) time.sleep(3) driver.find_element_by_xpath('//*[@id="sbmit"]').click() time.sleep(3) if not isElementExist('//*[@id="sbmit"]', driver): addCount(phone) time.sleep(2) print('手机号' + phone + '注册成功') #black(token, phone) #print('已将该号码拉入黑名单') # f = open('gewala_account.txt', 'a') # # f.write('token:'+token) # # f.write('\n') # f.write(phone) # f.write('\n') # f.write('abc' + phone) # f.write('\n') # f.write('\n') # f.close() else: print('该手机号已注册过') except Exception as e: print(e) finally: print('本次注册结束') print('---------我是分割线------------') driver.quit()