MCPcopy Create free account
hub / github.com/ZyphrZero/z.ai2api_python / get_random_user_agent

Function get_random_user_agent

app/utils/user_agent.py:25–55  ·  view source on GitHub ↗

获取随机用户代理字符串 Args: browser_type: 指定浏览器类型 ('chrome', 'firefox', 'safari', 'edge') 如果为 None,则随机选择 Returns: str: 用户代理字符串

(browser_type: Optional[str] = None)

Source from the content-addressed store, hash-verified

23
24
25def get_random_user_agent(browser_type: Optional[str] = None) -> str:
26 """
27 获取随机用户代理字符串
28
29 Args:
30 browser_type: 指定浏览器类型 ('chrome', 'firefox', 'safari', 'edge')
31 如果为 None,则随机选择
32
33 Returns:
34 str: 用户代理字符串
35 """
36 ua = get_user_agent_instance()
37
38 # 如果没有指定浏览器类型,随机选择一个(偏向 Chrome 和 Edge)
39 if browser_type is None:
40 browser_choices = ["chrome", "chrome", "chrome", "edge", "edge", "firefox", "safari"]
41 browser_type = random.choice(browser_choices)
42
43 # 根据浏览器类型获取用户代理
44 if browser_type == "chrome":
45 user_agent = ua.chrome
46 elif browser_type == "edge":
47 user_agent = ua.edge
48 elif browser_type == "firefox":
49 user_agent = ua.firefox
50 elif browser_type == "safari":
51 user_agent = ua.safari
52 else:
53 user_agent = ua.random
54
55 return user_agent
56
57
58# 通用 UserAgent headers 生成函数

Callers 4

get_latest_fe_versionFunction · 0.90
_build_dynamic_headersFunction · 0.90
get_dynamic_headersFunction · 0.90
get_dynamic_headersFunction · 0.85

Calls 1

get_user_agent_instanceFunction · 0.85

Tested by

no test coverage detected