<option id="mwy0y"><strong id="mwy0y"></strong></option>
  • <ul id="mwy0y"><sup id="mwy0y"></sup></ul>
  • <ul id="mwy0y"></ul>
  • <del id="mwy0y"><dfn id="mwy0y"></dfn></del><ul id="mwy0y"><sup id="mwy0y"></sup></ul>
  • <abbr id="mwy0y"></abbr>

    千鋒教育-做有情懷、有良心、有品質的職業教育機構

    400-811-9990
    手機站
    千鋒教育

    千鋒學習站 | 隨時隨地免費學

    千鋒教育

    掃一掃進入千鋒手機站

    領取全套視頻
    千鋒教育

    關注千鋒學習站小程序
    隨時隨地免費學習課程

    上海
    • 北京
    • 鄭州
    • 武漢
    • 成都
    • 西安
    • 沈陽
    • 廣州
    • 南京
    • 深圳
    • 大連
    • 青島
    • 杭州
    • 重慶
    當前位置:長沙千鋒IT培訓  >  技術干貨  >  python如何獲取cookie

    python如何獲取cookie

    來源:千鋒教育
    發布人:xqq
    時間: 2023-11-09 02:01:31

    介紹下3種python獲取cookie的方法。

    (1)借助handler

    這種方法也是網上介紹最多的一種方法,但是用起來比較麻煩

    fromhttpimportcookiejar

    fromurllibimportrequest

    classCraw():

    def__init__(self):

    self.url=''

    self.headers['User-Agent']='Mozilla/5.0(WindowsNT6.3;Win64;x64)'\

    'AppleWebKit/537.36(KHTML,likeGecko)Chrome/65.0.3325.162Safari/537.36'

    self.headers['Content-Type']='application/x-www-form-urlencoded'

    defgetCookies(self):

    cookie=cookiejar.CookieJar()

    handler=request.HTTPCookieProcessor(cookie)

    opener=request.build_opener(handler)

    response=opener.open(self.url)

    cookieValue=''

    foritemincookie:

    cookieValue+=item.name+'='+item.value+';'

    self.headers['Cookie']=cookieValue

    response=requests.get(url=self.url)

    defgetVerificationCode(self):

    img_url=''

    imgResponse=requests.get(url=img_url,headers=self.headers)#直接使用headers即可

    base64_jpg=base64.b64encode(imgResponse.content)

    returnbase64_jpg

    (2)使用responseheaders的set_cookie

    importrequests

    importre

    classCrawler():

    defgetCookie(self):

    response=requests.post(self.url)

    set_cookie=response.headers['Set-Cookie']

    array=re.split('[;,]',set_cookie)

    cookieValue=''

    forarrinarray:

    ifarr.find('DZSW_SESSIONID')>=0orarr.find('bl0gm1HBTB')>=0:

    cookieValue+=arr+';'

    (3)使用response的cookies屬性獲取

    只寫getCookies方法,代碼如下:

    importrequests

    classCrawler():

    defgetCookie(self):

    response=requests.get(self.url)

    cookie_value=''

    forkey,valueinresponse.cookies.items():

    cookie_value+=key+'='+value+';'

    self.headers['Cookie']=cookie_value

    以上內容為大家介紹了python培訓之如何獲取cookie,希望對大家有所幫助,如果想要了解更多Python相關知識,請關注IT培訓機構:千鋒教育。

    聲明:本站稿件版權均屬千鋒教育所有,未經許可不得擅自轉載。

    猜你喜歡LIKE

    python實現WSGI的框架

    2023-11-13

    python打開文本文件有哪些方法?

    2023-11-13

    python使用loguru操作日志

    2023-11-13

    最新文章NEW

    python-=是什么意思

    2023-11-13

    pythonre是什么?

    2023-11-13

    python列表追加元素出錯的解決

    2023-11-13

    相關推薦HOT

    更多>>

    快速通道 更多>>

    最新開班信息 更多>>

    網友熱搜 更多>>