wx_index.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import asyncio
  2. import random
  3. import time
  4. from readline import insert_text
  5. import requests
  6. import json
  7. import sys
  8. import os
  9. from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
  10. sys.path.insert(0, os.path.abspath("/root/AutoScraperX"))
  11. from core.utils.feishu_data_async import FeishuDataAsync
  12. class WechatDomainFetcher:
  13. """
  14. 微信小程序域名信息获取类
  15. 一次性任务
  16. """
  17. def __init__(self):
  18. pass
  19. @retry(
  20. stop=stop_after_attempt(3),
  21. wait=wait_exponential(multiplier=1, min=4, max=10),
  22. retry=retry_if_exception_type((requests.exceptions.RequestException, ConnectionError))
  23. )
  24. def get_domain_info(self, keyword, start_ymd):
  25. headers = {
  26. 'Content-Type': 'application/json',
  27. }
  28. json_data = {
  29. 'keyword': keyword,
  30. 'start_ymd': '20260423',
  31. 'end_ymd': '20260423',
  32. }
  33. try:
  34. response = requests.post('http://crawapi.piaoquantv.com/crawler/wei_xin/wxindex',
  35. headers=headers,
  36. json=json_data)
  37. response.raise_for_status() # 检查HTTP错误
  38. return response.json()
  39. except requests.exceptions.RequestException as e:
  40. print(f"获取域名信息失败: {str(e)}")
  41. raise # 重新抛出异常以触发重试
  42. except Exception as e:
  43. print(f"解析响应失败: {str(e)}")
  44. return None
  45. async def main():
  46. while True:
  47. try:
  48. async with FeishuDataAsync() as feishu_data:
  49. config = await feishu_data.get_values_v3("TWeZsuGW4hURHatWnaec12blnAe", "jvQdJL",range_str="A2:B")
  50. # 初始化微信域名信息获取器
  51. domain_fetcher = WechatDomainFetcher()
  52. for row in config[1:]:
  53. if len(row) < 2:
  54. continue
  55. keyword = row[1]
  56. start_ymd = row[0]
  57. if not keyword or not start_ymd:
  58. continue
  59. domain_info = domain_fetcher.get_domain_info(keyword, start_ymd)
  60. if not domain_info:
  61. continue
  62. wx_index_datas = domain_info.get('data', {}).get("data", [])
  63. if not wx_index_datas:
  64. continue
  65. for data in wx_index_datas:
  66. if data.get("ymd") == str(start_ymd):
  67. channel_score = data.get("channel_score", {})
  68. insert_row = [
  69. keyword,
  70. start_ymd,
  71. str(channel_score.get("total_score", 0)),
  72. str(channel_score.get("mpdoc_score", 0)),
  73. str(channel_score.get("finder_score", 0)),
  74. str(channel_score.get("query_score", 0)),
  75. str(channel_score.get("live_score", 0)),
  76. str(channel_score.get("miniapp_score", 0))
  77. ]
  78. print(f"准备追加数据: {insert_row}")
  79. async with FeishuDataAsync() as feishu_data:
  80. await feishu_data.append_values(
  81. "JvUOwyowjir2wSkueEBcIxaSn4f",
  82. "Nv3PJg",
  83. "A2:H",
  84. [insert_row]
  85. )
  86. print(f"成功追加: {keyword} - {start_ymd}")
  87. break
  88. time.sleep(random.randint(60, 120))
  89. except Exception as e:
  90. print(f"执行过程中出现错误: {str(e)}")
  91. import traceback
  92. traceback.print_exc()
  93. if __name__ == '__main__':
  94. asyncio.run(main())