使用参考:https://www.yuque.com/u1046159/erg6ec/giok20
    config.ini

    1. [mysql]
    2. host = localhost
    3. user = root
    4. password =
    5. db = sizechart_spider
    6. port = 3306
    7. charset = utf8

    readconfig.py

    1. import configparser
    2. import os
    3. class ReadConfig:
    4. """读取配置文件类"""
    5. def __init__(self,filepath=None):
    6. if filepath:
    7. configpath =filepath
    8. else:
    9. root_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    10. configpath = os.path.join(root_dir, 'config.ini')
    11. print(configpath)
    12. self.cf = configparser.ConfigParser()
    13. self.cf.read(configpath, encoding="utf-8")
    14. def get_db(self, param):
    15. return self.cf.get('mysql', param)