国产成人AV无码一二三区,少女1到100集,国产精品久久久久精品综合紧,巜公妇之诱感肉欲HD在线播放

文章 > Python常见问题 > python如何获取cookie

python如何获取cookie

头像

(*-*)浩

2019-08-29 09:57:0212088浏览 · 0收藏 · 0评论

介绍下3种python获取cookie的方法。

(1)借助handler

这种方法也是网上介绍最多的一种方法,但是用起来比较麻烦

from http import cookiejar
from urllib import request

class Craw():
    def __init__(self):
        self.url = ''
        self.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) ' \
                                     'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36'
        self.headers['Content-Type'] = 'application/x-www-form-urlencoded'

    def getCookies(self):
        cookie = cookiejar.CookieJar()
        handler = request.HTTPCookieProcessor(cookie)
        opener = request.build_opener(handler)
        response = opener.open(self.url)
        cookieValue = ''
        for item in cookie:
            cookieValue += item.name + '=' + item.value + ';'
        self.headers['Cookie'] = cookieValue
        response = requests.get(url=self.url)
 
    def getVerificationCode(self):
     img_url = ''
        imgResponse = requests.get(url=img_url,headers = self.headers) #直接使用headers即可
        base64_jpg = base64.b64encode(imgResponse.content)
        return base64_jpg

(2)使用response headers的set_cookie 

import requests
import re
class Crawler():
   def getCookie(self):
     response = requests.post(self.url)
    set_cookie = response.headers['Set-Cookie']
    array = re.split('[;,]',set_cookie)
    cookieValue = ''
    for arr in array:
      if arr.find('DZSW_SESSIONID') >= 0 or arr.find('bl0gm1HBTB') >= 0:
        cookieValue += arr + ';'

(3)使用response的cookies属性获取

只写getCookies方法,代码如下:

import requests    
class Crawler():
    def getCookie(self):
        response = requests.get(self.url)
        cookie_value = ''
        for key,value in response.cookies.items():  
            cookie_value += key + '=' + value + ';'  
        self.headers['Cookie'] = cookie_value
关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

《大尺度吃奶做爰》电影| 约会大作战H| 无人区6高清免费追剧软件介绍| 已满十八岁免费观看电视剧| 苏畅| 火车上荫蒂添的好舒服图片| 夹好我的精去上课别流出来了| 《妻子6》在线观看免费版电视剧| 《女教师的味道3》在线观看| 南来北往电视剧免费观看完整版高..| 宝宝扶着水池乖乖让爸爸洗头文 | 扌暴辶鬲和扌暴辶的区别| 老公在洗澡我被公干日本| 阴部按摩出水太尴尬怎么办| 恋爱之瘾电影完整版在线观看免费| DK| 麻豆 无码 国产在线观看| 共享女儿小诗1~10章的背景资.| 亚洲乱熟女一区二区三区山口珠理| 王娟被老赵一夜干4次| 私处按摩SPA养生精油| 《特殊游泳教练》| 门卫老头吮她的花蒂的主演是谁 | 簧片| 暴躁少女免费看完整版| 上海吃奶门| 快拨出天我是你母亲歌词监控| 顶到里面了~疼| 妈妈がだけの心に漂う| 77777影院在线观看电视剧大..| 李丽珍《红颜祸水》| 巜生殖按摩1995伦理| 不想和你有遗憾电影在线观看| 宋芸桦| 孕妇| 淫语....粗话..脏话小说 | 《酒店激战》动漫1-5集免费观..| 法国《口咬》电影完整版| 《饥饿妻子》完整版在线观看| 自慰是什么| 国精-二二产品无人区