test4.py 756 B

123456789101112131415161718192021222324252627282930
  1. """
  2. @author: luojunhui
  3. """
  4. import json
  5. from spider.toutiao import parse_detail, search_article
  6. with open("test_return.json", encoding="utf-8") as f:
  7. video_data = json.loads(f.read())
  8. L = []
  9. for video_obj in video_data['data']:
  10. title = video_obj['title']
  11. print(title)
  12. urls = search_article(title)
  13. if urls:
  14. search_list = []
  15. for url in urls:
  16. try:
  17. res_o = parse_detail(url)
  18. search_list.append(res_o)
  19. except Exception as e:
  20. print(e)
  21. video_obj['search_list'] = search_list
  22. L.append(video_obj)
  23. else:
  24. continue
  25. with open("search_tt.json", "w", encoding="utf-8") as f:
  26. f.write(json.dumps(L, ensure_ascii=False, indent=4))