1.python解析URL中的参数
src = 'http://www.baidu.com/search?a=1'
parseUrl = parse.urlparse(src)
query_dict = parse.parse_qs(parseUrl.query)
a = query_dict.get('a')[0]
2.pymysql
// 初始化
class DB:
def __init__(self):
self.conn = pymysql.connect(
host=configs.mysqlhost,
port=configs.mysqlport,
user=configs.mysqluser,
password=str(configs.mysqlpassword),
database=configs.mysqldatabase
)
self.cursor = self.conn.cursor()
// 基本使用
def test():
db = DB()
querySql = 'select id from tablename where ...'
arr = []
try:
db.cursor.execute(querySql)
id = db.cursor.fetchone()
if id:
...
except Exception as e:
print('test: ', e)
finally:
db.cursor.close()
db.conn.close()
3.带索引遍历
for index, item in enumerate(arr):
print(index, item)
4.python配置yaml
//pip install pyyaml
import yaml
config = None
try:
f = open('config.yaml')
data = yaml.load(f.read(), Loader=yaml.FullLoader)
config = data['config']
host = config['host']
f.close()
except Exception as e:
print('读取配置文件失败', e)
//config.yaml
config:
host: www...com
5.将文本中特殊字符替换为空格
def tidy_text(text):
txt = text
for i in '!"#$%&()*+,-./:;<=>?@[\]^_‘{|}~,。、 :':
txt = txt.replace(i, "") # 将文本中特殊字符替换为空格
return txt
6.文件常用操作
//拼接路径
os.path.join('path1', 'path2')
// 判断是否存在
os.path.exists(path)
// 创建文件夹
os.mkdir(path)
// 覆盖写入文件
with open(img_path, 'wb') as f:
f.write(content)
f.close()
7.带参数启动
parser = argparse.ArgumentParser(description='description')
parser.add_argument('--id', type=int, help='任务ID')
args = parser.parse_args()
if args.id is None:
print('参数未补全')
return
8.带session的网络请求
session = requests.session()
resp = session.post(url=startLoginUrl, data=params, headers=headers)
9.print转log
f = open('logs.txt', 'a')
sys.stdout = f
print('test')
10.python生成依赖清单
// 生成
pip freeze > requirements.txt
// 下载
pip install -r requriements.txt