按月归档: 4 月 2023

requests的cookie使用

#响应头中的cookie获取
import requests
def get_buvid3():
url = ‘https://www.bilibili.com/video/BV17R4y1G7qt?spm_id_from=333.999.0.0’
headers = {
‘User-Agent’:’Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36′
}
response = requests.get(url=url,headers=headers)
#响应头中的cookie获取
result = response.cookies.get_dict()
return result[‘buvid3’]
buvid3_value = get_buvid3()
print(buvid3_value)

python *和**的用法 param, kwparam

*的用法
*在python中有多种功能和用法,下面一一给大家介绍。
1、乘法
这里的乘法有两种
1.1数字的乘法
print(3*4)
得到结果12
1.2数据的乘法,表示数据的加倍的次数
注意,可以针对字符串(str),列表(list),元组(tuple)做乘法
对于字典(dict),集合(set)则不能使用乘法
print(‘a’*4)
得到aaaa字符串
print([1,2]*2)
得到[1,2,1,2]

2、接收多余的数据,转换成列表
注意,对于元组,集合,最终返回的结果都是列表
a,b,*c = [1,2,3,4,5]
print(c)
得到[3, 4, 5]

a,b,*c = (9,8,7,6,5)
print(c)
得到[7,6,5]

3、定义函数时,接收参数,并将所有参数放入一个元组中
参数可以是同一类型的数据
def test1(*parm):
print(parm)
test1(1,2,3)
得到(1, 2, 3)

参数也可以是不同类型的数据
def test2(*parm):
print(parm)
test2(1, [‘2’,3], {‘a’:777})
得到(1, [‘2’, 3], {‘a’: 777})

4、调用函数时,分配参数
注意,此时参数个数要对应好,两个形参,就要传入两个实参
def test3(x,y):
print(x)
print(y)
param = [1,2]
test3(*param)
得到
1
2
如果传入实参和形参个数不一样,则会报错
def test4(x,y):
print(x)
print(y)
param = [1,2,3]
test4(*param)
TypeError: test2() takes 2 positional arguments but 3 were given

**的用法
5、定义函数时,将传入的参数组装成字典
def test5(**param):
print(param)
test5(x=1,y=2)
得到{‘x’: 1, ‘y’: 2}

6、调用函数时,分配参数
它获取到的是对应key的value
def test6(x,y):
print(x)
print(y)
param = {‘x’: 1, ‘y’: 2}
test6(**param)
得到
1
2

python实现json、ini、csv格式的文件转换

import json
from configparser import ConfigParser

# test.json
”’
[{“name”: “apple”, “price”: “10”, “amount”: “3”},
{“name”: “tesla”, “price”: “100000”, “amount”: “1”},
{“name”: “mac”, “price”: “3000”, “amount”: “2”},
{“name”: “lenovo”, “price”: “30000”, “amount”: “3”},
{“name”: “chicken”, “price”: “10”, “amount”: “3”}]
”’

# mysql.ini
”’
[DEFAULT]
a = test
[mysql]
default-character-set = utf8
[mysqld]
test1 = 200
datadir = /dbserver/data
port = 33060
character-set-server = utf8
sql_mode = NO_ENGINE_SUBSTRITUTION,STRICT_TRANS_TABLES
c = True
[test]
test1 = 100
”’

# sun.csv
”’
name,price,amount
apple,10,3
tesla,100000,1
mac,3000,2
lenovo,30000,3
chicken,10,3
”’

# file:json to csv
def json2csv(json_path, csv_path):
with open(json_path, ‘r’, encoding=’utf-8′) as f1:
with open(csv_path, ‘w’, newline=”) as f2:
ls = json.load(f1)
data = [list(ls[0].keys())]
for item in ls:
data.append(list(item.values()))
for line in data:
f2.write(‘,’.join(line) + ‘\n’)

# file:csv to json
def csv2json(json_path, csv_path):
ls = []
with open(csv_path, ‘r’, newline=”) as f1:
with open(json_path, ‘w’, encoding=’utf-8′) as f2:
for line in f1:
ls.append(line.replace(‘\n’, ”).split(‘,’))
# print(ls)
for i in range(1, len(ls)):
ls[i] = dict(zip(ls[0], ls[i]))
json.dump(ls[1:], f2, indent=4)
# 将Python数据类型转换成json格式,编码过程
# 默认是顺序存放,sort_keys是对字典元素按照key进行排序
# indent参数用语增加数据缩进,使文件更具有可读性

def ini2json(json_path, ini_path):
d = {}
cfg = ConfigParser()
cfg.read(ini_path)
for s in cfg.sections():
# print(s)
# print(cfg.items(s)) # 指定section,返回二元组
d[s] = dict(cfg.items(s))
with open(json_path, ‘w’) as f:
json.dump(d, f)

def json2ini(json_path, ini_path):
with open(json_path, ‘r’) as f1:
with open(ini_path, ‘w’) as f2:
dic = json.load(f1)
cfg = ConfigParser()
# print(ls)
for section, section_items in zip(dic.keys(), dic.values()):
cfg._write_section(f2, section, section_items.items(), delimiter=’=’)

if __name__ == ‘__main__’:
ini2json(‘./test2.json’, ‘./mysql.ini’)
json2ini(‘./test2.json’, ‘./sun.ini’)
json2csv(‘./test.json’, ‘./sun.csv’)
csv2json(‘./b.json’, ‘./sun.csv’)