kuaishou_did.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import json
  2. import requests
  3. import urllib3
  4. from fake_useragent import FakeUserAgent
  5. from requests.adapters import HTTPAdapter
  6. def get_did(user_agent):
  7. headers = {
  8. "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
  9. "Accept-Encoding": "gzip, deflate, br",
  10. "Accept-Language": "zh-CN,zh;q=0.9",
  11. "Cache-Control": "max-age=0",
  12. "Connection": "keep-alive",
  13. "Host": "www.kuaishou.com",
  14. "Sec-Ch-Ua": '"Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"',
  15. "Sec-Ch-Ua-Mobile": "?0",
  16. "Sec-Ch-Ua-Platform": '"macOS"',
  17. "Sec-Fetch-Dest": "document",
  18. "Sec-Fetch-Mode": "navigate",
  19. "Sec-Fetch-Site": "same-origin",
  20. "Sec-Fetch-User": "?1",
  21. "Upgrade-Insecure-Requests": "1",
  22. "User-Agent": user_agent
  23. }
  24. response = requests.get('https://www.kuaishou.com/search/video?searchKey=', headers=headers)
  25. value_list = response.cookies.values()
  26. for v in value_list:
  27. if "web_" in v:
  28. return v
  29. return ""
  30. def get_captchaSession(did, user_agent):
  31. payload = {
  32. "operationName": "graphqlSearchUser",
  33. "variables": {
  34. "keyword": "周大爷不服老"
  35. },
  36. "query": "query graphqlSearchUser($keyword: String, $pcursor: String, $searchSessionId: String) {\n visionSearchUser(keyword: $keyword, pcursor: $pcursor, searchSessionId: $searchSessionId) {\n result\n users {\n fansCount\n photoCount\n isFollowing\n user_id\n headurl\n user_text\n user_name\n verified\n verifiedDetail {\n description\n iconType\n newVerified\n musicCompany\n type\n __typename\n }\n __typename\n }\n searchSessionId\n pcursor\n __typename\n }\n}\n"
  37. }
  38. headers = {
  39. 'Accept': '*/*',
  40. 'Accept-Encoding': 'gzip, deflate, br',
  41. 'Accept-Language': 'zh-CN,zh;q=0.9',
  42. 'Connection': 'keep-alive',
  43. 'Content-Length': '668',
  44. 'Content-Type': 'application/json',
  45. 'Cookie': 'did={}'.format(did),
  46. 'Host': 'www.kuaishou.com',
  47. 'Origin': 'https://www.kuaishou.com',
  48. # 'Referer': 'https://www.kuaishou.com/search/author?searchKey=%E5%91%A8%E5%A4%A7%E7%88%B7%E4%B8%8D%E6%9C%8D%E8%80%81',
  49. 'Sec-Ch-Ua': '"Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"',
  50. 'Sec-Ch-Ua-Mobile': '?0',
  51. 'Sec-Ch-Ua-Platform': '"macOS"',
  52. 'Sec-Fetch-Dest': 'empty',
  53. 'Sec-Fetch-Mode': 'cors',
  54. 'Sec-Fetch-Site': 'same-origin',
  55. 'User-Agent': user_agent
  56. }
  57. urllib3.disable_warnings()
  58. s = requests.session()
  59. # max_retries=3 重试3次
  60. s.mount("http://", HTTPAdapter(max_retries=3))
  61. s.mount("https://", HTTPAdapter(max_retries=3))
  62. response = s.post(
  63. url="https://www.kuaishou.com/graphql",
  64. headers=headers,
  65. data=json.dumps(payload),
  66. # proxies=Common.tunnel_proxies(),
  67. verify=False,
  68. timeout=10,
  69. )
  70. response.close()
  71. # print(response.json())
  72. return response.json()['data']['url'].split("?")[-1]
  73. def get_config(did, user_agent, captcha_session):
  74. payload = captcha_session
  75. headers = {
  76. 'Accept': 'application/json, text/plain, */*',
  77. 'Accept-Encoding': 'gzip, deflate, br',
  78. 'Accept-Language': 'zh-CN,zh;q=0.9',
  79. 'Connection': 'keep-alive',
  80. 'Content-Length': '411',
  81. 'Content-Type': 'application/x-www-form-urlencoded',
  82. 'Cookie': 'did='.format(did),
  83. 'Host': 'captcha.zt.kuaishou.com',
  84. 'Origin': 'https://captcha.zt.kuaishou.com',
  85. 'Referer': 'https://captcha.zt.kuaishou.com/iframe/index.html?{}'.format(captcha_session),
  86. 'Sec-Ch-Ua': '"Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"',
  87. 'Sec-Ch-Ua-Mobile': '?0',
  88. 'Sec-Ch-Ua-Platform': '"macOS"',
  89. 'Sec-Fetch-Dest': 'empty',
  90. 'Sec-Fetch-Mode': 'cors',
  91. 'Sec-Fetch-Site': 'same-origin',
  92. 'User-Agent': user_agent
  93. }
  94. urllib3.disable_warnings()
  95. s = requests.session()
  96. # max_retries=3 重试3次
  97. s.mount("http://", HTTPAdapter(max_retries=3))
  98. s.mount("https://", HTTPAdapter(max_retries=3))
  99. response = s.post(
  100. url="https://captcha.zt.kuaishou.com/rest/zt/captcha/sliding/config",
  101. headers=headers,
  102. data=payload,
  103. # proxies=Common.tunnel_proxies(),
  104. verify=False,
  105. timeout=10,
  106. )
  107. response.close()
  108. print(json.dumps(response.json(), ensure_ascii=False, indent=4))
  109. return response.json()['captchaSn']
  110. def check(encrypted_param, did, user_agent):
  111. payload = {
  112. "verifyParam": encrypted_param
  113. }
  114. headers = {
  115. 'Accept': 'application/json, text/plain, */*',
  116. 'Accept-Encoding': 'gzip, deflate, br',
  117. 'Accept-Language': 'zh-CN,zh;q=0.9',
  118. 'Connection': 'keep-alive',
  119. 'Content-Length': '9246',
  120. 'Content-Type': 'application/json',
  121. 'Cookie': 'did={}'.format(did),
  122. 'Host': 'captcha.zt.kuaishou.com',
  123. 'Origin': 'https://captcha.zt.kuaishou.com',
  124. 'Sec-Ch-Ua': '"Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"',
  125. 'Sec-Ch-Ua-Mobile': '?0',
  126. 'Sec-Ch-Ua-Platform': '"macOS"',
  127. 'Sec-Fetch-Dest': 'empty',
  128. 'Sec-Fetch-Mode': 'cors',
  129. 'Sec-Fetch-Site': 'same-origin',
  130. 'User-Agent': user_agent
  131. }
  132. urllib3.disable_warnings()
  133. s = requests.session()
  134. # max_retries=3 重试3次
  135. s.mount("http://", HTTPAdapter(max_retries=3))
  136. s.mount("https://", HTTPAdapter(max_retries=3))
  137. response = s.post(
  138. url="https://captcha.zt.kuaishou.com/rest/zt/captcha/sliding/kSecretApiVerify",
  139. headers=headers,
  140. data=json.dumps(payload),
  141. verify=False,
  142. timeout=10,
  143. )
  144. response.close()
  145. if __name__ == "__main__":
  146. fake_agent = FakeUserAgent().random
  147. fake_did = get_did(fake_agent)
  148. print(fake_did)
  149. captcha_session = get_captchaSession(did=fake_did, user_agent=fake_agent)
  150. print(captcha_session)
  151. captchaSn = get_config(did=fake_did, user_agent=fake_agent, captcha_session=captcha_session)
  152. print(captchaSn)