jsl_5s.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on 2023-12-25
  4. ---------
  5. @summary: jsl+创宇云盾 通用模板
  6. ---------
  7. @author: jsl、创宇5秒盾
  8. """
  9. import json
  10. import re
  11. import execjs
  12. import requests
  13. from feapder.network.cookie_pool import PageCookiePool
  14. class DTCookiePool(PageCookiePool):
  15. def __init__(self, redis_key, header, page_url=None, cwd=None, save_js=False, **kwargs):
  16. super(DTCookiePool, self).__init__(redis_key, page_url=None,
  17. min_cookies=10000,
  18. must_contained_keys=(),
  19. keep_alive=False, **kwargs)
  20. self.headers = header
  21. self.page_url = page_url
  22. self.proxies = kwargs.get('proxies') or False
  23. self.cwd = cwd
  24. self.is_save_js = save_js
  25. def create_cookie(self):
  26. proxies = self.proxies
  27. try:
  28. session = requests.Session()
  29. session.proxies = proxies
  30. start_url = self.page_url
  31. res = session.get(start_url, headers=self.headers,timeout=120, verify=False)
  32. js_func = "".join(re.findall("document.cookie=(.*?)location.href", res.text))
  33. js_func = 'function sd() { return ' + js_func + "}"
  34. ctx = execjs.compile(js_func)
  35. sss = ctx.call("sd")
  36. cookie = {}
  37. for temp, index in res.cookies.get_dict().items():
  38. cookie[temp] = index
  39. for item in sss.split(";"):
  40. if '=' in item:
  41. cookie[item.split("=")[0]] = item.split("=")[-1]
  42. res = session.get(start_url, cookies=cookie,headers=self.headers,timeout=120,verify=False)
  43. html_str = res.content.decode()
  44. if "<!DOCTYPE html>" in html_str:
  45. html_str = re.sub("<!DOCTYPE html>[\s\S]*?</html>", "", html_str.strip(),re.S)
  46. if self.is_save_js:
  47. with open('./source_code.js', 'w+', encoding='utf-8') as f:
  48. f.write(html_str)
  49. js_do_data = "".join(re.findall('};go\((.*?)\)', html_str))
  50. js_func = re.sub("<(/*?)script>", "", html_str)
  51. location = re.compile('location(.*?)}}else')
  52. location2 = re.compile('location(.*?)}else')
  53. setTimeout = re.compile('0x5dc;}}(.*?)setTimeout,function\(\)\{')
  54. setTimeout2 = re.compile('0x5dc;}(.*?)setTimeout\(function\(\)\{')
  55. gox = re.compile('};go(.*?)\)')
  56. js_func = re.sub(location, "}}else", js_func)
  57. js_func = re.sub(location2, "}else", js_func)
  58. js_func = re.sub(setTimeout, "0x5dc;}}", js_func)
  59. js_func = re.sub(setTimeout2, "0x5dc;}", js_func)
  60. js_func = re.sub(gox, "return document['cookie']\n};", js_func)
  61. js_func = '''const jsdom = require("jsdom");
  62. const {JSDOM} = jsdom;
  63. const dom = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`,
  64. {
  65. url: "https://example.org/",
  66. referrer: "https://example.com/",
  67. contentType: "text/html",
  68. });
  69. window = dom.window;
  70. document = window.document;
  71. location = window.location;
  72. ''' + js_func
  73. ctx = execjs.compile(js_func,cwd=self.cwd)
  74. if self.is_save_js:
  75. with open('./clean_code.js', 'w+', encoding='utf-8') as f:
  76. f.write(js_func)
  77. ss = ctx.call("go", json.loads(js_do_data))
  78. for item in ss.split(";"):
  79. if '=' in item:
  80. session.cookies.setdefault(item.split("=")[0], item.split("=")[-1])
  81. session.get(start_url,headers=self.headers,timeout=120,verify=False)
  82. cookies = requests.utils.dict_from_cookiejar(session.cookies)
  83. return cookies
  84. except Exception as e:
  85. print("cookie生产错误:",e)
  86. return {}