Program simulation browser requests and session persistence-python allows you to read data from a page in python through urllib2
import urllib2print urllib2.urlopen('http://www.pythontab.com').read()
Header information, submitted POST data, and request pages are required for page post request operations.
The post data requires urllib. encode () to convert the dictionary to the format of "data1 = value1 & data2 = value2.
import urllibimport urllib2 HEADER = { '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) print urlresponse.read()
After the request, the browser will have a session persistence process. sessions are stored in a cookie. the next page request will put the cookie in the request header, and the session will be disconnected if the cookie is lost.
Set cookie persistence in python.
# Cookie set # used to keep the session cj = cookielib. LWPCookieJar () cookie_support = Encrypt (cj) opener = urllib2.build _ opener (cookie_support, urllib2.HTTPHandler) urllib2.install _ opener)
The following 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: zxbd1016@163.com # message: I need a python job # time: import urllibimport urllib2import cookielib # cookie set # used to maintain session cj = cookielib. LWPCookieJar () cookie_support = Encrypt (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/123456', 'referer': 'http: // 202.206.1.163/logout. do '} # operate methoddef geturlopen (hosturl, postdata ={}, headers = HEADER): # encode postdata enpostdata = urllib. urlencode (postdata) # request url urlrequest = urllib2.Request (hosturl, enpostdata, headers) # open url urlresponse = urllib2.urlopen (urlrequest) # return url return urlresponse
This is a test file, because the reader does not have a test environment, you need to build your own or find a website for testing:
#filename: test.pyfrom analogop import geturlopen postd = { 'usernum': '2011411111', 'upw': '124569', 'userip': '192.168.10.1', 'token': 'xxx'} urlread = geturlopen('http://127.0.0.1:8000/login/', postd)print urlread.read().decode('utf-8')urlread = geturlopen('http://127.0.0.1:8000/chafen/', {})print urlread.read().decode('utf-8')