Login authentication code processing
Super Eagles , cloud play yards and other API interface to obtain.
Case: ancient poems Sign In https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx
. 1 Import Requests 2 from lxml.html.clean Import etree . 3 from fake_useragent Import UserAgent . 4 the UA = UserAgent () . 5 headers = { ' UserAgent ' : UA.random,} . 6 the session requests.Session = () # avoid cookie Effects generally used directly session request . 7 . 8 # codes processing (machine identification, the instructions refer to the official documentation http://www.chaojiying.com/api-14.html---ABC123456,ABC123456) . 9 Import requests 10 from hashlib Import MD5 11 class Chaojiying_Client(object): 12 13 def __init__(self, username, password, soft_id): 14 self.username = username 15 password = password.encode('utf8') 16 self.password = md5(password).hexdigest() 17 self.soft_id = soft_id 18 self.base_params = { 19 'user': self.username, 20 'pass2': self.password, 21 'softid': Self.soft_id, 22 is } 23 is self.headers = { 24 ' Connection ' : ' the Keep-Alive ' , 25 ' the User-- Agent ' : ' the Mozilla / 4.0 (compatible; MSIE 8.0; the Windows NT 5.1; Trident / 4.0) ' , 26 is } 27 28 DEF PostPic (Self, IM, CodeType): 29 "" " 30 IM: image byte 31 is CodeType: Title type reference http://www.chaojiying.com/price.html 32 " "" 33 params = { 34 'codetype': codetype, 35 } 36 params.update(self.base_params) 37 files = {'userfile': ('ccc.jpg', im)} 38 r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers) 39 return r.json() 40 41 def ReportError(self, im_id): 42 """ 43 im_id:报错题目的图片ID 44 "" " 45 the params = { 46 is ' ID ' : im_id, 47 } 48 params.update (self.base_params) 49 R & lt requests.post = ( ' http://upload.chaojiying.net/Upload/ReportError.php ' , the params = Data, headers = self.headers) 50 return r.json () 51 is DEF getCodeImgText (imgpath, imgType): # imgType type codes official website >> price system 52 is chaojiying = Chaojiying_Client ( ' bobo328410948 ' , ' bobo328410948 ', ' 899370 ' ) # User ID Center software generates a replacement >> 96001 53 is IM = Open (imgpath, ' RB ' ) .read () # local file path to image WIN system may need to replace a.jpg // 54 is return chaojiying .PostPic (IM, imgType) [ ' pic_str ' ] 55 56 is 57 is # dimensional code recognition 58 URL = ' https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen. ORG / User / collect.aspx ' 59 Response = Session.get (URL, headers = headers) 60 Tree = etree.HTML (response.text) 61 is img_src='https://so.gushiwen.org/'+tree.xpath('//img[@id="imgCode"]/@src')[0] 62 print(img_src) 63 response_img=session.get(url=img_src,headers=headers) 64 with open('Code.jpg','wb')as f : 65 f.write(response_img.content) 66 code=getCodeImgText('Code.jpg',1004) 67 print(code) 68 #一般额外的参数隐藏在页面中 69 __VIEWSTATE=tree.xpath('//input[@id="__VIEWSTATE"]/@value')[0] 70 __VIEWSTATEGENERATOR=tree.xpath('//input[@id="__VIEWSTATEGENERATOR"]/@value')[0] 71 print(__VIEWSTATE) 72 print(__VIEWSTATEGENERATOR) 73 #请求参数 74 data = { 75 '__VIEWSTATE': __VIEWSTATE, 76 '__VIEWSTATEGENERATOR': __VIEWSTATEGENERATOR, 77 'from ' : ' http://so.gushiwen.org/user/collect.aspx ' , 78 ' In Email ' : ' [email protected] ' , 79 ' pwd ' : ' ABC123456 ' , 80 ' code ' : code, 81 ' denglu ' : ' login ' , 82 } 83 # log data submitted data 84 URL = 'https://so.gushiwen.org/user/login.aspx?from=http%3a%2f%2fso.gushiwen.org%2fuser%2fcollect.aspx' 85 response = session.post(url=url,data=data,headers=headers) 86 with open('index.html','w',encoding='utf-8')as f: 87 f.write(response.text) 88 # print(response.text) 89 text=etree.HTML(response.text).xpath('//div[@class="mainreg2"]') 90 for i in text: 91 print(i.xpath('./span[2]/text()'))