csv

import csv
# encoding='utf-8':设置文件内容编码
# newline='':写入避免双倍行距
def write_data():
    with open('stu.csv', 'a+', encoding='utf-8', newline='') as file:
        writer = csv.writer(file)
        writer.writerow(['Jhone', 23, '0001', '男'])  # 单行写入
        writer.writerows([{'Jhone', 23, '0001', '男'}, {'Henri', 10, '0002', '女'}])  # 多行写入


def read_data():
    with open('stu.csv', 'r', newline='', encoding='utf-8') as file:
        reader = csv.reader(file)
        for row in reader:
            print(row)


if __name__ == '__main__':
    # write_data()
    read_data()
import os.path
import urllib
from http import cookiejar

filename = 'cookies.txt'


def file_exists(filename):
    if not os.path.exists(filename):
        with open(filename, mode='w', encoding='utf-8') as f:
            pass
        print(f"文件已创建:{filename}")
    else:
        print(f"文件已存在:{filename}")


def get_cookies():
    file_exists(filename)
    cookie = cookiejar.MozillaCookieJar(filename)
    handler = urllib.request.HTTPCookieProcessor(cookie)

    opener = urllib.request.build_opener(handler)

    url = 'https://tieba.baidu.com/index.html?traceid=#'
    resp = opener.open(url)
    cookie.save(filename)


def use_cookie():
    cookie = cookiejar.MozillaCookieJar(filename)
    cookie.load(filename)
    print(cookie)


if __name__ == '__main__':
    get_cookies()
    use_cookie()