1. Using the Python simulation login csdn is divided into three steps:
1. access to Url=https://passport.csdn.net/account/login;
2. Analyze login information: Get Username,password and Hideen tag hidden properties from webpage, there are three hidden tags in csdn, Lt,execution,_eventid//Note that these three tags are dynamic than can be written dead
3.POST Data Analysis:
1. Construction belongs to the Hearder, while most Web sites require cookies, cookies must be set up cookie processor, the use of the session module will automatically save the cookies, will automatically save the data, Session can automatically keep the conversation process (Learning Java Network Programming), in the login csdn process has JS written jsessionid dynamic page password, in the process of access to add it to url+ '; ' + ' Jsessionid ' form.
However, there is no picture or verification code in this crawler, and it needs to be studied in depth .... Let's see the next section.
1 #----CODING-GBK----2 3 ImportRequests4 fromlxmlImportHTML5 ImportRe6 7Username="XXXXXX"8password="XXXXXXX"9 TenUrl="Https://passport.csdn.net/account/login" One defcraw_csdn (): As=requests. Session () -datas={'username': Username,'Password':P Assword} -head={'mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'} the -B=s.post (url,data=datas) - ##print (B.text) -Tree=html.fromstring (B.text) +Lt=list (Set (Tree.xpath ("//input[@name = ' lt ']/@value"))) [0] - +Execution=list (Set (Tree.xpath ("//input[@name = ' execution ']/@value"))) [0] A atEventid=list (Set (Tree.xpath ("//input[@name = ' _eventid ']/@value"))) [0] -Jsessionid=list (Set (Tree.xpath ("//link/@href"))) [0] -Jsessionid=str (Re.findall ('jsessionid= (. *)', Jsessionid) [0]) - -post_url=url+';'+'jsessionid='+Jsessionid - Print(Post_url) inPayload={ - 'username': Username, to 'Password':P Assword, + 'LT': LT, - 'Execution': Execution, the '_eventid': EventID * } $ Panax NotoginsengAgent='mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) gecko/20100101 firefox/48.0' -Head={ the 'Accept':"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", + 'user-agent': Agent, A 'Host':'passport.csdn.net', the 'Referer':'https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn', + - } $R=s.post (post_url,data=payload,headers=head) $ Print(R.text)
If __name__== "__main__": -CRAW_CSDN ()
The results show that the login is csdn HTML and the Web process to dig deeper into other things:
4. NetEase Cloud class second lesson crawl to the second level of code:
1 #--coding:utf-8-----2 #!/usr/bin/env python3 ImportRequests4 fromlxmlImportHTML5 from_cffi_backendImportstring6 7Url='Http://www.heibanke.com/accounts/login'8Logurl='http://www.heibanke.com/lesson/crawler_ex02/'9 Ten ##采用尝试方式发送数据得到其中cirf值 One defPost_data_url (s,url,data): Ab=s.get (URL) - -Tree=html.fromstring (B.text) theAuth_token=list (Set (Tree.xpath ("//input[@name = ' Csrfmiddlewaretoken ']/@value"))) [0] - PrintAuth_token - - ##使用这句话将cookies保存下来 +params={'Csrfmiddlewaretoken': B.cookies.get ('Csrftoken')} -Params.update (data)##将csrf加入到payload中 + AR=s.post (Url,data=params)##post向服务器发送的请求 at returnr,s - -s=requests. Session () -Payload={ - 'username':'Test', - 'Password':'test123' in } -r,s=Post_data_url (s,url,payload) to + PrintR.status_code - the forIinchRange (30): *Rr,s=post_data_url (s,logurl,{'username':'Heibanke','Password': Str (i)}) $ ifRr.text.find (U'Error') >0:Panax Notoginseng PrintI"no correct" -I+=1 the Else: + PrintRr.text A Break the +
5. When the analog login csdn, using the Requests_session () module, using the Httpfix crawl:
From the information obtained in: The post packet, also found in the Post URL has a section of the Jessionid dynamic code, in the Python writing process must be added to the URL at the same time to get the Post packet submission method:
Post-data:
The red arrow indicates the Post URL and finds something automatically added to the Jessionid.
Analog Login Csdn