crawl.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import time
  2. import requests
  3. from bs4 import BeautifulSoup
  4. import re
  5. import json
  6. import datetime
  7. import socket
  8. import socks
  9. cookies = {}
  10. with open('data.json', 'r') as f:
  11. jsonData = json.load(f)
  12. cookies = jsonData['cookies']
  13. # 设置代理服务器的 IP 和端口号
  14. socks.set_default_proxy(socks.SOCKS5, "172.16.8.1", 1080)
  15. # 将所有的 TCP 连接都通过代理服务器进行处理
  16. socket.socket = socks.socksocket
  17. def flushCookie():
  18. reqData = {
  19. 'email': 'ftebox@qq.com',
  20. 'passwd': '369958Na',
  21. 'code': ''
  22. }
  23. headers = {
  24. 'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Mobile Safari/537.36',
  25. 'Referer': 'https://www.freewhale.co/auth/login',
  26. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  27. }
  28. try:
  29. # 发送登录请求
  30. response = requests.post('https://www.freewhale.co/auth/login', data=reqData, headers=headers, verify=False)
  31. # 判断是否登录成功
  32. if response.status_code == 200 and '"ret":1' in response.text:
  33. global cookies
  34. global jsonData
  35. # 读取cookie
  36. cookies = response.cookies.get_dict()
  37. # 将cookie写入jsonData
  38. jsonData['cookies'] = cookies;
  39. flushJson("cookie已刷新!");
  40. else:
  41. # 失败就重新登陆
  42. flushCookie();
  43. except requests.exceptions.RequestException as e:
  44. # 出现异常,打印异常 重新登陆
  45. print(e)
  46. flushCookie();
  47. def getData():
  48. url = 'https://www.freewhale.co/user' # 将此链接替换为您要抓取的实际链接
  49. global cookies
  50. try:
  51. response = requests.get(url, cookies=cookies, verify=False)
  52. # 如果发生了重定向 就刷新cookie
  53. if response.history:
  54. flushCookie();
  55. getData();
  56. # 如果发生了异常 就递归
  57. except requests.exceptions.RequestException as e:
  58. print(e)
  59. getData();
  60. else:
  61. html = response.content
  62. soup = BeautifulSoup(html, 'html.parser')
  63. item = soup.findAll(class_='nodemain')
  64. item1 = soup.findAll('code')
  65. item2 = soup.findAll('input')
  66. # 在线设备数
  67. zxsb = item[2].find('dd').text.strip()
  68. # 上次使用时间
  69. sysj = item[2].findAll('div')[4].text[6:].strip()
  70. # 剩余流量
  71. syll = item1[2].text.strip()
  72. # 到期时间
  73. dqsj = item[0].findAll('div')[4].text[7:].strip()
  74. # ssr订阅链接
  75. ssr = item2[0]['value'].strip()
  76. ssr_bak = item2[1]['value'].strip()
  77. # clash订阅链接
  78. clash = item2[2]['value'].strip()
  79. # v2ray订阅链接
  80. v2ray = item2[4]['value'].strip()
  81. v2ray_bak = item2[5]['value'].strip()
  82. global jsonData
  83. jsonData['data'] = {
  84. 'code': 1,
  85. 'message': 'success!',
  86. 'data': {
  87. 'zxsb': zxsb,
  88. 'sysj': sysj,
  89. 'syll': syll,
  90. 'dqsj': dqsj,
  91. 'v2rayurl': v2ray,
  92. 'v2rayBakurl': v2ray_bak,
  93. 'SSRurl': ssr,
  94. 'SSRBakurl': ssr_bak,
  95. 'Clash': clash
  96. }
  97. }
  98. jsonData['saveTime'] = time.time();
  99. flushJson("数据已刷新");
  100. def flushJson(msg):
  101. global jsonData;
  102. with open('data.json', 'w') as f:
  103. json.dump(jsonData, f)
  104. # 获取当前的日期和时间
  105. now = datetime.datetime.now()
  106. # 格式化输出日期和时间
  107. formatted_time = now.strftime('%Y-%m-%d %H:%M:%S')
  108. print('{} '.format(formatted_time) + msg)