日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

爬取json Swaggerui界面

發布時間:2023/12/20 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 爬取json Swaggerui界面 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?對一個靜態的網頁進行爬取。

?

要獲取的內容分別為 paths 標簽下的

1./quota/開頭的路徑

2. get 這樣的httpmode

3 description對應的描述

4 summary

5 tags 里存放著的服務名

6 服務名所對應的副描述(不在paths標簽下)

7總的title(只有一個

?

import requests import json import pymysql import urllib from urllib.request import urlopen from bs4 import BeautifulSoup import reurl = 'http://192.168.101.213:7027/v2/api-docs' headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36)"} response = requests.get(url,headers=headers).text json_str = json.loads(response)#轉化為字符串 aa1 = json_str['info']['title']#大title aa2=[] aa2.append(aa1) aa=aa2*42 cc1 = json_str['paths']#提取在整個paths標簽下的數據 cc = [] #微服務路徑 dd = [] #獲取方式 for key in cc1.keys():#獲得字典的key值 cc.append(key) for m in cc:dd1 = json_str['paths'][m]#在cc這個list中進行遍歷for key2 in dd1.keys():dd.append(key2) ee = []#擴展操作 bb = []#微服務名 gg = []#微服務描述 ff = []#主描述 for o,p in zip(cc,dd):#同時遍歷 cc和dd兩個list 用zip對他們進行封裝try:ee1 = json_str['paths'][o][p]['summary']bb1 = json_str['paths'][o][p]['tags'][0]gg1 = json_str['paths'][o][p]['description']bb.append(bb1)ee.append(ee1)gg.append(gg1)except(KeyError):#因為有兩個要爬取的內容是沒有 description這個key值的 所以遇到keyerror時繼續爬取continue hh=[]#這一部爬取6個微服務名 因為相應description沒有被一起存放在paths標簽下面 for n1 in range (0,6):hh1 = json_str['tags'][n1]['name']hh.append(hh1)list3 = [hh.index(num) for num in bb]#將存放在bb中的微服務名拿到hh中進行對比 獲得他們的num值存放在list3中 for n3 in list3:ff1 = json_str['tags'][n3]['description']#遍歷list3獲得他們想對應的description值 ff.append(ff1) db = pymysql.connect('localhost', 'root', '******', 'languid') cursor = db.cursor() for z, x, c, v, z1, x1 ,c1 in zip(aa,bb,gg,cc,dd,ff,ee):#遍歷6個列表并對他們封裝sql = """insert into swaggerui(Platform,Microservice,Microservicedescrption,MicroPaths,Httpmode,Microdescrption,MicroNotes)VALUES ('%s','%s','%s','%s','%s','%s','%s')"""%(z, x, c, v, z1, x1, c1)cursor.execute(sql)db.commit()

?

1).主要在于標簽名字的獲取,因為想要獲取的內容被存放在了標簽名。

for key in cc1.keys():cc.append(key) 

所以要用for循環來獲得字典的鍵值。

?

相當于

dic ={"name"="香蕉","種類"=“水果","sales”=“1000”} for ke in dic.keys():print(ke)name 種類 sales也可以用items()的方法 for ke in dic.items():print(ke[0])name 種類 sales

?

2).用一個if語句 對比兩個list取得需要的值

list1=[a,b,c,d,e,a,c,e,a,b,d,c,a,e,b,c,e,a,d,e](順序是隨機的但是都是a-e) list2=[a,b,c,d,e]

?

用索引的方法我們可以獲得對應的num值

?

list3 = [list2.index(num) for num in list1][0, 1, 2, 3, 4, 0, 2, 4, 0, 1, 3, 2, 0, 4, 1, 2, 4, 0, 3, 4]

再用一個for循環遍歷list3就可以獲得相應的數據了。

?

?

?

最后存放到mysql里面的顯示是這樣的

?

轉載于:https://www.cnblogs.com/languid/p/10790207.html

總結

以上是生活随笔為你收集整理的爬取json Swaggerui界面的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。