| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- import aiohttp
- import asyncio
- async def get_hot_point_content(page_index=1):
- """
- 获取今日热榜内容排名(异步版本)
- """
- url = "http://crawapi.piaoquantv.com/crawler/jin_ri_re_bang/content_rank"
- headers = {"Content-Type": "application/json"}
- data = {"sort_type": "最热", "category": "news", "cursor": page_index}
- try:
- async with aiohttp.ClientSession() as session:
- async with session.post(url, headers=headers, json=data) as response:
- response.raise_for_status() # 检查请求是否成功
- # 返回JSON响应
- return await response.json()
- except aiohttp.ClientError as e:
- print(f"请求失败: {e}")
- return None
- except Exception as e:
- print(f"其他错误: {e}")
- return None
- async def get_hot_point_content_batch(page_indices):
- """
- 批量获取热榜内容(异步并发)
- """
- tasks = [get_hot_point_content(page_index) for page_index in page_indices]
- results = await asyncio.gather(*tasks, return_exceptions=True)
- # 过滤掉异常结果
- valid_results = []
- for result in results:
- if not isinstance(result, Exception) and result is not None:
- valid_results.append(result)
- return valid_results
|