use_js.py 550 B

123456789101112131415161718192021222324
  1. """
  2. @author: luojunhui
  3. """
  4. import json
  5. import subprocess
  6. toutiao_js_path = "applications/crawler/toutiao/toutiao.js"
  7. def call_js_function(arguments_list):
  8. """
  9. call js function
  10. """
  11. # 将参数转换为JSON字符串
  12. args_json = json.dumps(arguments_list)
  13. # 调用Node.js执行脚本
  14. result = subprocess.run(
  15. ["node", toutiao_js_path, args_json], capture_output=True, text=True
  16. )
  17. if result.returncode == 0:
  18. return result.stdout.strip()
  19. else:
  20. raise Exception(f"Error: {result.stderr}")