python 通过文本获取cookies

公良泰宁
2023-12-01

简介

当cookies保存在文本文件中,并且有换行时可使用以下代码。

cookies.txt

BrowserId=sLz7_STyT12tV4xly6RdOQ;QCQQ=C5hPsneBh1y;
oinfo=c3RhdHVzPUFDVElWRSZ0eXBlPTImb2lkPTAwRDI4MDAwMDAxMjdHZQ==;
autocomplete=1;sid=00D2800000127Ge
!AQUAQLgf1650cJO58cc6yk0IZt8iuGXjkJM_uSHgi0yFdp9BDUkTBi_08xiQ59HqGvAZ3DY_srKmD5IQcqpDqW8U29rwTm5t;sid_Client=8000004Luae800000127Ge;clientSrc=223.104.63.26;inst=APP_0I;
oid=00D2800000127Ge;alohaEpt=SatAug18201814:34:35GMT+0800

方案一:
txtToCookies.py

def txt_cookies(file_name_):
    f = open(file_name_, "r")
    lines = f.readlines()

    lines = (line.strip() for line in lines)
    cookie = "".join(lines)

    f.close()

    return cookie


if __name__ == '__main__':
    file_name = "cookies.txt"
    cookies = txt_cookies(file_name)
    print(cookies)

方案二:
txtToCookies.py

def txt_cookies(file_name_):
    with open(file_name_, "r") as f:
        lines = f.readlines()
        lines = (line.strip() for line in lines)
        cookie = "".join(lines)

    return cookie


if __name__ == '__main__':
    file_name = "cookies.txt"
    cookies = txt_cookies(file_name)
    print(cookies)
 类似资料: