AirasiaExchangeRate.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # 亚航的汇率
  2. import json
  3. import urllib3
  4. import requests
  5. import re
  6. import time;
  7. from pyquery import PyQuery as pq
  8. from utils.HttpUtils import HttpUtils;
  9. from utils.ProxyUtils import ProxyUtils;
  10. urllib3.disable_warnings()
  11. headers = {
  12. 'Accept-Encoding': 'gzip',
  13. "Content-Encoding": "gzip, deflate, sdch",
  14. "Accept-Language": "zh-CN,zh;q=0.8,en;q=0.6",
  15. "Upgrade-Insecure-Requests": "1",
  16. "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
  17. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36",
  18. }
  19. class AirasiaExchangeRate():
  20. def __init__(self):
  21. print("请开始你的表演")
  22. def worker(self):
  23. print("开始汇率的查询");
  24. proxyUtils = ProxyUtils();
  25. proxy = proxyUtils.getIp();
  26. proxies = {
  27. "http": proxy,
  28. "https": proxy
  29. };
  30. print("使用的ip为---->"+proxy)
  31. session = requests.session();
  32. session.headers.update(headers);
  33. HttpUtils.get(session, "https://www.airasia.com/my/en/login/travel-agent.page", proxies);
  34. print(session.cookies)
  35. logindata = {
  36. "__EVENTARGUMENT": "",
  37. "__EVENTTARGET": "ControlGroupLoginAgentView$AgentLoginView$LinkButtonLogIn",
  38. "ControlGroupLoginAgentView$AgentLoginView$TextBoxUserID": "CNTRIPTOPK_ADMIN",
  39. "ControlGroupLoginAgentView$AgentLoginView$PasswordFieldPassword": "Tripto123",
  40. "pageToken": "",
  41. "TimeZoneDiff": "480"
  42. }
  43. res = HttpUtils.post(session, "https://booking2.airasia.com/LoginAgent.aspx", data=logindata);
  44. # print(res.content)
  45. AgentHome = res.url
  46. htym = res.content.decode()
  47. aacc = 'window.location.reload'
  48. print(AgentHome)
  49. if aacc in htym:
  50. print('哈哈 需要刷新一下')
  51. res = session.get(AgentHome)
  52. htym = res.content.decode()
  53. else:
  54. print(htym)
  55. currencycode = 'default'
  56. if 'ZENG' in htym:
  57. print("登录成功");
  58. html = HttpUtils.get(session, "https://booking2.airasia.com", proxies=proxies).content.decode();
  59. if html is None:
  60. return "获取失败";
  61. startIndex = html.find("externalRateInfo") + 18;
  62. endIndex = html.find("currencyInfo");
  63. rate = html[startIndex:endIndex].rstrip();
  64. rate = rate[0:len(rate) - 9];
  65. print(rate)
  66. # now 开始解析json
  67. # jsonRates = json.dumps(rate);
  68. # for jsonRates
  69. htmlJson = json.loads(rate);
  70. ExternalRateLists = htmlJson["ExternalRateList"];
  71. ExchangeRateEntitys = [];
  72. for ExternalRateList in ExternalRateLists:
  73. baseCurrency = ExternalRateList["quotedCurrency"];
  74. transactionCurrency = ExternalRateList["collectedCurrency"];
  75. curDate = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()));
  76. exchangeRate = ExternalRateList["exchangeRate"];
  77. updateDate = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()));
  78. source = 1;
  79. ExchangeRateEntity = {
  80. "baseCurrency": baseCurrency,
  81. "curDate": curDate,
  82. "transactionCurrency": transactionCurrency,
  83. "exchangeRate": exchangeRate,
  84. "updateDate": updateDate,
  85. "source": source
  86. }
  87. ExchangeRateEntitys.append(ExchangeRateEntity);
  88. print(ExchangeRateEntitys)
  89. return ExchangeRateEntitys;