def __register_one(self, Session, present_website: str): # user,pass, try: headers = {} headers['Connection'] = 'close' headers['user_agent'] = project_util.get_user_agent() res = Session.get("https://www.reddit.com/register/?actionSource=header_signup",proxies=Session.proxies, headers=headers, timeout=g_var.TIMEOUT) re_res = re.search('<input type="hidden" name="csrf_token" value="(.*?)">', res.text) if re_res.group(): csrf_token = re_res.group(1) else: g_var.ERR_CODE = "2001" g_var.ERR_MSG = g_var.ERR_MSG + "|_|没有获取到token" g_var.logger.error("没有获取到token") return -1 google_code = project_util.google_captcha(requests.session(), "6LeTnxkTAAAAAN9QEuDZRpn90WwKk_R1TRW_g-JC", "https://www.reddit.com") if len(google_code) < 5: g_var.ERR_CODE = "2010" g_var.ERR_MSG = g_var.ERR_MSG + "|_|" g_var.logger.error("没有获取到谷歌验证码") user = project_util.generate_random_string(6, 12) pwd = project_util.generate_random_string(10, 16) email = user + "@hotmail.com" data = { "csrf_token": csrf_token, "g-recaptcha-response": google_code, "dest": "https://www.reddit.com", "password": pwd, "username": user, "email": email, } # headers["content-type"]="application/x-www-form-urlencoded" res = Session.post("https://www.reddit.com/register", headers=headers,proxies=Session.proxies, data=data, timeout=g_var.TIMEOUT) # if res.json():#成功结果:{"dest": "https://www.reddit.com"} if self.__dictExistValue(res.json(), "dest"): self.captcha_err_count = 0 sql = "INSERT INTO reddit_com(username, password, mail, status) VALUES('" + user + \ "', '" + pwd + "', '" + email + "', '" + str(0) + "');" print("正在进入sql:", sql) last_row_id = MysqlHandler().insert(sql) if last_row_id != -1: userId = last_row_id return {'user_id': userId, 'name': user, 'password': pwd, 'mail': email} else: g_var.logger.error("数据库插入失败") return -1 else: g_var.logger.info("验证码错误或邮箱名重复!result:", res.text) self.captcha_err_count = self.captcha_err_count + 1 return -1 except Exception as e: g_var.ERR_CODE = "2100" g_var.ERR_MSG = g_var.ERR_MSG + "|_|"+"ip出现问题 请求失败" g_var.logger.info("未知错误:", e) return -1
def __register_one(self, present_website): """ 注册一个账户,需要实现注册、激活、并将注册数据存入数据库的功能 Args: Session:Session对象 present_website:当前网站名,用于数据库表名 email_and_passwd:邮箱账户和密码,email_and_passwd[0]是邮箱,[1]是密码 Returns: 注册成功返回注册数据字典对象registerData,需要包含id, username, password, email, cookie(在访问激活链接时能取到,\ 取不到返回空) user_id这样获取:(示例) # 将注册的账户写入数据库(sql自己写,这边只是个示例) sql = "INSERT INTO "+present_website+"(username, password, mail, status, cookie) VALUES('" + \ username + "', '" + password + "', '" + email + "', '" + str(0) + cookie + "');" last_row_id = MysqlHandler().insert(sql) if last_row_id != -1: registerData["user_id"] = last_row_id return registerData else: g_var.logger.error("数据库插入用户注册数据失败") return 0 注册失败返回状态码 0:数据库存储失败 -1:连续代理错误或页面发生改变等取不到关键数据等,需要停止程序 -2:注册失败,可能是打码出错等原因 """ g_var.logger.info('register......') headers = generate_headers(0) if headers == -1: return -1 g_var.logger.info('session_id......') session_id = get_session_id() if session_id == -1: return -1 elif session_id == -2: return -2 googlekey = '6LefyhkTAAAAANpeEKwwgimNneiKWXRQtEqFZbat' captcha_value = google_captcha( "", googlekey, 'https://auth.voxmedia.com/signup?return_to=https://www.sbnation.com/' ) if captcha_value == -1: return -2 headers['cookie'] = session_id username = generate_random_string(8, 12) password = generate_random_string(10, 14) community_id = random.randint(210, 299) g_var.logger.info('community_id.....') g_var.logger.info(community_id) email = username + '@hotmail.com' registerData = { 'g-recaptcha-response': captcha_value, 'user[username]': username, 'user[password]': password, 'user[email]': email, 'user[newsletter]': 'false', 'community_id': community_id, } g_var.logger.info('开始提交注册信息...') url_login = '******' html = requestsW.post(url_login, proxies=ip_proxy("en"), data=registerData, headers=headers, timeout=g_var.TIMEOUT) if html == -1: return html try: g_var.logger.info(html.text) res_data = json.loads(html.text) except Exception as e: g_var.logger.info(e) g_var.logger.info('注册失败,返回信息解析失败。。。') g_var.logger.info(html.text) return -2 if not res_data['success']: g_var.logger.info('注册失败。。。') g_var.logger.info(html.text) return -2 try: sql = "INSERT INTO " + present_website + "(username, password, mail) VALUES('" + \ username + "', '" + password + "', '" + email + "');" last_row_id = MysqlHandler().insert(sql) g_var.logger.info(last_row_id) if last_row_id != -1: g_var.logger.info('注册成功!' + username) userData = { 'id': last_row_id, 'username': username, 'password': password, } return userData else: g_var.ERR_CODE = 2004 g_var.ERR_MSG = "数据库插入用户注册数据失败..." g_var.logger.error("数据库插入用户注册数据失败...") return 0 except Exception as e: g_var.logger.info(e) g_var.ERR_CODE = 2004 g_var.ERR_MSG = "数据库插入用户注册数据异常..." g_var.logger.error("数据库插入用户注册数据异常...") return 0
def __register_one(self, Session, present_website: str, email_and_passwd): """ 注册一个账户 Args: Session:Session对象 present_website:当前网站名,用于数据库表名 email_and_passwd:邮箱账户和密码,email_and_passwd[0]是邮箱,[1]是密码 Returns: 注册成功返回注册数据字典对象registerData,需要包含user_id, username, password, email user_id这样获取:(示例) # 将注册的账户写入数据库(sql自己写,这边只是个示例) sql = "INSERT INTO "+present_website+"(username, password, mail, status) VALUES('" + name + \ "', '" + psd + "', '" + email_and_passwd[0] + "', '" + str(0) + "');" last_row_id = MysqlHandler().insert(sql) if last_row_id != -1: registerData["id"] = last_row_id return registerData else: g_var.logger.error("数据库插入用户注册数据失败") return 0 注册失败返回状态码 0:更换email 返回0 或其他错误,但是激活失败或插入数据库失败 -1:表示requests请求页面失败,需要更换代理 -2:注册失败,可能是邮箱密码不符合要求、或ip被封等原因,需要排查 """ user = project_util.generate_random_string(12, 16) pwd = project_util.generate_random_string(10, 12) email_list = email_and_passwd if email_list == -1: g_var.SPIDER_STATUS = 2 g_var.ERR_MSG = g_var.ERR_MSG + "|_|NO email" g_var.logger.info("NO email") return 0 verify_email = Session.get( "https://www.diigo.com/user_mana2/check_email?email=" + email_list[0], timeout=g_var.TIMEOUT, headers=self.headers, proxies=Session.proxies) # 验证邮箱是否可用 verify_user = Session.get( "https://www.diigo.com/user_mana2/check_name?username="******"1": g_var.SPIDER_STATUS = 2 g_var.ERR_MSG = g_var.ERR_MSG + "|_|账号密码或邮箱已经被注册" g_var.logger.info("账号密码或邮箱已经被注册") return 0 # time.sleep(3) google_captchas = google_captcha( "", "6Ld23sMSAAAAALfyXkI9d0nHmzOH9jZZNuh66nql", "https://www.diigo.com/sign-up?plan=free") if google_captchas == -1: g_var.SPIDER_STATUS = 2 g_var.ERR_MSG = g_var.ERR_MSG + "|_|谷歌打码失败" g_var.logger.info("谷歌打码失败") return -2 res = requestsW.get("https://www.diigo.com/", headers=self.headers, proxies=Session.proxies) # 打开首页 if res == -1: return res cookies = res.cookies.get_dict() i = 0 while i < g_var.ERR_COUNT: i += 1 try: Session.proxies = ip_proxy() res = requests.get("https://www.diigo.com/sign-up?plan=free", headers=self.headers, cookies=cookies, proxies=Session.proxies, verify=False) user_input = re.search('id="username" name="(\w{32})">', res.text) email_input = re.search('id=\'email\' name="(\w{32})">', res.text) pwd_input = re.search('id=\'password\' name="(\w{32})"', res.text) if not user_input and email_input and pwd_input: # TODO 获取不到参数 return "注册无法打开网页" else: user_input = user_input.group(1) email_input = email_input.group(1) pwd_input = pwd_input.group(1) data = { "plan": "free", "g-recaptcha-response": google_captchas, user_input: user, email_input: email_and_passwd[0], pwd_input: pwd, } self.headers["X-Requested-With"] = "XMLHttpRequest" self.headers[ "Referer"] = "https://www.diigo.com/sign-up?plan=free" self.headers[ "Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8" res = requests.post( "https://www.diigo.com/user_mana2/register_2", headers=self.headers, cookies=cookies, data=data, proxies=Session.proxies, verify=False) print(res.json()) if project_util.dictExistValue(res.json(), "status"): if res.json()["status"] == 1: cookies.update(res.cookies.get_dict()) savec = cookies res = requestsW.post( "https://www.diigo.com/user_mana2/resend_verify", cookies=cookies, headers=self.headers, data={"email": email_and_passwd[0]}, proxies=Session.proxies) print("重新发送邮箱:", res.text) emailinfo = EmailVerify( email_and_passwd[0], email_and_passwd[1], 'href="(https://www.diigo.com/user_mana2/register_verify/\w{32})"' ).execute_Start() print("这里是邮箱参数:", emailinfo) if emailinfo["data"] != -1: Session = requestsW.session() res = Session.get(emailinfo["data"], headers=self.headers, proxies=Session.proxies, cookies=cookies) sql = """INSERT INTO %s (username, password, mail, status, cookie) VALUES("%s", "%s", "%s", "%s", "%s");""" % ( "diigo_com", user, pwd, email_and_passwd[0], 0, savec) g_var.logger.info(sql) last_row_id = MysqlHandler().insert(sql) if last_row_id != -1: registerData = { "username": user, "password": pwd, "email": email_and_passwd[0], "cookie": savec, } registerData["id"] = int(last_row_id) return registerData return { "user": user, "pwd": pwd, "email": email_and_passwd[0], "cookies": Session.cookies.get_dict() } # if project_util.dictExistValue(res.cookies.get_dict(),"diigoandlogincookie"): # 注册成功并登陆cookie # saveCookie = str(Session.cookies.get_dict()) # # print({"user": user, "pwd": pwd, "email": email_and_passwd[0], "cookies": saveCookie}) # return {"user": user, "pwd": pwd, "email": email_and_passwd[0], "cookies": saveCookie} elif res.json()["status"] == -2: if "captcha error" in res.json()["status"]: g_var.SPIDER_STATUS = 2 g_var.ERR_MSG = g_var.ERR_MSG + "|_|谷歌打码失败" g_var.logger.info("谷歌打码失败") return -2 return -2 except Exception as e: res = requestsW.get("https://www.diigo.com/", headers=self.headers, proxies=Session.proxies, verify=False) # 打开首页 cookies = res.cookies.get_dict() g_var.logger.info(e) g_var.logger.info("正在换ip", e) return 0
def login(self, Session, present_website: str, VPN, userInfo): """ 登录 根据用户信息userInfo中是否包含cookie 1、有cookie直接构造loginData返回,跳过登录流程 2、没有cookie,需要post登录请求,获取到cookie,再构造loginData返回 Args: Session:Session对象 present_website:当前网站名,用于数据库表名 VPN:使用国内or国外代理 userInfo:用户信息 userInfo[0]:id [1]:username [2]passwod [3]:emial [4]:status [5]cookie Mysql Update: # 如果cookie失效,将该cookie从数据库中清除,并重新从数据库中获取登录账号密码 sql = "UPDATE 网站名 SET cookie='' WHERE id=" + str(loginData['id']) + ";" status = MysqlHandler().update(sql) if status == 0: g_var.logger.info("cookie失效,清除cookie update OK") return {"error": -2} else: g_var.logger.error("数据库清除cookie错误!") return {"error": 1} Returns: 成功返回loginData loginData = { 'id': user_id, 'username': username, 'password': password, 'cookie': cookie, } 失败返回状态值: 1:表示账号密码失效,密码被改或账号被网站删除 -1:表示requests请求页面失败,需要更换代理 -2:页面发生改变,获取不到页面上的一些token值 -3:数据库插入更新等错误 """ user_id = userInfo[0] username = userInfo[1] password = userInfo[2] if userInfo[5] != None and userInfo[5] != "": # userInfo[5]保存cookie值,如果cookie不为空,则使用cookie g_var.logger.info("返回cookie" + userInfo[5]) cookie = userInfo[5] loginData = { 'id': user_id, 'username': username, 'password': password, 'cookie': str(cookie), } return loginData else: google_captchas = google_captcha( "", "6Ld23sMSAAAAALfyXkI9d0nHmzOH9jZZNuh66nql", "https://www.diigo.com/sign-in?referInfo=https%3A%2F%2Fwww.diigo.com" ) if google_captchas == -1: return "谷歌打码失败" i = 0 while i < g_var.ERR_COUNT: i += 1 try: Session.proxies = ip_proxy() res = requests.get( "https://www.diigo.com/sign-in?referInfo=https%3A%2F%2Fwww.diigo.com", headers=self.headers, proxies=Session.proxies) login_token = re.search( 'name="loginToken" value="(\w{32})"', res.text) if login_token: login_token = login_token.group(1) print(login_token) else: return "为获取登陆cookie" cookies = res.cookies.get_dict() if res == -1: return res data = { "referInfo": "https://www.diigo.com", "loginToken": login_token, "username": username, "password": password, "g-recaptcha-response": google_captchas, "recaptcha": "v2", } self.headers["X-Requested-With"] = "XMLHttpRequest" self.headers[ "Referer"] = "https://www.diigo.com/sign-in?referInfo=https%3A%2F%2Fwww.diigo.com" self.headers[ "Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8" g_var.logger.info("正在登录中") res = requests.post("https://www.diigo.com/sign-in", headers=self.headers, data=data, cookies=cookies, proxies=Session.proxies) g_var.logger.info("登录结束") g_var.logger.info(res.text) if not '"status":1' in res.text: return -2 else: break except Exception as e: g_var.logger.info("正在换ip" + str(e)) save_cookies = str(res.cookies.get_dict()) if "diigoandlogincookie" in save_cookies: sql = "UPDATE %s SET cookie=\"%s\" WHERE id=%s ;" % ( present_website, save_cookies, user_id) g_var.logger.info(sql) status = MysqlHandler().update(sql) if status == 0: g_var.logger.info("cookie失效,清除cookie update OK") return { 'id': user_id, 'username': username, 'password': password, 'cookie': save_cookies, } else: g_var.logger.error("数据库清除cookie错误!") return {"error": 1} else: return -1 pass
def __postMessage(self, loginData, present_website, googlekey): """ 发文章 Args: Session:Session对象 loginData:用户信息,包括user_id,username,password,cookie present_website:当前网站名,用于数据库表名 Returns: 成功返回状态值:0 失败返回状态值: 1:表示账号密码失效,密码被改或账号被网站删除 -1:表示requests请求页面失败,需要更换代理 -2:页面发生改变,获取不到页面上的一些token值 -3:数据库插入更新等错误 -4:cookie失效 """ g_var.logger.info('post article......') item_id = str(uuid.uuid4()) sections_id = str(uuid.uuid4()) g_var.logger.info('正在获取headers。。。') headers = generate_headers(2, loginData['cookie'], item_id) if headers == -1 or loginData['cookie'] == "": g_var.logger.info("获取headers失败...") return -1 captcha_url = 'https://app.ex.co/create/new/preview' captcha_value = google_captcha('', googlekey, captcha_url) if captcha_value == -1: return -2 title, ops = get_article() if title == -1 or ops == -1: g_var.logger.info("未能获取对应文章格式内容...") return -1 article_data = generate_post_article_data(loginData, item_id, sections_id, captcha_value, title, ops) g_var.logger.info("文章发送中...") url = 'https://editor.ex.co/item/publish' res = requestsW.post(url, proxies=ip_proxy("en"), json=article_data, headers=headers, timeout=g_var.TIMEOUT) if res == -1: return res # cookie失效判断 cookie_prove = '401 - "Failed to authenticate token"' if cookie_prove == res.text: g_var.logger.info('cookie 失效 ......') # 如果cookie失效,将该cookie从数据库中清除,并重新从数据库中获取登录账号密码 sql = "UPDATE " + present_website + " SET cookie='' WHERE id=" + str( loginData['id']) + ";" status = MysqlHandler().update(sql) if status == 0: g_var.logger.info("cookie失效,清除cookie update OK") return -4 else: g_var.logger.error("数据库清除cookie错误!") return 1 res_article = re.findall('"item":\{"id":"(.*?)","tags"', res.text) if not res_article: g_var.ERR_CODE = 5000 g_var.ERR_MSG = "文章发送失败,IP异常等原因..." g_var.logger.info('文章发送失败,IP异常等原因...') return -1 try: url = 'https://app.ex.co/stories/item/' + item_id sql = "INSERT INTO playbuzz_com_article(url, keyword, user_id) VALUES('" + url + "', '" + title + "', '" + str( loginData["id"]) + "');" last_row_id = MysqlHandler().insert(sql) g_var.logger.info(last_row_id) if last_row_id != -1: g_var.logger.info('文章成功!' + loginData['firstName']) return 0 else: g_var.ERR_CODE = 2004 g_var.ERR_MSG = "数据库插入用户注册数据失败..." g_var.logger.error("数据库插入用户注册数据失败...") return -3 except Exception as e: g_var.logger.info(e) g_var.ERR_CODE = 2004 g_var.ERR_MSG = "数据库插入用户注册数据异常..." g_var.logger.error("数据库插入用户注册数据异常...") return -3
def __login(self, present_website, VPN, userInfo, googlekey): """ 登录 根据用户信息userInfo中是否包含cookie 1、有cookie直接构造loginData返回,跳过登录流程 2、没有cookie,需要post登录请求,获取到cookie,再构造loginData返回 Args: Session:Session对象 present_website:当前网站名,用于数据库表名 VPN:使用国内or国外代理 userInfo:用户信息 Returns: 成功返回loginData loginData = { 'id': user_id, 'username': username, 'password': password, 'cookie': cookie, } 失败返回状态值: 1:表示账号密码失效,密码被改或账号被网站删除 -1:表示requests请求页面失败,需要更换代理 -2:页面发生改变,获取不到页面上的一些token值 -3:数据库插入更新等错误 """ if userInfo[5] != None and userInfo[5] != "": g_var.logger.info('login, cookie....') # userInfo[5]保存cookie值,如果cookie不为空,则使用cookie loginData = { 'id': userInfo[0], 'firstName': userInfo[1], 'password': userInfo[2], 'cookie': userInfo[5], 'userId': userInfo[6], } return loginData g_var.logger.info('login, no cookie....') url_login = '******' # cookie为空,使用账号密码登录 headers = generate_headers(0) headers['Referer'] = 'https://login.ex.co/login' if headers == -1: g_var.logger.info("获取headers失败...") return -1 captcha_value = google_captcha('', googlekey, url_login) if captcha_value == -1: return -2 loginData = { "email": userInfo[3], "password": userInfo[2], "loginType": "Email", "captchaResponse": captcha_value, } g_var.logger.info("登录中...") html = requestsW.post(url_login, proxies=ip_proxy("en"), json=loginData, headers=headers, timeout=g_var.TIMEOUT) if html == -1: return html if '"response":"success"' not in html.text: g_var.logger.info('登录失败。。。') g_var.logger.info(html.text) return -2 accessToken = re.findall('"accessToken":"(.*?)"}', html.text)[0] try: # 获取cookie,保存到数据库。 sql = "UPDATE " + present_website + " SET cookie='" + accessToken + "' WHERE id=" + str( userInfo[0]) + ";" status = MysqlHandler().update(sql) if status == 0: g_var.logger.info("update cookie OK") else: g_var.ERR_CODE = 2004 g_var.ERR_MSG = "数据库更新cookie错误..." g_var.logger.error("数据库更新cookie错误...") return 0 except Exception as e: g_var.logger.info(e) g_var.ERR_CODE = 2004 g_var.ERR_MSG = "数据库更新cookie异常..." g_var.logger.error("数据库更新cookie异常...") return 0 loginData = { 'id': userInfo[0], 'firstName': userInfo[1], 'password': userInfo[2], 'cookie': accessToken, 'userId': userInfo[6], } return loginData
def __register_one(self, present_website, email_info, googlekey): """ 注册一个账户 Args: Session:Session对象 present_website:当前网站名,用于数据库表名 email_and_passwd:邮箱账户和密码,email_and_passwd[0]是邮箱,[1]是密码 Returns: 注册成功返回注册数据字典对象registerData,需要包含user_id, username, password, email user_id这样获取:(示例) # 将注册的账户写入数据库(sql自己写,这边只是个示例) sql = "INSERT INTO "+present_website+"(username, password, mail, status) VALUES('" + name + \ "', '" + psd + "', '" + email_and_passwd[0] + "', '" + str(0) + "');" last_row_id = MysqlHandler().insert(sql) if last_row_id != -1: registerData["user_id"] = last_row_id return registerData else: g_var.logger.error("数据库插入用户注册数据失败") return 0 注册失败返回状态码 0:注册成功,但是激活失败或插入数据库失败 -1:表示requests请求页面失败,需要更换代理 -2:注册失败,可能是邮箱密码不符合要求、或ip被封等原因,需要排查 """ url_register_one = 'https://login.ex.co/api/signup' headers = generate_headers(0) headers['Referer'] = 'https://login.ex.co/signup' if headers == -1: g_var.logger.info("获取headers失败...") return -1 captcha_value = google_captcha("", googlekey, url_register_one) if captcha_value == -1: return -2 registerData = generate_register_data(email_info, captcha_value) g_var.logger.info("提交注册中...") html = requestsW.post(url_register_one, proxies=ip_proxy("en"), json=registerData, headers=headers, timeout=g_var.TIMEOUT) if html == -1: return html if '"success":true' not in html.text: g_var.logger.info('注册失败。。。') g_var.logger.info(html.text) return -2 accessToken = re.findall('accessToken":"(.*?)"}}', html.text)[0] userId = re.findall('"UserId":"(.*?)",', html.text)[0] headers = generate_headers(1, accessToken) if headers == -1: g_var.logger.info("获取第二步注册验证的headers失败...") return -1 company = generate_random_string(10, 12) data = { "company": company, "industryType": 'Freelancer', "companySize": "", "userIntent": "" } url_register_two = 'https://login.ex.co/api/additional-data' g_var.logger.info("第二步提交注册中...") html = requestsW.post(url_register_two, proxies=ip_proxy("en"), json=data, headers=headers, timeout=g_var.TIMEOUT) if html == -1: return html if html.text != '{"success":true}': g_var.logger.info('第二步注册失败。。。') g_var.logger.info(html.text) return -2 # 将注册的账户写入数据库 try: sql = "INSERT INTO " + present_website + "(username, password, mail, cookie, userId) VALUES('" + \ registerData['firstName'] + "', '" + registerData['password'] + "', '" + email_info[0] + "', '" + accessToken + "', '" + str( userId) + "');" last_row_id = MysqlHandler().insert(sql) g_var.logger.info(last_row_id) if last_row_id != -1: g_var.logger.info('注册成功!' + registerData['firstName']) registerData["id"] = last_row_id registerData["cookie"] = accessToken registerData["userId"] = str(userId) return registerData else: g_var.ERR_CODE = 2004 g_var.ERR_MSG = "数据库插入用户注册数据失败..." g_var.logger.error("数据库插入用户注册数据失败...") return 0 except Exception as e: g_var.logger.info(e) g_var.ERR_CODE = 2004 g_var.ERR_MSG = "数据库插入用户注册数据异常..." g_var.logger.error("数据库插入用户注册数据异常...") return 0
def __register_one(self, present_website, email_and_passwd): """ 注册一个账户,需要实现注册、激活、并将注册数据存入数据库的功能 Args: Session:Session对象 present_website:当前网站名,用于数据库表名 email_and_passwd:邮箱账户和密码,email_and_passwd[0]是邮箱,[1]是密码 Returns: 注册成功返回注册数据字典对象registerData,需要包含id, username, password, email, cookie(在访问激活链接时能取到,\ 取不到返回空) user_id这样获取:(示例) # 将注册的账户写入数据库(sql自己写,这边只是个示例) sql = "INSERT INTO "+present_website+"(username, password, mail, status, cookie) VALUES('" + \ username + "', '" + password + "', '" + email + "', '" + str(0) + cookie + "');" last_row_id = MysqlHandler().insert(sql) if last_row_id != -1: registerData["user_id"] = last_row_id return registerData else: g_var.logger.error("数据库插入用户注册数据失败") return 0 注册失败返回状态码 0:某些报错需要跳出while循环,更换邮箱 -1:连续代理错误或页面发生改变等取不到关键数据等,需要停止程序 -2:注册失败,可能是打码出错等原因,邮箱可以继续使用(邮箱资源成本较高,因此要确保注册成功后再更换邮箱),不跳出循环 """ g_var.logger.info('register......') url = 'http://www.liveinternet.ru/journal_register.php' headers = generate_headers(0) if headers == -1: g_var.logger.info("获取headers失败...") return -1 tok = get_tok(url, headers) if tok == -1: return -1 elif tok == -2: return -2 googlekey = '6Lcl3BYUAAAAAG1gTAOhNtJIeTrPn68melrC1gbV' captcha_value = google_captcha("", googlekey, url) if captcha_value == -1: return -2 registerData = generate_register_data(email_and_passwd, captcha_value, tok) headers['Origin'] = 'http://www.liveinternet.ru' headers['Referer'] = 'http://www.liveinternet.ru/journal_register.php' headers['Content-Type'] = 'application/x-www-form-urlencoded' g_var.logger.info("提交注册中...") html = requestsW.post(url, proxies=ip_proxy("en"), data=registerData, headers=headers, timeout=g_var.TIMEOUT) if html == -1: return html # 第一步注册成功与否的验证 result = re.findall(email_and_passwd[0], html.text) if len(result) != 2: g_var.logger.info("第一步注册失败...") g_var.logger.info(html.status_code) return -2 # 邮箱验证 time.sleep(2) verify_url = get_verify_url(email_and_passwd) if verify_url == 0: g_var.logger.info("未读取到邮箱验证的url...") return 0 # 邮箱验证的tok获取 email_tok = get_tok_email(verify_url) if email_tok == -1: return 0 elif email_tok == -2: return 0 id = re.findall('id=(.*?)&', verify_url)[0] h = re.findall('h=(.*)', verify_url)[0] headers['Referer'] = verify_url captcha_value = google_captcha("", googlekey, verify_url) if captcha_value == -1: return 0 username = generate_random_string( 10, 12, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") day = str(random.randint(1, 28)) month = str(random.randint(1, 12)) year = str(random.randint(1980, 2010)) sex = ['M', 'W'] multipart_encoder = MultipartEncoder( fields={ 'username': username, 'comm': '0', 'sexchar': random.choice(sex), 'day': day, 'month': month, 'year': year, 'city': '1870', 'icq': '', 'emails': '', 'addinfo': username, 'avatarfile': ('', '', 'application/octet-stream'), 'g-recaptcha-response': captcha_value, 'dailynews': '1', 'Submit.x': '80', 'Submit.y': '20', 'familyname': '', 'firstname': '', 'password': registerData['password1'], 'email': email_and_passwd[0], 'passwordconfirm': registerData['password1'], 'imagehash': '', 'regkey': '', 'invite_id': '0', 'regkeynb': '', 'url_redirect': '', 'url2': '', 'action': 'add_step1', 'h': h, 'id': id, 'tok': email_tok, }, boundary='----WebKitFormBoundary' + generate_random_string( 16, 16, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), ) headers['Content-Type'] = multipart_encoder.content_type g_var.logger.info("注册第二步,邮箱验证提交信息中...") url_email_prove = 'http://www.liveinternet.ru/journal_register.php' html = requestsW.post(url_email_prove, proxies=ip_proxy("en"), data=multipart_encoder, headers=headers, allow_redirects=False, timeout=g_var.TIMEOUT) if html == -1: return html if not html.headers.get('Set-Cookie', None): g_var.logger.info('第二步邮箱验证信息提交失败...') return 0 # 将注册的账户写入数据库 try: set_cookie = html.headers['Set-Cookie'] user_Id = re.findall('bbuserid=(.*?);', set_cookie) user_password = re.findall('bbpassword=(.*?);', set_cookie) cookie = user_Id[0] + '|_|' + user_password[0] sql = "INSERT INTO " + present_website + "(username, password, mail, cookie) VALUES('" + \ username + "', '" + registerData['password1'] + "', '" + email_and_passwd[0] + "', '" + cookie + "');" last_row_id = MysqlHandler().insert(sql) g_var.logger.info(last_row_id) if last_row_id != -1: g_var.logger.info('注册成功!' + username) userData = { 'id': last_row_id, 'username': username, 'password': registerData['password1'], 'cookie': cookie, } return userData else: g_var.ERR_CODE = 2004 g_var.ERR_MSG = "数据库插入用户注册数据失败..." g_var.logger.error("数据库插入用户注册数据失败...") return 0 except Exception as e: g_var.logger.info(e) g_var.ERR_CODE = 2004 g_var.ERR_MSG = "数据库插入用户注册数据异常..." g_var.logger.error("数据库插入用户注册数据异常...") return 0