|
| 1 | +# @Time : 2020/3/15 18:57 |
| 2 | +# @Author : Libuda |
| 3 | +# @FileName: register.py |
| 4 | +# @Software: PyCharm |
| 5 | +import re |
| 6 | +import werobot |
| 7 | +import xlrd |
| 8 | +import smtplib # 发送邮件 连接邮件服务器 |
| 9 | +from email.mime.text import MIMEText # 构建邮件格式 |
| 10 | +from xlutils.copy import copy |
| 11 | +from selenium import webdriver |
| 12 | +import time |
| 13 | +import datetime |
| 14 | +from 远程文件传输.config import get_config |
| 15 | +from selenium.webdriver.chrome.options import Options |
| 16 | +import pandas as pd |
| 17 | +from pandas import DataFrame |
| 18 | +from copy import deepcopy |
| 19 | + |
| 20 | +robot = werobot.WeRoBot(token='fandengdushu') |
| 21 | +chrome_options = Options() |
| 22 | +chrome_options.add_argument('--headless') |
| 23 | +chrome_options.add_argument('--disable-gpu') |
| 24 | +chrome_options.add_argument( |
| 25 | + 'user-agent="Mozilla/5.0 (iPod; U; CPU iPhone OS 2_1 like Mac OS X; ja-jp) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5F137 Safari/525.20"') |
| 26 | +chrome_options.add_argument('--no-sandbox') # 这个配置很重要 |
| 27 | + |
| 28 | +config = get_config() |
| 29 | +driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=config['executable_path']) |
| 30 | +# driver = webdriver.Chrome(chrome_options=chrome_options, executable_path="/usr/bin/chromedriver") |
| 31 | +# |
| 32 | +user_list = ['1364826576@qq.com'] |
| 33 | + |
| 34 | +phone_num = 13281890000 |
| 35 | +wait_time = 2 # 各个阶段等待时间 |
| 36 | +time_jiange = 60 # 时间间隔 隔多长时间执行脚本一次 |
| 37 | +start_date = datetime.datetime.strptime("2019-12-1 00:00:00", "%Y-%m-%d %H:%M:%S") # 起始时间 |
| 38 | +end_date = datetime.datetime.strptime("2019-12-13 18:00:00", "%Y-%m-%d %H:%M:%S") # 结束时间 |
| 39 | +ding_num = 5 # 链接条数报警阈值 |
| 40 | +# 更换头部 |
| 41 | + |
| 42 | +# driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='/usr/bin/chromedriver') |
| 43 | +# driver = webdriver.Chrome(config['executable_path'],options=chrome_options) |
| 44 | + |
| 45 | +link_file_path = config['link_file_path'] |
| 46 | +# phone_file_path = config['phone_file_path'] |
| 47 | + |
| 48 | +link_ecel = xlrd.open_workbook(link_file_path) |
| 49 | +link_tables = link_ecel.sheet_by_index(0) |
| 50 | +link_get_col = 2 |
| 51 | +link_write_col = 3 |
| 52 | + |
| 53 | +# phone_excel = xlrd.open_workbook(phone_file_path) |
| 54 | +# phoe_tables = phone_excel.sheet_by_index(0) |
| 55 | +# phone_get_col = 1 |
| 56 | +# phone_write_col = 2 |
| 57 | + |
| 58 | +# phone_can_use_index = phoe_tables.get_rows() |
| 59 | +link_can_use_index = int(config['start_link_index']) |
| 60 | +totle_break_set = set() |
| 61 | + |
| 62 | + |
| 63 | +def logger(msg): |
| 64 | + """ |
| 65 | + 日志信息 |
| 66 | + """ |
| 67 | + now = time.ctime() |
| 68 | + print("[%s] %s" % (now, msg)) |
| 69 | + |
| 70 | + |
| 71 | +def get_keywords_data(tables, row, col): |
| 72 | + actual_data = tables.cell_value(row, col) |
| 73 | + return actual_data |
| 74 | + |
| 75 | + |
| 76 | +def write_to_excel(file_path, row, col, value): |
| 77 | + work_book = xlrd.open_workbook(file_path, formatting_info=False) |
| 78 | + write_to_work = copy(work_book) |
| 79 | + sheet_data = write_to_work.get_sheet(0) |
| 80 | + sheet_data.write(row, col, str(value)) |
| 81 | + write_to_work.save(file_path) |
| 82 | + |
| 83 | + |
| 84 | +def register(phone): |
| 85 | + """ |
| 86 | + 给手机号开卡 返回开卡结果及剩余链接数 |
| 87 | + :param phone: |
| 88 | + :return: |
| 89 | + """ |
| 90 | + print("开卡中") |
| 91 | + res = None |
| 92 | + df = pd.read_excel(link_file_path) |
| 93 | + link_data = [] |
| 94 | + for i in df.index.values: # 获取行号的索引,并对其进行遍历: |
| 95 | + # 根据i来获取每一行指定的数据 并利用to_dict转成字典 |
| 96 | + row_data = df.loc[i, ['id', 'link']].to_dict() |
| 97 | + link_data.append(row_data) |
| 98 | + |
| 99 | + link_data_tem = deepcopy(link_data) |
| 100 | + writer = pd.ExcelWriter(link_file_path, cell_overwrite_ok=True) |
| 101 | + dataframe = DataFrame() |
| 102 | + for index, data in enumerate(link_data): |
| 103 | + link = (data['link']) |
| 104 | + |
| 105 | + driver.get(link) |
| 106 | + time.sleep(wait_time) |
| 107 | + try: |
| 108 | + text = driver.find_element_by_xpath("/html/body/div[1]/div[1]/p[1]") |
| 109 | + if text.text == "开卡失败": |
| 110 | + write_to_excel(link_file_path, index + 1, link_write_col, "已使用") |
| 111 | + print("该卡已经被使用..{}".format(link)) |
| 112 | + link_data_tem.pop(0) |
| 113 | + continue |
| 114 | + except Exception as e: |
| 115 | + pass |
| 116 | + # time.sleep(wait_time) |
| 117 | + # # print(e) |
| 118 | + try: |
| 119 | + |
| 120 | + time.sleep(wait_time) |
| 121 | + text = "" |
| 122 | + try: |
| 123 | + text = driver.find_element_by_xpath('//*[@id="app"]/div[1]/div[1]/p') |
| 124 | + except Exception: |
| 125 | + pass |
| 126 | + |
| 127 | + print("该卡可以使用:{},正在查询可用手机号。。".format(link)) |
| 128 | + if text != "": |
| 129 | + driver.get(link) |
| 130 | + time.sleep(wait_time) |
| 131 | + driver.find_element_by_xpath('//*[@id="app"]/div[1]/div[2]/div[1]/input').send_keys(phone) |
| 132 | + driver.find_element_by_xpath('//*[@id="app"]/div[1]/div[2]/div[3]/input').send_keys(phone) |
| 133 | + driver.find_element_by_xpath('//*[@id="app"]/div[1]/div[3]').click() |
| 134 | + time.sleep(wait_time) |
| 135 | + # 点击开卡 |
| 136 | + driver.find_element_by_xpath('//*[@id="join-btn"]').click() |
| 137 | + # 点击开卡后页面延迟较为严重 |
| 138 | + time.sleep(wait_time) |
| 139 | + try: |
| 140 | + tem = driver.find_element_by_xpath('/html/body/div[1]/div[1]/p[1]') |
| 141 | + if tem.text == "开卡失败": |
| 142 | + res = "开卡失败" |
| 143 | + print("开卡失败,您已经是樊登读书好友") |
| 144 | + if len(link_data_tem) <= 0: |
| 145 | + link_data_tem = [{"id": "", "link": ""}] |
| 146 | + |
| 147 | + dataframe = dataframe.append(DataFrame(link_data_tem)) |
| 148 | + dataframe.to_excel(writer, index=0) |
| 149 | + writer.save() |
| 150 | + return res, len(link_data_tem) |
| 151 | + except Exception as e: |
| 152 | + time.sleep(wait_time) |
| 153 | + try: |
| 154 | + if driver.find_element_by_xpath('/html/body/div[1]/div/h1').text == "领取成功!": |
| 155 | + print("开卡成功") |
| 156 | + res = "开卡成功" |
| 157 | + link_data_tem.pop(0) |
| 158 | + if len(link_data_tem) <= 0: |
| 159 | + link_data_tem = [{"id": "", "link": ""}] |
| 160 | + |
| 161 | + dataframe = dataframe.append(DataFrame(link_data_tem)) |
| 162 | + dataframe.to_excel(writer, index=0) |
| 163 | + writer.save() |
| 164 | + return res, len(link_data_tem) |
| 165 | + except Exception as e: |
| 166 | + print("开卡成功") |
| 167 | + res = "开卡成功" |
| 168 | + if len(link_data_tem) <= 0: |
| 169 | + link_data_tem = [{"id": "", "link": ""}] |
| 170 | + |
| 171 | + dataframe = dataframe.append(DataFrame(link_data_tem)) |
| 172 | + dataframe.to_excel(writer, index=0) |
| 173 | + writer.save() |
| 174 | + return res, len(link_data_tem) |
| 175 | + |
| 176 | + |
| 177 | + except Exception as e: |
| 178 | + # print(e) |
| 179 | + pass |
| 180 | + # if len(link_data_tem) <= 0: |
| 181 | + # link_data_tem = [{"id": "", "link": ""}] |
| 182 | + # |
| 183 | + # dataframe = dataframe.append(DataFrame(link_data_tem)) |
| 184 | + # dataframe.to_excel(writer, index=0) |
| 185 | + # writer.save() |
| 186 | + return res, len(link_data_tem) |
| 187 | + |
| 188 | + |
| 189 | +if __name__ == '__main__': |
| 190 | + res, l = register("15735656005") |
| 191 | + print(res, l) |
0 commit comments