python 的cookie處理操作

來源:互聯網
上載者:User

  • 使用已有的cookie訪問網站

import cookielib, urllib2

ckjar = cookielib.MozillaCookieJar(os.path.join(’C:\Documents and Settings\tom\Application Data\Mozilla\Firefox\Profiles\h5m61j1i.default’, ‘cookies.txt’))

req = urllib2.Request(url, postdata, header)

req.add_header(’User-Agent’, \ 

‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)’)

opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(ckjar) )

f = opener.open(req) 

htm = f.read() 

f.close()

  • 訪問網站獲得cookie,並把獲得的cookie儲存在cookie檔案中

import cookielib, urllib2

req = urllib2.Request(url, postdata, header) 

req.add_header(’User-Agent’, \ 

‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)’)

ckjar = cookielib.MozillaCookieJar(filename) 

ckproc = urllib2.HTTPCookieProcessor(ckjar)

opener = urllib2.build_opener(ckproc)

f = opener.open(req) 

htm = f.read() 

f.close()

ckjar.save(ignore_discard=True, ignore_expires=True)

  • 使用指定的參數產生cookie,並用這個cookie訪問網站

import urllib.request as urimport urllib.parse as up#import http.cookiejar as cj#coding:gbkcharset='utf8'#組件資訊#cookiejar = cj.CookieJar()#urlopener = ur.build_opener(ur.HTTPCookieProcessor(cookiejar))urlopener = ur.build_opener(ur.HTTPCookieProcessor())#jiayuan配置資訊name='Yatere'uid='22017518'#http頭headers={'User-Agent':'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)'}seachhead={'User-Agent':'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)',           'Referer':'http://search.jiayuan.com/',           'Origin':'http://search.jiayuan.com'}#使用者登入datavalues = {'password':'198398','name':'yin_kai@163.com'}#搜尋datasearchdata={    'sex':'f',    'work_location':'42',    'work_sublocation':'4201',    'min_age':'22',    'max_age':'26',    'min_height':'160',    'max_height':'180',    'education':'20',    'edu_more_than':'on',    'astro':'0',    'animal':'',    'bloodtype':'0',    'income':'0',    'house':'0',    'auto':'0',    'marriage':'1',    'children':'0',    'level':'0',    'industry':'0',    'company':'0',    'home_location':'42',    'home_sublocation':'4201',    'nation':'0',    'belief':'0',    'ques_love':'0',    'avatar':'on',    'save_name':''}#post資料轉換程式def data(values):    data=up.urlencode(values).encode()    return data#訪問指定頁面def geturlcon(url,data=None,headers=headers):    request = ur.Request(url,data,headers)    url = urlopener.open(request)    page=url.read().decode('utf8','ignore')    return page#檢測是否登入成功def checklogin(page):    if page.find(name)>0:        return True    elif page.find(uid)>0:        return True    else:        return False        #訪問登陸頁面(獲得cookie)url1='http://login.jiayuan.com/dologin.php'geturlcon(url1,data(values),headers)#登入後訪問其他頁面url2='http://www.jiayuan.com/usercp/'page=geturlcon(url2)if checklogin(page):    print ('登入成功')else:    print ('登入失敗')url3='http://profile.jiayuan.com/14214171'page=geturlcon(url3)if checklogin(page):    print (url3,'登入成功')else:    print ('登入失敗')#查看搜尋結果url3='http://search.jiayuan.com/result.php?t=10&m=1'page=geturlcon(url3,data(searchdata),seachhead)if checklogin(page):    print (url3,'登入成功')else:    print ('登入失敗')    
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.