jiugg 发表于 2024-4-25 21:28

获取cookie源码,适合大部分网站

顾名思义就是获取cookie的
通过扫码登录的方式,通过LWPCookieJar模块,可以获取各大网站的cookie信息,并将其保存到指定的cookie文件中, 后续使用可以直接加载cookie文件,无需二次登录。
注意:切勿将保存的cookie文件随意泄露给他人,以免出现不可控因素。

from time import sleep
from http.cookiejar import LWPCookieJar
import requests
from re import findall
from tkinter import StringVar, Tk, messagebox
from os import path
from io import BytesIO
from PIL import Image, ImageTk
from qrcode import QRCode
from tkinter.ttk import Button, Label
from threading import Thread


temp_cookie = './cookie/bzcookies.txt'
headers = {
    'authority': 'api.vc.bilibili.com', 'accept': 'application/json, text/plain, */*',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6', 'content-type': 'application/x-www-form-urlencoded',
    'origin': 'https://message.bilibili.com', 'referer': 'https://message.bilibili.com/',
    'sec-ch-ua': '"Chromium";v="116", "Not)A;Brand";v="24", "Microsoft Edge";v="116"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty', 'sec-fetch-mode': 'cors', 'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.81',
}


def is_login(session):
    try:
      session.cookies.load(ignore_discard = True)
    except Exception as e:
      print(e)
    login_url = session.get("https://api.bilibili.com/x/web-interface/nav", verify = False, headers = headers).json()
    if login_url['code'] == 0:
      print(f"Cookies值有效, {login_url['data']['uname']}, 已登录!")
      # verification()
      return True
    else:
      print('Cookies值已经失效,请重新扫码登录!')
      # verification()
      return False


def scan_code(session2):
    global bili_jct
    get_login = session2.get('https://passport.bilibili.com/x/passport-login/web/qrcode/generate?source=main-fe-header', headers = headers).json()
    qrcode_key = get_login['data']['qrcode_key']
    qr = QRCode()
    qr.add_data(get_login['data']['url'])
    img = qr.make_image()
    pil_image_change = img.resize((200, 200), resample = Image.BICUBIC, box = None, reducing_gap = None)
    code_pic = ImageTk.PhotoImage(pil_image_change)
    token_url = f'https://passport.bilibili.com/x/passport-login/web/qrcode/poll?qrcode_key={qrcode_key}&source=main-fe-header'
    label_ver1 = Label(root, image = code_pic)
    v1.set('等待扫码')
    label_ver1.grid(row = 1, column = 1, rowspan = 8, columnspan = 1, sticky = 'n')
    while 1:
      qrcode_data = session2.get(token_url, headers = headers).json()
      if qrcode_data['data']['code'] == 0:
            v1.set('扫码成功')
            session2.get(qrcode_data['data']['url'], headers = headers)
            break
      else:
            v1.set(qrcode_data['data']['message'])
      sleep(1)
      root.update()
    session2.cookies.save()
    with open(temp_cookie, 'r', encoding = 'utf-8') as f:
      bzcookie = f.read()
    bili_jct = findall(r'bili_jct=(.*?);', bzcookie)


def bz_login():
    global code_pic
    session1.cookies = LWPCookieJar(filename = temp_cookie)
    status = is_login(session1)
    if not status:
      scan_code(session1)
      verification()
    else:
      verification()


def verification():
    url = 'https://api.bilibili.com/x/web-interface/nav'
    resp1 = session1.get(url = url, headers = headers).json()
    global tk_image
    if resp1['data']['isLogin']:
      face_url = resp1['data']['face']
      image_bytes = requests.get(face_url).content
      data_stream = BytesIO(image_bytes)
      pil_image = Image.open(data_stream)
      pil_image_change = pil_image.resize((200, 200), resample = Image.BICUBIC, box = None, reducing_gap = None)
      tk_image = ImageTk.PhotoImage(pil_image_change)
      status = "cookie有效!登录成功!"
    else:
      thread_it(bz_login)
      status = 'cookie无效!重新登录'
    label_ver = Label(root, image = tk_image)
    label_ver.grid(row = 1, column = 1, rowspan = 8, columnspan = 1, sticky = 'n')
    v1.set(status)


def thread_it(func, *args):
    thread = Thread(target = func, args = args, daemon = True)
    thread.start()


def cancel_login():
    msg1 = messagebox.askyesno(title = "提示", message = "注销后cookie将失效,是否注销登录?")
    if msg1:
      url3 = 'https://passport.bilibili.com/login/exit/v2'
      data3 = {'biliCSRF': f'{bili_jct}'}
      session1.post(url = url3, headers = headers, data = data3).json()
      verification()


if __name__ == '__main__':
    root = Tk()
    v1 = StringVar()
    if not path.exists(temp_cookie):
      with open(temp_cookie, 'w', encoding = 'utf-8') as f:
            f.write("")
    with open(temp_cookie, 'r', encoding = 'utf-8') as f:
      bzcookie = f.read()
    try:
      bili_jct = findall(r'bili_jct=(.*?);', bzcookie)
    except Exception as e:
      print(e)
    requests.packages.urllib3.disable_warnings()
    session1 = requests.session()
    root.geometry('300x225')
    root.title("cookie")
    thread_it(bz_login)
    btn1 = Button(root, width=10, text='注销登录', command=cancel_login)
    btn1.grid(row=3, column=2)
    label_ver2 = Label(root, textvariable = v1)
    label_ver2.grid(row = 9, column = 1, rowspan = 8, columnspan = 1, sticky = 'n')
    root.mainloop()

调味包 发表于 2024-4-26 08:53

个人感觉还是登录后快捷键方便
javascript:(function(){var oInput=document.createElement('input');oInput.value=document.cookie;document.body.appendChild(oInput);oInput.select();document.execCommand("Copy");oInput.className='oInput';oInput.style.display='none';alert('复制成功');})()

anning666 发表于 2024-4-26 08:40

cenoser795 发表于 2024-4-26 08:37
请教一下,
这段代码是扫描登陆获取cookies的,
将cookies保存成一个txt文件吗,再次使用的时候,再读取 ...

cookie的机制,注定只能维持多久的登录有效期,永久的cookie不推荐也不安全
基cookie的爬虫,每隔一段时间要重新获取cookie,登录才有效

lyie15 发表于 2024-4-25 23:04

请教一下,这样的PY代码,我需要用一个什么样的环境来运行呢,另外,PY能否做一些可视化操作的工具类呢

wasm2023 发表于 2024-4-25 22:06

感谢分享

shanzhanzhe 发表于 2024-4-25 22:07

学习啦,谢谢!

Sunny2023 发表于 2024-4-25 22:51

这个厉害,学习下思路

dailexing 发表于 2024-4-25 23:02

学习一下,正好也在学习python

deffedyy 发表于 2024-4-25 23:04

感谢分享!!

kyu9 发表于 2024-4-26 00:34

学习了,感谢分享!

xixicoco 发表于 2024-4-26 00:36

大概看了下代码,只适合b站吧

jiugg 发表于 2024-4-26 05:58

xixicoco 发表于 2024-4-26 00:36
大概看了下代码,只适合b站吧

写法都一样的,改几个关键信息就行了
页: [1] 2 3 4
查看完整版本: 获取cookie源码,适合大部分网站