shixin.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import json
  2. import time
  3. from urllib import parse
  4. import ddddocr
  5. import requests
  6. from RS import Get_Cookie
  7. from Captcha import Init_Captcha
  8. import re
  9. class Get_Info:
  10. def __init__(self):
  11. self.Get_Cookie = Get_Cookie()
  12. self.session, self.ctx200 = self.Get_Cookie.main()
  13. self.updateCookie = self.Get_Cookie.update_cookie
  14. self.initURL = "http://zxgk.court.gov.cn/shixin"
  15. self.CaptchaCheckURL = "http://zxgk.court.gov.cn/shixin/checkyzm.do"
  16. self.ListURL = "http://zxgk.court.gov.cn/zhixing/searchBzxr.do"
  17. self.DetailURL = "http://zxgk.court.gov.cn/zhixing/newdetail"
  18. self.headers = {
  19. "Connection": "keep-alive",
  20. "Pragma": "no-cache",
  21. "Cache-Control": "no-cache",
  22. "Accept": "application/json, text/javascript, */*; q=0.01",
  23. "X-Requested-With": "XMLHttpRequest",
  24. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36",
  25. "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
  26. "Origin": "http://zxgk.court.gov.cn",
  27. "Referer": "http://zxgk.court.gov.cn/zhixing/",
  28. "Accept-Language": "zh-CN,zh;q=0.9"
  29. }
  30. self.pCode = None
  31. self.ocr = ddddocr.DdddOcr()
  32. self.totalPage = 0
  33. self.Id_CaseCode = {}
  34. self.names = ["小明","小白","张三","王五","张伟"]
  35. self.file = open("test.txt",mode="w",encoding="utf-8")
  36. def init_page(self):
  37. init_page_response = self.session.get(url=self.initURL, headers=self.headers)
  38. print("init_page_response ===>", init_page_response)
  39. if init_page_response.status_code == 412:
  40. print("cookie失效,重新获取ts")
  41. exit(0)
  42. self.updateCookie()
  43. def init_captcha(self):
  44. self.CaptchaURL, self.captchaId, self.MyRandom = Init_Captcha().main()
  45. # print("获取验证码的URL ===>",self.CaptchaURL)
  46. Captcha_headers = {
  47. "Connection": "keep-alive",
  48. "Pragma": "no-cache",
  49. "Cache-Control": "no-cache",
  50. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36",
  51. "Accept": "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8",
  52. "Referer": "http://zxgk.court.gov.cn/zhixing/",
  53. "Accept-Language": "zh-CN,zh;q=0.9"
  54. }
  55. CaptchaResponse = self.session.get(url=self.CaptchaURL,headers=Captcha_headers)
  56. if CaptchaResponse.status_code == 412:
  57. print("cookie失效,重新获取ts")
  58. exit(0)
  59. # print("请求验证码的响应结果 ===>",CaptchaResponse)
  60. with open("captcha.jpg", 'wb') as f:
  61. f.write(CaptchaResponse.content)
  62. self.pCode = self.ocr.classification(CaptchaResponse.content)
  63. # print("验证码的结果 ===>",self.pCode)
  64. self.updateCookie()
  65. def check_Captcha(self):
  66. url = "checkyzm.do?captchaId=" + self.captchaId + "&pCode=" + self.pCode + ""
  67. checkURL = self.ctx200.call("check_yzm","GET",url,self.captchaId,self.pCode)
  68. checkURL = "http://zxgk.court.gov.cn" + checkURL
  69. # print("验证码验证的URL ===>",checkURL)
  70. response = self.session.get(url=checkURL,headers=self.headers)
  71. if response.status_code == 412:
  72. print("cookie失效,重新获取ts")
  73. exit(0)
  74. if response.text.split("\n")[0] == "1":
  75. return True
  76. return False
  77. def check_yzm(self):
  78. while True:
  79. self.init_captcha()
  80. ret = self.check_Captcha()
  81. self.updateCookie()
  82. if ret == True:
  83. break
  84. else:
  85. print("验证失败,正在进行重新请求验证码并验证")
  86. print("验证码的验证通过")
  87. def get_list(self, pname, currentPage):
  88. # print("执行了get_list")
  89. url = "http://zxgk.court.gov.cn"
  90. url = url + self.ctx200.call("get_list","POST","searchSX.do")
  91. print("请求列表页的URL ===>",url)
  92. data = {
  93. "pName": pname,
  94. "pCardNum": "",
  95. "pProvince": "0",
  96. "pCode": self.pCode,
  97. "captchaId": self.captchaId,
  98. "currentPage": str(currentPage)
  99. }
  100. response = self.session.post(url=url,headers=self.headers,data=data)
  101. print("response =>",response.text)
  102. if response.status_code == 412:
  103. print("cookie失效,重新获取ts")
  104. exit(0)
  105. try:
  106. result = json.loads(response.text)[0].get("result")
  107. self.totalPage = json.loads(response.text)[0].get("totalPage")
  108. for ret in result:
  109. self.Id_CaseCode[ret["id"]] = ret["caseCode"]
  110. print("self.Id_CaseCode ===>",self.Id_CaseCode)
  111. self.updateCookie()
  112. return True
  113. except:
  114. print(response.content.decode())
  115. print("出现了error,进行刷新验证码操作")
  116. self.check_yzm()
  117. return False
  118. def get_detail(self):
  119. for id in self.Id_CaseCode:
  120. print("当前的id对应的caseCode ===>",self.Id_CaseCode[id])
  121. caseCode = parse.quote(self.Id_CaseCode[id])
  122. checkURL = "disDetailNew?id=" + str(id) + "&caseCode=" + caseCode + "&pCode="+self.pCode+"&captchaId=" + self.captchaId
  123. ret = self.ctx200.call("get_detail","GET",checkURL,self.captchaId,self.pCode, str(id) ,caseCode)
  124. ret = "http://zxgk.court.gov.cn" + ret
  125. print("当前请求的详情页 ===>",ret)
  126. response = self.session.get(url=ret,headers=self.headers)
  127. if response.status_code == 412:
  128. print("cookie失效,重新获取ts")
  129. exit(0)
  130. id = json.loads(response.text).get("id")
  131. print("id =>",id)
  132. if id == None:
  133. print(response.content.decode())
  134. print("进行刷新验证码操作")
  135. self.init_captcha()
  136. self.check_Captcha()
  137. print("resp.text =>", response.text)
  138. self.file.write(response.text)
  139. self.file.write("\n")
  140. self.updateCookie()
  141. self.Id_CaseCode = {}
  142. def test(self):
  143. pass
  144. def main(self):
  145. self.init_page()
  146. self.check_yzm()
  147. for name in self.names:
  148. index = 1
  149. self.file.write(f"关键字 =======> {name}")
  150. self.file.write("\n")
  151. while True:
  152. time.sleep(0.5)
  153. if self.get_list(name, index):
  154. self.get_detail()
  155. if index == self.totalPage:
  156. break
  157. index += 1
  158. print("index =>",index)
  159. print(f"关键字为 {name} 搜索完毕")
  160. self.totalPage = 0
  161. self.file.close()
  162. if __name__ == '__main__':
  163. Get_Info().main()