天天看點

python發送cookie請求_Python3發送post請求,自動記住cookie

在做登入的post請求時,需要記住cookie,否則不能通路登入後的頁面。

下面是登入的代碼:

#coding:utf-8

import urllib

import http.cookiejar

url = "http://c.highpin.cn/Users/CLogin"

postdata =urllib.parse.urlencode({

"Logon_Password":"sunmin",

"Logon_PostCode":"fghc",

"Logon_RememberMe":"false",

"Logon_UserEmail":"[email protected]"

}).encode('utf-8')

header = {

"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,**;q=0.8","Accept-Encoding":"utf-8","User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0","Host":"h.highpin.cn","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8","Referer":"http://h.highpin.cn/ManageJob/PubNewJob","Cookie":"XXXXX","Connection":"keep-alive"

}

req = urllib.request.Request(url,postdata,header)

r=urllib.request.urlopen(req)print(r.read().decode('utf-8'))

說明:cookie中的内容我給替換掉了,放上自己的cookie就可以了

Python3和Python2的變更,可以參考文章:http://blog.csdn.net/samxx8/article/details/21535901