def getAllProxy(pool_size=10, thread_or_process=True, is_refash=True): Log.v('正在更新ip池,请稍后...') # address = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + '/' if is_refash: proxys = GetFreeProxy.get_list_proxys() proxys = list(set(proxys)) sqlite = Sqlite(address + 'ip.db') sqlite.update_data('DELETE FROM ip_house') sqlite = Sqlite(address + 'ip.db') for i in range(len(proxys)): if proxys[i] and GetFreeProxy.verifyProxyFormat(proxys[i]): sqlite.cursor.execute( "INSERT INTO ip_house VALUES (?,?,?,?);", [i + 1, proxys[i], 'HTTP', 'true']) sqlite.conn.commit() sqlite.close_conn() GetFreeProxy.get_proxys_by_website() else: sqlite = Sqlite(address + 'ip.db') results = sqlite.query_data( 'select count(proxy_adress) from ip_house') if int(results[0][0]) == 0: proxys = GetFreeProxy.get_list_proxys() proxys = list(set(proxys)) sqlite = Sqlite(address + 'ip.db') for i in range(len(proxys)): if proxys[i] and GetFreeProxy.verifyProxyFormat(proxys[i]): sqlite.cursor.execute( "INSERT INTO ip_house VALUES (?,?,?,?);", [i + 1, proxys[i], 'HTTP', 'true']) sqlite.conn.commit() sqlite.close_conn() GetFreeProxy.get_proxys_by_website() sqlite = Sqlite(address + 'ip.db') results = sqlite.query_data( 'select id,proxy_adress,type from ip_house') params = [] for result in results: param = str(result[0]) + '&&' + result[1] + '&&' + result[2] params.append(param) Log.v("发现ip代理数量:" + str(len(params))) Log.v('正在检查ip可用性...') if thread_or_process: GetFreeProxy.exec_multi_threading(pool_size, params) else: GetFreeProxy.exec_multi_process(pool_size, params) Log.v('更新完成')
def main(): GetFreeProxy.getAllProxy(THREAD_POOL_SIZE,THREAD_OR_PROCESS,IS_REFASH_IP_POOL) sqlite = Sqlite(address + 'ip.db') ips_results = sqlite.query_data('select proxy_adress from ip_house') login = Login() Log.v('正在登录...') result, msg = login.login(USER_NAME, USER_PWD, IS_AUTO_CHECK_CAPTHCA) if not Utils.check(result, msg): Log.e(msg) return Log.v('%s,登录成功' % msg) seatTypesCode = SEAT_TYPE_CODE if SEAT_TYPE_CODE else [SEAT_TYPE[key] for key in SEAT_TYPE.keys()] passengerTypeCode = PASSENGER_TYPE_CODE if PASSENGER_TYPE_CODE else '1' count = 0 while True: # 死循环一直查票,直到下单成功 try: nowTime, status = deadline.do_fix_time() if status: Log.v('当前时间:%s 处于23点到6点之间,12306处于维护状态,暂不处理下单业务' % nowTime) continue count += 1 Log.v('第%d次访问12306网站' % count) print('-' * 40) ticketDetails = Query.loopQuery(TRAIN_DATE, FROM_STATION, TO_STATION, TrainUtils.passengerType2Desc(passengerTypeCode), TRAINS_NO, seatTypesCode, PASSENGERS_ID, POLICY_BILL, QUERY_TICKET_REFERSH_INTERVAL) Log.v('已为您查询到可用余票:%s' % ticketDetails) ticketDetails.passengersId = PASSENGERS_ID ticketDetails.ticketTypeCodes = passengerTypeCode ticketDetails.tourFlag = TOUR_FLAG if TOUR_FLAG else 'dc' submit = Submit(ticketDetails) if submit.submit(CHOOSE_SEATS): status, contents = submit.showSubmitInfoPretty() if status: flag = send_mail(mail_user, mailto_list, '12306订票结果通知', mail_host, mail_user, mail_pass, contents) if flag: Log.v("邮件发送成功!") else: Log.v("邮件发送失败!") sms_id = send_sms(ACCOUNT_SID,AUTO_TOKEN,FROM_NUM,TO_NUM,'小机机已经成功拿到小票票,请主人记得在30分钟内完成支付!!!') if sms_id: Log.v("短信提醒发送成功!") else: Log.v("短信提醒发送成功!") break time.sleep(1) except Exception as e: Log.w(e) print(login.loginOut())
def init_ip_pool(): import os from utils.sqllite_handle import Sqlite address = os.path.dirname(os.path.dirname( os.path.realpath(__file__))) + '/' sqlite = Sqlite(address + 'ip.db') ips_results = sqlite.query_data('select proxy_adress from ip_house') for ip in ips_results: ips.append(ip[0])
def init_ip_pool(): import os from utils.sqllite_handle import Sqlite address = os.path.dirname(os.path.dirname( os.path.realpath(__file__))) + '/' sqlite = Sqlite(address + 'ip.db') ips_results = sqlite.query_data('select proxy_adress,type from ip_house') Log.v('有效ip代理数量:' + str(len(ips_results))) for ip in ips_results: ips.append([ip[0], ip[1]]) # init_ip_pool()
def get_proxys_by_website(): import time headers = { 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate, br', 'Upgrade-Insecure-Requests': '1', 'Host': 'www.xicidaili.com', 'Pragma': 'no-cache', 'Connection': 'keep-alive', 'Cache-Control': 'no-cache', 'Accept-Language': 'zh-CN,zh;q=0.9' } response = requests.get('https://www.xicidaili.com/nn/1', headers=headers, timeout=20) html = BeautifulSoup(response.text, 'html.parser') table = html.find('table', attrs={'id': 'ip_list'}) trs = table.find_all('tr') trs_list = [] trs_list.extend(trs[1:]) time.sleep(1) response = requests.get('https://www.xicidaili.com/nt/', headers=headers, timeout=20) html = BeautifulSoup(response.text, 'html.parser') table = html.find('table', attrs={'id': 'ip_list'}) trs = table.find_all('tr') trs_list.extend(trs[1:]) time.sleep(1) response = requests.get('https://www.xicidaili.com/wn/', headers=headers, timeout=20) html = BeautifulSoup(response.text, 'html.parser') table = html.find('table', attrs={'id': 'ip_list'}) trs = table.find_all('tr') trs_list.extend(trs[1:]) time.sleep(1) response = requests.get('https://www.xicidaili.com/wt/', headers=headers, timeout=20) html = BeautifulSoup(response.text, 'html.parser') table = html.find('table', attrs={'id': 'ip_list'}) trs = table.find_all('tr') trs_list.extend(trs[1:]) proxys = [] for i in range(0, len(trs_list)): tds = trs_list[i].find_all('td') ip = tds[1].get_text() port = tds[2].get_text() type = str(tds[5].get_text()).strip(' ') date_time = str(tds[9].get_text()).split(' ')[0] now = time.strftime("%y-%m-%d", time.localtime()) # if date_time == now: proxys.append(ip + ":" + port + '&' + type) proxys = list(set(proxys)) sqlite = Sqlite(address + 'ip.db') results = sqlite.query_data('select count(proxy_adress) from ip_house') sqlite = Sqlite(address + 'ip.db') num = int(results[0][0]) for i in range(len(proxys)): paras = proxys[i].split('&') if paras[0] and GetFreeProxy.verifyProxyFormat(paras[0]): sqlite.cursor.execute("INSERT INTO ip_house VALUES (?,?,?,?);", [num + i, paras[0], paras[1], 'true']) sqlite.conn.commit() sqlite.close_conn()