MediaCrawler/media_platform/tieba/client.py

361 lines
13 KiB
Python
Raw Normal View History

2024-08-06 11:21:34 +00:00
import asyncio
2024-08-05 10:51:51 +00:00
import json
from typing import Any, Callable, Dict, List, Optional, Union
from urllib.parse import urlencode
import httpx
from playwright.async_api import BrowserContext
2024-08-08 06:19:32 +00:00
from tenacity import RetryError, retry, stop_after_attempt, wait_fixed
2024-08-05 10:51:51 +00:00
2024-08-06 18:34:56 +00:00
import config
2024-08-05 10:51:51 +00:00
from base.base_crawler import AbstractApiClient
from model.m_baidu_tieba import TiebaComment, TiebaCreator, TiebaNote
from proxy.proxy_ip_pool import ProxyIpPool
2024-08-05 10:51:51 +00:00
from tools import utils
from .field import SearchNoteType, SearchSortType
from .help import TieBaExtractor
2024-08-05 10:51:51 +00:00
class BaiduTieBaClient(AbstractApiClient):
def __init__(
self,
timeout=10,
ip_pool=None,
default_ip_proxy=None,
2024-08-05 10:51:51 +00:00
):
self.ip_pool: Optional[ProxyIpPool] = ip_pool
2024-08-05 10:51:51 +00:00
self.timeout = timeout
2024-08-06 20:13:15 +00:00
self.headers = {
"User-Agent": utils.get_user_agent(),
"Cookies": "",
}
2024-08-05 10:51:51 +00:00
self._host = "https://tieba.baidu.com"
self._page_extractor = TieBaExtractor()
self.default_ip_proxy = default_ip_proxy
2024-08-05 10:51:51 +00:00
@retry(stop=stop_after_attempt(3), wait=wait_fixed(1))
async def request(self, method, url, return_ori_content=False, proxies=None, **kwargs) -> Union[str, Any]:
2024-08-05 10:51:51 +00:00
"""
封装httpx的公共请求方法对请求响应做一些处理
Args:
method: 请求方法
url: 请求的URL
return_ori_content: 是否返回原始内容
proxies: 代理IP
2024-08-05 10:51:51 +00:00
**kwargs: 其他请求参数例如请求头请求体等
Returns:
"""
actual_proxies = proxies if proxies else self.default_ip_proxy
async with httpx.AsyncClient(proxies=actual_proxies) as client:
2024-08-05 10:51:51 +00:00
response = await client.request(
method, url, timeout=self.timeout,
2024-08-06 20:13:15 +00:00
headers=self.headers, **kwargs
2024-08-05 10:51:51 +00:00
)
if response.status_code != 200:
utils.logger.error(f"Request failed, method: {method}, url: {url}, status code: {response.status_code}")
utils.logger.error(f"Request failed, response: {response.text}")
raise Exception(f"Request failed, method: {method}, url: {url}, status code: {response.status_code}")
if response.text == "" or response.text == "blocked":
utils.logger.error(f"request params incrr, response.text: {response.text}")
raise Exception("account blocked")
if return_ori_content:
2024-08-05 10:51:51 +00:00
return response.text
return response.json()
async def get(self, uri: str, params=None, return_ori_content=False, **kwargs) -> Any:
2024-08-05 10:51:51 +00:00
"""
GET请求对请求头签名
Args:
uri: 请求路由
params: 请求参数
return_ori_content: 是否返回原始内容
2024-08-05 10:51:51 +00:00
Returns:
"""
final_uri = uri
if isinstance(params, dict):
final_uri = (f"{uri}?"
f"{urlencode(params)}")
try:
res = await self.request(method="GET", url=f"{self._host}{final_uri}",
return_ori_content=return_ori_content,
**kwargs)
return res
except RetryError as e:
if self.ip_pool:
proxie_model = await self.ip_pool.get_proxy()
_, proxies = utils.format_proxy_info(proxie_model)
res = await self.request(method="GET", url=f"{self._host}{final_uri}",
return_ori_content=return_ori_content,
proxies=proxies,
**kwargs)
self.default_ip_proxy = proxies
return res
2024-08-06 20:13:15 +00:00
utils.logger.error(f"[BaiduTieBaClient.get] 达到了最大重试次数IP已经被Block请尝试更换新的IP代理: {e}")
2024-08-08 06:19:32 +00:00
raise Exception(f"[BaiduTieBaClient.get] 达到了最大重试次数IP已经被Block请尝试更换新的IP代理: {e}")
async def post(self, uri: str, data: dict, **kwargs) -> Dict:
2024-08-05 10:51:51 +00:00
"""
POST请求对请求头签名
Args:
uri: 请求路由
data: 请求体参数
Returns:
"""
json_str = json.dumps(data, separators=(',', ':'), ensure_ascii=False)
return await self.request(method="POST", url=f"{self._host}{uri}",
data=json_str, **kwargs)
2024-08-05 10:51:51 +00:00
async def pong(self) -> bool:
"""
用于检查登录态是否失效了
Returns:
"""
utils.logger.info("[BaiduTieBaClient.pong] Begin to pong tieba...")
try:
uri = "/mo/q/sync"
res: Dict = await self.get(uri)
utils.logger.info(f"[BaiduTieBaClient.pong] res: {res}")
2024-08-05 10:51:51 +00:00
if res and res.get("no") == 0:
ping_flag = True
else:
utils.logger.info(f"[BaiduTieBaClient.pong] user not login, will try to login again...")
ping_flag = False
except Exception as e:
utils.logger.error(f"[BaiduTieBaClient.pong] Ping tieba failed: {e}, and try to login again...")
ping_flag = False
return ping_flag
async def update_cookies(self, browser_context: BrowserContext):
"""
API客户端提供的更新cookies方法一般情况下登录成功后会调用此方法
Args:
browser_context: 浏览器上下文对象
Returns:
"""
pass
2024-08-05 10:51:51 +00:00
async def get_notes_by_keyword(
2024-08-05 10:51:51 +00:00
self, keyword: str,
page: int = 1,
page_size: int = 10,
sort: SearchSortType = SearchSortType.TIME_DESC,
note_type: SearchNoteType = SearchNoteType.FIXED_THREAD,
2024-08-06 17:01:21 +00:00
) -> List[TiebaNote]:
2024-08-05 10:51:51 +00:00
"""
根据关键词搜索贴吧帖子
Args:
keyword: 关键词
page: 分页第几页
page_size: 每页大小
2024-08-05 10:51:51 +00:00
sort: 结果排序方式
note_type: 帖子类型主题贴主题+回复混合模式
Returns:
"""
uri = "/f/search/res"
params = {
"isnew": 1,
"qw": keyword,
"rn": page_size,
"pn": page,
"sm": sort.value,
"only_thread": note_type.value
}
page_content = await self.get(uri, params=params, return_ori_content=True)
return self._page_extractor.extract_search_note_list(page_content)
2024-08-05 10:51:51 +00:00
2024-08-06 17:01:21 +00:00
async def get_note_by_id(self, note_id: str) -> TiebaNote:
2024-08-05 10:51:51 +00:00
"""
根据帖子ID获取帖子详情
Args:
note_id:
Returns:
"""
2024-08-06 11:21:34 +00:00
uri = f"/p/{note_id}"
page_content = await self.get(uri, return_ori_content=True)
return self._page_extractor.extract_note_detail(page_content)
2024-08-05 10:51:51 +00:00
2024-08-06 18:34:56 +00:00
async def get_note_all_comments(self, note_detail: TiebaNote, crawl_interval: float = 1.0,
callback: Optional[Callable] = None) -> List[TiebaComment]:
2024-08-05 10:51:51 +00:00
"""
获取指定帖子下的所有一级评论该方法会一直查找一个帖子下的所有评论信息
Args:
2024-08-06 18:34:56 +00:00
note_detail: 帖子详情对象
2024-08-05 10:51:51 +00:00
crawl_interval: 爬取一次笔记的延迟单位
callback: 一次笔记爬取结束后
Returns:
"""
2024-08-06 18:34:56 +00:00
uri = f"/p/{note_detail.note_id}"
result: List[TiebaComment] = []
current_page = 1
while note_detail.total_replay_page >= current_page:
params = {
"pn": current_page
}
page_content = await self.get(uri, params=params, return_ori_content=True)
2024-08-06 20:13:15 +00:00
comments = self._page_extractor.extract_tieba_note_parment_comments(page_content,
note_id=note_detail.note_id)
2024-08-06 18:34:56 +00:00
if not comments:
2024-08-06 11:21:34 +00:00
break
if callback:
2024-08-06 18:34:56 +00:00
await callback(note_detail.note_id, comments)
2024-08-06 11:21:34 +00:00
result.extend(comments)
2024-08-06 20:13:15 +00:00
# 获取所有子评论
await self.get_comments_all_sub_comments(comments, crawl_interval=crawl_interval, callback=callback)
2024-08-06 18:34:56 +00:00
await asyncio.sleep(crawl_interval)
current_page += 1
2024-08-06 11:21:34 +00:00
return result
2024-08-06 20:13:15 +00:00
async def get_comments_all_sub_comments(self, comments: List[TiebaComment], crawl_interval: float = 1.0,
2024-08-06 18:34:56 +00:00
callback: Optional[Callable] = None) -> List[TiebaComment]:
2024-08-06 11:21:34 +00:00
"""
获取指定评论下的所有子评论
Args:
comments: 评论列表
crawl_interval: 爬取一次笔记的延迟单位
callback: 一次笔记爬取结束后
Returns:
"""
2024-08-06 20:13:15 +00:00
uri = "/p/comment"
2024-08-06 18:34:56 +00:00
if not config.ENABLE_GET_SUB_COMMENTS:
return []
2024-08-06 20:13:15 +00:00
# # 贴吧获取所有子评论需要登录态
# if self.headers.get("Cookies") == "" or not self.pong():
# raise Exception(f"[BaiduTieBaClient.pong] Cookies is empty, please login first...")
all_sub_comments: List[TiebaComment] = []
2024-08-08 06:19:32 +00:00
for parment_comment in comments:
if parment_comment.sub_comment_count == 0:
2024-08-06 20:13:15 +00:00
continue
current_page = 1
2024-08-08 06:19:32 +00:00
max_sub_page_num = parment_comment.sub_comment_count // 10 + 1
2024-08-06 20:13:15 +00:00
while max_sub_page_num >= current_page:
params = {
2024-08-08 06:19:32 +00:00
"tid": parment_comment.note_id, # 帖子ID
"pid": parment_comment.comment_id, # 父级评论ID
"fid": parment_comment.tieba_id, # 贴吧ID
2024-08-06 20:13:15 +00:00
"pn": current_page # 页码
}
page_content = await self.get(uri, params=params, return_ori_content=True)
sub_comments = self._page_extractor.extract_tieba_note_sub_comments(page_content,
2024-08-08 06:19:32 +00:00
parent_comment=parment_comment)
2024-08-06 20:13:15 +00:00
if not sub_comments:
break
if callback:
2024-08-08 06:19:32 +00:00
await callback(parment_comment.note_id, sub_comments)
2024-08-06 20:13:15 +00:00
all_sub_comments.extend(sub_comments)
await asyncio.sleep(crawl_interval)
current_page += 1
return all_sub_comments
2024-08-08 06:19:32 +00:00
async def get_notes_by_tieba_name(self, tieba_name: str, page_num: int) -> List[TiebaNote]:
"""
根据贴吧名称获取帖子列表
Args:
tieba_name: 贴吧名称
page_num: 分页数量
Returns:
"""
uri = f"/f?kw={tieba_name}&pn={page_num}"
page_content = await self.get(uri, return_ori_content=True)
return self._page_extractor.extract_tieba_note_list(page_content)
2024-08-24 01:12:03 +00:00
async def get_creator_info_by_url(self, creator_url: str) -> TiebaCreator:
"""
根据创作者ID获取创作者信息
Args:
creator_url: 创作者主页URL
Returns:
"""
page_content = await self.request(method="GET", url=creator_url, return_ori_content=True)
return self._page_extractor.extract_creator_info(page_content)
async def get_notes_by_creator(self, user_name: str, page_number: int) -> Dict:
"""
根据创作者获取创作者的所有帖子
Args:
user_name:
page_number:
Returns:
"""
uri = f"/home/get/getthread"
params = {
"un": user_name,
"pn": page_number,
"id": "utf-8",
"_": utils.get_current_timestamp()
}
return await self.get(uri, params=params)
async def get_all_notes_by_creator_user_name(self,
user_name: str, crawl_interval: float = 1.0,
callback: Optional[Callable] = None,
max_note_count: int = 0) -> List[TiebaNote]:
2024-08-24 01:12:03 +00:00
"""
根据创作者用户名获取创作者所有帖子
Args:
user_name: 创作者用户名
crawl_interval: 爬取一次笔记的延迟单位
callback: 一次笔记爬取结束后的回调函数是一个awaitable类型的函数
max_note_count: 帖子最大获取数量如果为0则获取所有
2024-08-24 01:12:03 +00:00
Returns:
"""
result = []
notes_has_more = 1
page_number = 1
page_per_count = 20
total_get_count = 0
while notes_has_more == 1 and (max_note_count == 0 or total_get_count < max_note_count):
2024-08-24 01:12:03 +00:00
notes_res = await self.get_notes_by_creator(user_name, page_number)
if not notes_res or notes_res.get("no") != 0:
utils.logger.error(
f"[WeiboClient.get_notes_by_creator] got user_name:{user_name} notes failed, notes_res: {notes_res}")
break
notes_data = notes_res.get("data")
notes_has_more = notes_data.get("has_more")
notes = notes_data["thread_list"]
2024-08-24 01:12:03 +00:00
utils.logger.info(
f"[WeiboClient.get_all_notes_by_creator] got user_name:{user_name} notes len : {len(notes)}")
note_detail_task = [self.get_note_by_id(note['thread_id']) for note in notes]
notes = await asyncio.gather(*note_detail_task)
if callback:
await callback(notes)
await asyncio.sleep(crawl_interval)
result.extend(notes)
page_number += 1
total_get_count += page_per_count
2024-08-24 01:12:03 +00:00
return result