python通過API獲取資料並入庫

2021-10-08 03:25:28 字數 1569 閱讀 9238

背景:

idea2020,mysql8.0,python3.7   通過京推推商品介面獲取所有商品資料並入庫

需要提前建庫表

**:

import json

import pymysql as pymysql

import requests

def addjttapi():

try:

#需要插入資料庫資訊

conn1 = pymysql.connect(host='localhost', user='username',

passwd='password', port=3306, db='jtuituiapi', charset='utf8')

cur1 = conn1.cursor()

pagenum = range(1, 100) #分頁獲取資料1-100頁的所有商品資料

results =

for a in pagenum: #for迴圈遍歷每頁的商品資訊

#介面鏈結

url = ""

"page": a, "num": "單頁面顯示條數"}

ret = requests.post(url, json=requestdata, headers=headers)

if ret.status_code == 200:

text = json.loads(ret.text)

test1 = text['result']['data']

for i in range(len(test1)):

#需要獲取商品的字段

data=[test1[i]['jid'], test1[i]['goods_id'], test1[i]['goods_name'], test1[i]['goods_price'], test1[i]['goods_link'], test1[i]['discount_price'],

test1[i]['coupon_price'], test1[i]['commission'], test1[i]['cid1name']]

#每獲取一頁商品資訊執行一次插入語句

writesql = "insert into jtuituiapi (jid,goods_id,goods_name,goods_price,goods_link," \

"discount_price,coupon_price,commission,cid1name)" \

"values (%s, %s, %s,%s, %s, %s,%s, %s, %s )"

param = results

print(writesql)

cur1.executemany(writesql, param)

conn1.commit()

cur1.close()

conn1.close()

print("插入成功")

except pymysql.error as e:

print("could not connect to mysql server", e)

if __name__ == '__main__':

addjttapi()

python通過api獲取值,解析

cmp info f e t svc.get cmp basic time node api company name 工商資訊 cmp data cmp info.get cmp model.data,查詢的是api返回字典中是否有data這個鍵,若有,則返回該鍵對應的值 如果沒有則返回 clas...

python通過flask編寫API

pip install json pip install flask import flask,json from flask import request flask seb框架,通過flask提供的裝飾器 server.route 將普通函式轉換為服務 登入介面,需要傳入url,username...

通過api獲取控制代碼控制其他窗體

很多時候,編寫程式模擬滑鼠和鍵盤操作可以方便的實現你需要的功能,而不需要對方程式為你開放介面。比如,操作飛信定時傳送簡訊等。我之前開發過飛信耗子,用的是對飛信協議進行抓包,然後分析協議,進而模擬協議的執行,開發出了客戶端,與移動伺服器進行通訊,但是這有一些缺點。如果移動的伺服器對介面進行變更,我所編...