Reading data from a page under Python makes it easy to implement requests with URLLIB2
Import Urllib2 print urllib2.urlopen ('http://www.baidu.com'). Read ()
The POST request operation that involves the page needs to provide the header information, the post data submitted and the request page.
The post data requires Urllib.encode (), which translates the dictionary into a "data1=value1&data2=value2" format.
ImportUrllibImportUrllib2header= { 'user-agent':'mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) gecko/20100101 firefox/31.0', 'Referer':'http://202.206.1.163/logout.do'}postdata= { 'data1':'value1', 'data2':'value2'}hosturl='http://xxx.com'Enpostdata=Urllib.urlencode (postdata) urlrequest=Urllib2. Request (hosturl,enpostdata,header) urlresponse=Urllib2.urlopen (urlrequest)PrintUrlresponse.read ()
After the request, the browser will have a session to keep the process, the session is stored in a cookie, the next page of the request will put the cookie to the request header, if the cookie lost session will be disconnected.
Under Python, you need to set the cookie hold
# Cookie Set # used to keep the sessionCJ= = = Urllib2.build_opener (cookie_support, Urllib2. HttpHandler) Urllib2.install_opener (opener)
Here is a library file that summarizes the above knowledge points for ease of use:
# filename:analogop.py
#!/usr/bin/python#-*-coding:utf-8-*-#Author: first line#qq:121866673#Mail: [email protected]#message:i need a python job#TIME:2014/10/5ImportUrllibImportUrllib2ImportCookielib#Cookie Set#used to keep the sessionCJ =Cookielib. Lwpcookiejar () Cookie_support=Urllib2. Httpcookieprocessor (CJ) Opener=Urllib2.build_opener (Cookie_support, Urllib2. HttpHandler) Urllib2.install_opener (opener)#Default HeaderHEADER = {
'user-agent':'mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) gecko/20100101 firefox/31.0', 'Referer':'http://202.206.1.163/logout.do'}#Operate MethoddefGeturlopen (Hosturl, postdata = {}, headers =HEADER):#encode PostDataEnpostdata =Urllib.urlencode (postdata)#Request URLURLRequest =Urllib2. Request (Hosturl, Enpostdata, headers)#Open URLUrlresponse =Urllib2.urlopen (urlrequest)#return URL returnUrlresponse
This is a test file because the reader does not have a test environment and needs to build or find a website test:
#filename: test.py
fromAnalogopImportGETURLOPENPOSTD= { 'Usernum':'2011411111', 'UPW':'124569', 'Userip':'192.168.10.1', 'token':'XXX'}urlread= Geturlopen ('http://127.0.0.1:8000/login/', POSTD)PrintUrlread.read (). Decode ('Utf-8') Urlread= Geturlopen ('http://127.0.0.1:8000/chafen/', {})PrintUrlread.read (). Decode ('Utf-8')
Program simulates browser request and session hold-python implementation