V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
jadec0der
V2EX  ›  浏览器

请问有什么办法可以用浏览器的 cookie 跑 Python /Javascript 脚本吗?

  •  
  •   jadec0der · 191 天前 · 2041 次点击
    这是一个创建于 191 天前的主题,其中的信息可能已经有所发展或是发生改变。
    需求是想用脚本抓和处理一些内网 API 获取的数据。但是公司的内部登录有 2FA ,几小时就过期,要经常重新认证。

    之前的做法要么是复制浏览器 cookie 到 Python ,要么是建一个油猴脚本。但都感觉有些麻烦,希望有个 playground 类的扩展可以直接写代码然后跑,语言不太有所谓,主要是 HTTP 请求直接用浏览器 cookie ,要是内置一个 SQLite 就更棒了。
    12 条回复    2023-10-19 22:10:10 +08:00
    ShineyWang
        2
    ShineyWang  
       191 天前
    rukeypei
        3
    rukeypei  
       191 天前
    写一个 chrome extension 直接抓
    DeeCheung
        4
    DeeCheung  
       191 天前
    我自己用 `Bun(自带 sqlite) + js` 解析提取,一个 Bun 二进制+脚本即可,你也可以打包在一起单文件当 cli 用,缺点不支持 win

    ``` js
    // TODO v11 v12
    import { Database } from 'bun:sqlite'
    import { createDecipheriv, pbkdf2Sync } from 'node:crypto'

    const KEY_LENGTH = 16
    const SALT = 'saltysalt'
    const IV = Buffer.alloc(KEY_LENGTH).fill(' ')
    const password = 'peanuts'
    const key = getDerivedKey(password, 1)

    function getDerivedKey(password, iterations) {
    return pbkdf2Sync(password, SALT, iterations, KEY_LENGTH, 'sha1')
    }

    function decryptorCookie(encryptedCookie) {
    const decipher = createDecipheriv('AES-128-CBC', key, IV)
    const decryptedCookie = decipher.update(encryptedCookie.slice(3))
    return decryptedCookie.toString() + decipher.final('utf8')
    }

    function parseExpiresUtc(n) {
    return new Date(n / 1e3 - 116444736e5)
    }

    function parseCookie(item) {
    const { name, host_key, encrypted_value: str, expires_utc, has_expires } = item
    // V10
    const val = decryptorCookie(str)
    const expires = parseExpiresUtc(expires_utc)
    const unixTime = +(expires / 1e3).toFixed(0)
    const out = { key: name, val, has_expires, expires, host: host_key, unixTime }
    return out
    }

    const DefaultDbPath = '~/.config/chromium/Default/Cookies'
    export function getCookies(sql, dbPath = DefaultDbPath) {
    const db = new Database(dbPath)
    const query = db.query(sql)
    const items = query.all()
    return items.map(parseCookie)
    }

    export function toNetscapeCookieFile(arr) {
    const lines = arr
    .map(c => {
    return `${c.host} TRUE / TRUE ${c.unixTime} ${c.key} ${c.val}`
    })
    .join('\n')
    return `# Netscape HTTP Cookie File
    # This file is generated by yt-dlp. Do not edit.

    ${lines}`
    }

    export function toHeadersCookie(arr) {
    return arr.map(c => `${c.key}=${c.val}`).join('; ')
    }

    if (import.meta.main) {
    const sql = `SELECT * FROM cookies where host_key = '.aliyundrive.com' and name = 'cookie2';`
    console.log(getCookies(sql))

    const sql1 = `SELECT * FROM cookies where host_key = '.bilibili.com' and name = 'SESSDATA';`
    const cookies = await getCookies(sql)
    console.log(toNetscapeCookieFile(cookies))
    }

    ```
    ayfun
        6
    ayfun  
       191 天前
    原生 IndexedDB
    yh7gdiaYW
        7
    yh7gdiaYW  
       191 天前
    一般来说这种应该用 playwright/puppeteer ?不过需求比较简单的话可能重了点
    iOCZ
        8
    iOCZ  
       191 天前
    以前有个人用 go 写的 LeetCode 助手,使用了浏览器里的登录信息,用的一个三方去读取的
    julyclyde
        9
    julyclyde  
       191 天前
    我昨天刚实习了把 cookie 从浏览器抄到 requests 库

    先建立一个 SimpleCookie class 的对象,用 SimpleCookie 实例的 load 方法把 cookie 字符串加载进来
    再把这个对象压平成文本格式的 dict
    用 requests 的 session 对象的 cookie 成员对象的 update 方法读前述 dict 即可
    hollc
        10
    hollc  
       191 天前
    如果是因为 2FA 导致程序没法自动登录的话,有 python 实现的 2FA ,可以接入进去
    Cooooooode
        11
    Cooooooode  
       191 天前
    playwright 吧
    Qetesh
        12
    Qetesh  
       190 天前
    使用 selenium 模拟登录,2FA 在模拟登录也好解决。可以很方便取到最新 cookie
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2738 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 10:15 · PVG 18:15 · LAX 03:15 · JFK 06:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.