python項目中從接口獲取數據並存入本地數據庫


百度上看到的,覺得有用,留着備用

python項目中從接口獲取數據並存入本地數據庫
首先用postman測試接口
根據請求方式將數據存入數據庫中
首先用postman測試接口
通過url,選擇相應的請求方式,頭部,數據格式,點擊send看能否獲取數據

根據請求方式將數據存入數據庫中
下面是post請求方式

 

def get()
      URL = ''
      HEADERS = {'Content-Type': 'application/json'}
      JSON = {}
      response = request.post(URL,headers=HEADERS,json=JSON)
      #json.loads()用於將str類型的數據轉成dict
      jsondata = json.load(response.txt) 
      for i in jsondata:
             date1 = i[data]
             type1 = i[type]
             ...
             #拼接sql語句
             sql=""
             conn=MySQLdb.connect(host="localhost",user="root",passwd="sa",db="mytable")
            cursor=conn.cursor()
            ursor.execute(sql)
            

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM