hot_point.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import aiohttp
  2. import asyncio
  3. async def get_hot_point_content(page_index=1):
  4. """
  5. 获取今日热榜内容排名(异步版本)
  6. """
  7. url = "http://crawapi.piaoquantv.com/crawler/jin_ri_re_bang/content_rank"
  8. headers = {"Content-Type": "application/json"}
  9. data = {"sort_type": "最热", "category": "news", "cursor": page_index}
  10. try:
  11. async with aiohttp.ClientSession() as session:
  12. async with session.post(url, headers=headers, json=data) as response:
  13. response.raise_for_status() # 检查请求是否成功
  14. # 返回JSON响应
  15. return await response.json()
  16. except aiohttp.ClientError as e:
  17. print(f"请求失败: {e}")
  18. return None
  19. except Exception as e:
  20. print(f"其他错误: {e}")
  21. return None
  22. async def get_hot_point_content_batch(page_indices):
  23. """
  24. 批量获取热榜内容(异步并发)
  25. """
  26. tasks = [get_hot_point_content(page_index) for page_index in page_indices]
  27. results = await asyncio.gather(*tasks, return_exceptions=True)
  28. # 过滤掉异常结果
  29. valid_results = []
  30. for result in results:
  31. if not isinstance(result, Exception) and result is not None:
  32. valid_results.append(result)
  33. return valid_results