获取cookie里面的sessionid

论坛 期权论坛 脚本     
匿名技术用户   2021-1-3 23:38   26   0

做接口的时候我们需要获取一个令牌,类似于token以及sessionid这样的签名,也可以说是一个令牌。

import urllib.request, urllib.parse, urllib.error
import http.cookiejar
class Cookie_login:
    def cookie_login(self):

        url = "http://192.168.110.85:8488/motorcade/ac/login"
        params = {"username":"17600151400","password":"aec60231d83fe6cf81444bc536596887,bfff2dd4f1b310eb0dbf593bd83f94dd8d34077e","verifycode":"","redirectUrl":"../../home.html"}
        # 获取cookie
        postdata = urllib.parse.urlencode(params).encode()
        cookie_filename = 'cookie.txt'
        cookie = http.cookiejar.MozillaCookieJar(cookie_filename)
        handler = urllib.request.HTTPCookieProcessor(cookie)
        opener = urllib.request.build_opener(handler)
        request = urllib.request.Request(url,postdata)

        try:
            response = opener.open(request)
        except urllib.error.URLError as e:
            print(e.code, ':', e.reason)

        # 保存cookie到cookie.txt中
        cookie.save(ignore_discard=True, ignore_expires=True)

        # 把字典存放到session_id里面
        session_id ={}

        for item in cookie:
            session_id[item.name]=item.value

        return session_id

可以参考下面的这篇文章

Python3+Selenium获取session和token供Requests使用教程

转载于:https://www.cnblogs.com/666666pingzi/p/10520478.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP