Skip to content

Commit e20c83f

Browse files
committed
fix!: impersonate defaults to ""
1 parent a4eff3c commit e20c83f

File tree

5 files changed

+32
-14
lines changed

5 files changed

+32
-14
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,20 @@ if __name__ == '__main__':
158158
``` python
159159
from bilibili_api import select_client
160160

161-
select_client("curl_cffi") # 选择 curl_cffi
161+
select_client("curl_cffi") # 选择 curl_cffi,支持伪装浏览器的 TLS / JA3 / Fingerprint
162162
select_client("aiohttp") # 选择 aiohttp
163163
select_client("httpx") # 选择 httpx,不支持 WebSocket
164164
```
165165

166+
curl_cffi 支持伪装浏览器的 TLS / JA3 / Fingerprint,但需要手动设置。
167+
168+
``` python
169+
from bilibili_api import request_settings
170+
171+
request_settings.set("impersonate", "chrome131") # 第二参数数值参考 curl_cffi 文档
172+
# https://curl-cffi.readthedocs.io/en/latest/impersonate.html
173+
```
174+
166175
# FA♂Q
167176

168177
**Q: 关于 API 调用的正确姿势是什么?**

bilibili_api/clients/CurlCFFIClient.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(
2828
timeout: float = 0.0,
2929
verify_ssl: bool = True,
3030
trust_env: bool = True,
31-
impersonate: str = "chrome131",
31+
impersonate: str = "",
3232
http2: bool = False,
3333
session: Optional[requests.AsyncSession] = None,
3434
) -> None:
@@ -38,7 +38,7 @@ def __init__(
3838
timeout (float, optional): 请求超时时间. Defaults to 0.0.
3939
verify_ssl (bool, optional): 是否验证 SSL. Defaults to True.
4040
trust_env (bool, optional): `trust_env`. Defaults to True.
41-
impersonate (str, optional): 伪装的浏览器,可参考 curl_cffi 文档. Defaults to chrome131.
41+
impersonate (str, optional): 伪装的浏览器,可参考 curl_cffi 文档. Defaults to "".
4242
http2 (bool, optional): 是否使用 HTTP2. Defaults to False.
4343
session (object, optional): 会话对象. Defaults to None.
4444
@@ -79,12 +79,12 @@ def set_verify_ssl(self, verify_ssl: bool = True) -> None:
7979
def set_trust_env(self, trust_env: bool = True) -> None:
8080
self.__session.trust_env = trust_env
8181

82-
def set_impersonate(self, impersonate: str = "chrome131") -> None:
82+
def set_impersonate(self, impersonate: str = "") -> None:
8383
"""
8484
设置 curl_cffi 伪装的浏览器,可参考 curl_cffi 文档。
8585
8686
Args:
87-
impersonate (str, optional): 伪装的浏览器. Defaults to chrome131.
87+
impersonate (str, optional): 伪装的浏览器. Defaults to "".
8888
"""
8989
self.__session.impersonate = impersonate
9090

@@ -108,9 +108,9 @@ async def request(
108108
cookies: dict = {},
109109
allow_redirects: bool = False,
110110
) -> BiliAPIResponse:
111-
if headers.get("User-Agent"):
111+
if headers.get("User-Agent") and self.__session.impersonate != "":
112112
headers.pop("User-Agent")
113-
if headers.get("user-agent"):
113+
if headers.get("user-agent") and self.__session.impersonate != "":
114114
headers.pop("user-agent")
115115
request_log.dispatch(
116116
"REQUEST",
@@ -184,9 +184,9 @@ async def download_create(
184184
url: str = "",
185185
headers: dict = {},
186186
) -> int:
187-
if headers.get("User-Agent"):
187+
if headers.get("User-Agent") and self.__session.impersonate != "":
188188
headers.pop("User-Agent")
189-
if headers.get("user-agent"):
189+
if headers.get("user-agent") and self.__session.impersonate != "":
190190
headers.pop("user-agent")
191191
self.__download_cnt += 1
192192
request_log.dispatch(
@@ -220,9 +220,9 @@ def download_content_length(self, cnt: int) -> int:
220220
async def ws_create(
221221
self, url: str = "", params: dict = {}, headers: dict = {}
222222
) -> int:
223-
if headers.get("User-Agent"):
223+
if headers.get("User-Agent") and self.__session.impersonate != "":
224224
headers.pop("User-Agent")
225-
if headers.get("user-agent"):
225+
if headers.get("user-agent") and self.__session.impersonate != "":
226226
headers.pop("user-agent")
227227
self.__ws_cnt += 1
228228
request_log.dispatch(

bilibili_api/clients/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
ALL_PROVIDED_CLIENTS = [
6-
("curl_cffi", "CurlCFFIClient", {"impersonate": "chrome131", "http2": False}),
6+
("curl_cffi", "CurlCFFIClient", {"impersonate": "", "http2": False}),
77
("aiohttp", "AioHTTPClient", {}),
88
("httpx", "HTTPXClient", {"http2": False}),
99
]

docs/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,20 @@ if __name__ == '__main__':
158158
``` python
159159
from bilibili_api import select_client
160160

161-
select_client("curl_cffi") # 选择 curl_cffi
161+
select_client("curl_cffi") # 选择 curl_cffi,支持伪装浏览器的 TLS / JA3 / Fingerprint
162162
select_client("aiohttp") # 选择 aiohttp
163163
select_client("httpx") # 选择 httpx,不支持 WebSocket
164164
```
165165

166+
curl_cffi 支持伪装浏览器的 TLS / JA3 / Fingerprint,但需要手动设置。
167+
168+
``` python
169+
from bilibili_api import request_settings
170+
171+
request_settings.set("impersonate", "chrome131") # 第二参数数值参考 curl_cffi 文档
172+
# https://curl-cffi.readthedocs.io/en/latest/impersonate.html
173+
```
174+
166175
# FA♂Q
167176

168177
**Q: 关于 API 调用的正确姿势是什么?**

docs/request_client.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
| | 优先级 | request | stream | WebSocket | 额外网络请求设置 |
1212
| --------- | ------ | ------- | ------ | --------- | -------------------------------------------------------------------------- |
13-
| curl_cffi | 3 |||| - `impersonate` defaults to `chrome131` <br> - `http2` defaults to `False` |
13+
| curl_cffi | 3 |||| - `impersonate` defaults to ` ` <br> - `http2` defaults to `False` |
1414
| aiohttp | 2 |||| |
1515
| httpx | 1 |||| `http2` defaults to `False` |
1616

0 commit comments

Comments
 (0)