# 亚航的汇率 import json import urllib3 import requests import re import time; from pyquery import PyQuery as pq from utils.HttpUtils import HttpUtils; from utils.ProxyUtils import ProxyUtils; urllib3.disable_warnings() headers = { 'Accept-Encoding': 'gzip', "Content-Encoding": "gzip, deflate, sdch", "Accept-Language": "zh-CN,zh;q=0.8,en;q=0.6", "Upgrade-Insecure-Requests": "1", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36", } class AirasiaExchangeRate(): def __init__(self): print("请开始你的表演") def worker(self): print("开始汇率的查询"); proxyUtils = ProxyUtils(); proxy = proxyUtils.getIp(); proxies = { "http": proxy, "https": proxy }; print("使用的ip为---->"+proxy) session = requests.session(); session.headers.update(headers); HttpUtils.get(session, "https://www.airasia.com/my/en/login/travel-agent.page", proxies); print(session.cookies) logindata = { "__EVENTARGUMENT": "", "__EVENTTARGET": "ControlGroupLoginAgentView$AgentLoginView$LinkButtonLogIn", "ControlGroupLoginAgentView$AgentLoginView$TextBoxUserID": "CNTRIPTOPK_ADMIN", "ControlGroupLoginAgentView$AgentLoginView$PasswordFieldPassword": "Tripto123", "pageToken": "", "TimeZoneDiff": "480" } res = HttpUtils.post(session, "https://booking2.airasia.com/LoginAgent.aspx", data=logindata); # print(res.content) AgentHome = res.url htym = res.content.decode() aacc = 'window.location.reload' print(AgentHome) if aacc in htym: print('哈哈 需要刷新一下') res = session.get(AgentHome) htym = res.content.decode() else: print(htym) currencycode = 'default' if 'ZENG' in htym: print("登录成功"); html = HttpUtils.get(session, "https://booking2.airasia.com", proxies=proxies).content.decode(); if html is None: return "获取失败"; startIndex = html.find("externalRateInfo") + 18; endIndex = html.find("currencyInfo"); rate = html[startIndex:endIndex].rstrip(); rate = rate[0:len(rate) - 9]; print(rate) # now 开始解析json # jsonRates = json.dumps(rate); # for jsonRates htmlJson = json.loads(rate); ExternalRateLists = htmlJson["ExternalRateList"]; ExchangeRateEntitys = []; for ExternalRateList in ExternalRateLists: baseCurrency = ExternalRateList["quotedCurrency"]; transactionCurrency = ExternalRateList["collectedCurrency"]; curDate = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())); exchangeRate = ExternalRateList["exchangeRate"]; updateDate = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())); source = 1; ExchangeRateEntity = { "baseCurrency": baseCurrency, "curDate": curDate, "transactionCurrency": transactionCurrency, "exchangeRate": exchangeRate, "updateDate": updateDate, "source": source } ExchangeRateEntitys.append(ExchangeRateEntity); print(ExchangeRateEntitys) return ExchangeRateEntitys;