main_single_server.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import os
  2. import json
  3. import asyncio
  4. import datetime
  5. import pandas as pd
  6. from playwright.async_api import async_playwright
  7. from applications import get_info, Mysql
  8. platform_name = None
  9. lock = asyncio.Lock()
  10. async def handle_download(download):
  11. global platform_name
  12. global data_frame
  13. async with lock:
  14. today = datetime.datetime.today().__str__().split(" ")[0].replace("-", "")
  15. temp = [platform_name]
  16. download_path = await download.path()
  17. # 指定新文件名
  18. new_file_path = r"temp_file\temp.xlsx"
  19. # 重命名文件
  20. if os.path.exists(new_file_path):
  21. os.remove(new_file_path)
  22. os.rename(download_path, new_file_path)
  23. if os.path.exists(download_path):
  24. os.remove(download_path)
  25. df = pd.read_excel(new_file_path)
  26. df = df.values.tolist()
  27. # 实验名称
  28. task_name = df[0][1]
  29. temp.append(task_name)
  30. # 数据指标
  31. task_index = df[5][1]
  32. temp.append(task_index)
  33. title_row = df[8]
  34. data_line = df[-1]
  35. temp += data_line
  36. w = {temp: data_line[index] for index, temp in enumerate(title_row)}
  37. print(platform_name, "\t", task_name, "\t", task_index)
  38. print(json.dumps(w, ensure_ascii=False, indent=4))
  39. while len(temp) < 29:
  40. temp.append("NULL")
  41. Mysql().insert_line(data=temp)
  42. data_frame.append(temp)
  43. os.remove(new_file_path)
  44. async def run(playwright):
  45. global platform_name
  46. global data_frame
  47. Id = input("请输入小程序id:\n" + json.dumps({
  48. 1: "票圈内容精选",
  49. 2: "票圈 l 3亿人喜欢的视频平台",
  50. 3: "票圈 l 视频精选",
  51. 4: "票圈 l 祝福",
  52. 5: "票圈 l 福年",
  53. 6: "票圈 l 信仰之路",
  54. 7: "票圈视频",
  55. 8: "票圈短视频",
  56. 9: "老好看视频",
  57. 10: "票圈最惊奇",
  58. 11: "票圈视频+"
  59. }, ensure_ascii=False, indent=4) + "\n:")
  60. Id = int(Id)
  61. # 初始化浏览器
  62. browser = await playwright.chromium.launch(
  63. headless=False,
  64. downloads_path="temp_file",
  65. )
  66. context = await browser.new_context(accept_downloads=True)
  67. page = await context.new_page()
  68. await page.set_viewport_size({"width": 1680, "height": 1080})
  69. page.on("download", handle_download)
  70. # 登陆,需要扫码
  71. await page.goto("https://wedata.weixin.qq.com/mp2/login")
  72. await page.goto("https://wedata.weixin.qq.com/mp2/?source=0")
  73. await page.goto("https://wedata.weixin.qq.com/mp2/basic-data/core-data?source=0")
  74. # # 默认先进入票圈内容精选
  75. platform = {
  76. 1: "票圈内容精选",
  77. 2: "票圈 l 3亿人喜欢的视频平台",
  78. 3: "票圈 l 视频精选",
  79. 4: "票圈 l 祝福",
  80. 5: "票圈 l 福年",
  81. 6: "票圈 l 信仰之路",
  82. 7: "票圈视频",
  83. 8: "票圈短视频",
  84. 9: "老好看视频",
  85. 10: "票圈最惊奇",
  86. 11: "票圈视频+"
  87. }
  88. platform_name = platform[Id]
  89. await get_info(page, platform_name)
  90. # 关闭浏览器上下文和浏览器
  91. await context.close()
  92. await browser.close()
  93. async def main():
  94. async with async_playwright() as playwright:
  95. await run(playwright)
  96. asyncio.run(main())