from bs4 import BeautifulSoup
import re
import os.path
import itertools name='newcrm'
source_file_path='./'+name+'.html' def get_apiInfo():
with open(source_file_path,encoding='utf-8') as api_file:
fileInfo=api_file.read()
soup = BeautifulSoup(fileInfo,'lxml') #这里没有装lxml的话,把它去掉用默认的就好
#匹配带有class属性的div标签
divList = soup.find_all('div', attrs={'class': re.compile("api-one")})
# print(len(divList))
apiInfo_list=[]#存放所有的api_list
try:
for alltag in divList:
h3List=alltag.find_all('h3')#在每个div标签下,查找所有的h3标签(包含接口名称)
pList = alltag.find_all('p') #在每个div标签下,查找所有的p标签(包含请求url与请求方法)
tableList=alltag.find_all("table")#匹配div下所有线程的table标签(取出header字段/body字段)
dictInfo={}
api_descride_name_list,api_descride_example_list,api_descride_type_list=[],[],[]
'''所有的接口描述信息:名称/描述/类型'''
query_Param_name_list,query_Param_type_list,query_Param_IsNeed_list=[],[],[]
query_Param_describe_list,query_Param_example_list=[],[]
'''所有的query参数信息:名称/类型/是否必填/描述,主要用于get请求'''
body_Param_name_list,body_Param_type_list,body_Param_IsNeed_list=[],[],[]
body_Param_describe_list,body_Param_example_list=[],[]
'''所有的body参数信息:名称/类型/是否必填/描述,主要用于post/put/delete请求'''
header_Param_name_list,header_Param_type_list,header_Param_IsNeed_list=[],[],[]
header_Param_describe_list,header_Param_example_list=[],[]
'''所有的header参数信息:名称/类型/是否必填/描述'''
# if divList.index(alltag)==len(divList):
if not len(pList)<3:#如果请求方式/请求url为空则不执行后面的程序
api_name=h3List[0].string.strip()#匹配h3标签下的接口名称
api_method=pList[1].string.strip()#这里提取请求方式
api_path=pList[2].string.strip() #这里提取url,并去除空格
dictInfo['api_name']=api_name
dictInfo['api_method']=api_method
dictInfo['api_path']=api_path
header_list=[]#存放header值
body_list=[]#存放body值
query_list=[]#存放query值
api_descride_list=[]#存放所有接口描述信息
# print(api_name)
# print(api_method)
# print(api_path)
IsEmpty_str='Null_Str'#处理匹配为空时的字符串占位符
if not tableList in([],None):
for table in tableList:
table_title=table.tr.th.span.string#匹配table表头
table_thead=table.find_all('thead')#匹配table里的所有thead标签:字段title(描述)
table_tbody=table.find_all('tbody')#匹配字段value
tbody_temp_list1,tbody_temp_list2=[],[]#临时存放接口描述数据
for tbody in table_tbody:
tbody_key_type=tbody.find_all('th')#匹配出字段的类型
tbody_key_field=tbody.find_all('td')#匹配出字段的value
for tbody_name in tbody_key_field :#取出所有字段
if not tbody_name.string is None:
tbody_key=tbody_name.string.strip()
else:
tbody_name=IsEmpty_str#处理为空的情况
tbody_key=tbody_name
#将能够被5整除的字段放入body中(代表的是header/body)
# print(tbody_name)
if len(tbody_key_field)%5==0:
if table_title in('Query参数名'):
'''如果table的名称是Query,则添加到Query列表中'''
query_list.append(tbody_key)
elif table_title in('Body参数名'):
'''如果table的名称是body,则添加到body列表中'''
body_list.append(tbody_key)
elif table_title in('Header参数名'):
'''如果table的名称是header,则添加到header列表中'''
header_list.append(tbody_key)#将能够被5整除的字段放入body中(代表的是header/body)
elif len(tbody_key_field)%2==0 and table_title ==('参数名'):
tbody_temp_list1.append(tbody_key)#剩下的放入参数名信息中,代表的是参数名描述
# print(header_list)
# print(body_list)
# print(tbody_temp_list1)
if not tbody_key_type==[]:
for tbody_type in tbody_key_type:
if type(tbody_type)!=(str,int):#只处理type为tag的
if not (tbody_type.string is None) :
tbody_field=tbody_type.string.strip()
tbody_temp_list2.append(tbody_field)
else:
tbody_type=IsEmpty_str#处理为空的情况
tbody_temp_list2.append(tbody_type)
# print(tbody_temp_list2)
temp_num=int(len(tbody_temp_list1)/len(tbody_temp_list2))
#计算出两个list的对应关系,这里是2:1,
#需要添加tbody_temp_list1key添加两次后在添加tbody_temp_list2的值
for temp_tuple in enumerate(tbody_temp_list2):
#将tbody_temp_list1/tbody_temp_list2的元素添加至api_descride_list
#'''temp_tuple:是一个元组,第一个参数是index,第二个是value'''
for num in range(temp_num):
#因为tbody_temp_list1/tbody_temp_list2是多对1,
#因为接口描述里的type是单独的th标签里的值,
#而其他的值是td标签里面的值,所以需要将两个列表的元素进行合并
api_descride_list.append(tbody_temp_list1[temp_tuple[0]*(temp_num)+num])
else:
api_descride_list.append(temp_tuple[1])
# print(header_list)
# print(body_list)
# print(api_descride_list)
#
if not header_list in([],None):#处理header列表中字段为空的情况
for n,m in enumerate(header_list):
#删除filed(不存在)的相关元素,这个是apizza导出的bug导致的
#因为列表存在多个相同的值,所以不能使用index方法来获取元素下标,
#使用enumerate来获取list的值与对应的下标
if m in("是","否") :#根据m的值找到对应的下表n
try:
if header_list[(n+3)]==IsEmpty_str:
#如果n后面的第三个元素不是一个字段,而是事先定义的为空的字符串,则删除索引后面的五个元素
for i in range(5):
del header_list[n+3]
except IndexError as error:
pass for header_data in enumerate(header_list):#将列表中的值分别添加至各个列表
if header_data[0]==0 or header_data[0]%5==0 :
header_Param_name_list.append(header_data[1])
elif header_data[0]==1 or header_data[0]%5==1 :
header_Param_type_list.append(header_data[1])
elif header_data[0]==2 or header_data[0]%5==2:
header_Param_IsNeed_list.append(header_data[1])
elif header_data[0]==3 or header_data[0]%5==3:
header_Param_describe_list.append(header_data[1])
elif header_data[0]==4 or header_data[0]%5==4:
header_Param_example_list.append(header_data[1]) if not query_list in([],None):
for n,m in enumerate(query_list):#处理query列表中字段为空的情况
#因为列表可能存在多个相同的值,所以不能使用index方法来获取元素下标,
#使用enumerate来获取list的值与对应的下标
if m in("是","否") :#根据m的值找到对应的下表n
try:
if query_list[(n+3)]==IsEmpty_str:
#如果n后面的第三个元素不是一个字段,而是事先定义的为空的字符串,
#则删除索引后面的五个元素
for i in range(5):
del query_list[n+3]
except IndexError as error:
pass
for query_data in enumerate(query_list):#将列表中的值分别添加至各个列表
if query_data[0]==0 or query_data[0]%5==0 :
query_Param_name_list.append(query_data[1])
elif query_data[0]==1 or query_data[0]%5==1 :
query_Param_type_list.append(query_data[1])
elif query_data[0]==2 or query_data[0]%5==2:
query_Param_IsNeed_list.append(query_data[1])
elif query_data[0]==3 or query_data[0]%5==3:
query_Param_describe_list.append(query_data[1])
elif query_data[0]==4 or query_data[0]%5==4:
query_Param_example_list.append(query_data[1]) if not body_list in([],None):
for n,m in enumerate(body_list):#处理body列表中字段为空的情况
#因为列表可能存在多个相同的值,所以不能使用index方法来获取元素下标,
#使用enumerate来获取list的值与对应的下标
if m in("是","否") :#根据m的值找到对应的下表n
try:
if body_list[(n+3)]==IsEmpty_str:
#如果n后面的第三个元素不是一个字段,而是事先定义的为空的字符串,
#则删除索引后面的五个元素
for i in range(5):
del body_list[n+3]
except IndexError as error:
pass
for body_data in enumerate(body_list):#将列表中的值分别添加至各个列表
if body_data[0]==0 or body_data[0]%5==0 :
body_Param_name_list.append(body_data[1])
elif body_data[0]==1 or body_data[0]%5==1 :
body_Param_type_list.append(body_data[1])
elif body_data[0]==2 or body_data[0]%5==2:
body_Param_IsNeed_list.append(body_data[1])
elif body_data[0]==3 or body_data[0]%5==3:
body_Param_describe_list.append(body_data[1])
elif body_data[0]==4 or body_data[0]%5==4:
body_Param_example_list.append(body_data[1]) if not api_descride_list in([],None):#处理header列表中字段为空的情况
for api_descride_data in enumerate(api_descride_list):#将列表中的值分别添加至各个列表
if api_descride_data[0]==0 or api_descride_data[0]%3==0 :
api_descride_name_list.append(api_descride_data[1])
elif api_descride_data[0]==1 or api_descride_data[0]%3==1 :
api_descride_example_list.append(api_descride_data[1])
elif api_descride_data[0]==2 or api_descride_data[0]%3==2:
api_descride_type_list.append(api_descride_data[1]) if not header_list in(None,[]):
dictInfo['Header']={}
dictInfo['Header']['参数名']=header_Param_name_list
dictInfo['Header']['类型']=header_Param_type_list
dictInfo['Header']['必需']=header_Param_IsNeed_list
dictInfo['Header']['描述']=header_Param_describe_list
dictInfo['Header']['示例']=header_Param_example_list if not query_list in(None,[]):
dictInfo['Query']={}
dictInfo['Query']['参数名']=query_Param_name_list
dictInfo['Query']['类型']=query_Param_type_list
dictInfo['Query']['必需']=query_Param_IsNeed_list
dictInfo['Query']['描述']=query_Param_describe_list
dictInfo['Query']['示例']=query_Param_example_list if not body_list in(None,[]):
dictInfo['Body']={}
dictInfo['Body']['参数名']=body_Param_name_list
dictInfo['Body']['类型']=body_Param_type_list
dictInfo['Body']['必需']=body_Param_IsNeed_list
dictInfo['Body']['描述']=body_Param_describe_list
dictInfo['Body']['示例']=body_Param_example_list if not api_descride_list in(None,[]):
dictInfo['接口说明']={}
dictInfo['接口说明']['参数名']=api_descride_name_list
dictInfo['接口说明']['描述']=api_descride_example_list
dictInfo['接口说明']['类型']=api_descride_type_list # dictInfo['header_list']=header_list
# dictInfo['body_list']=body_list
# dictInfo['api_descride_list']=api_descride_list
apiInfo_list.append(dictInfo) except Exception as error:
# pass
raise(error)
return apiInfo_list
def write_apiInfo_to_file():
source_data=get_apiInfo()#获取数据来源
print(len(source_data))
file_path='./'+name+'-apiInfo.txt'
with open(file_path,'w',encoding='utf-8') as file:
file.write(str(source_data)) write_apiInfo_to_file()

BeautifulSoup模板简单应用-提取html指定数据(api_name/api_method/api_path,请求body/请求header/pagam参数)的更多相关文章

  1. thinkPHP框架 简单的删除和修改数据的做法 和 模板继承的意思大概做法

    BiaodanController.class.php控制器页面 <?php namespace Admin\Controller; use think\Controller; class Bi ...

  2. 爬虫基础库之beautifulsoup的简单使用

    beautifulsoup的简单使用 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: ''' Beautiful Soup提供一些简单的.p ...

  3. sql server编写archive通用模板脚本实现自动分批删除数据

    博主做过比较多项目的archive脚本编写,对于这种删除数据的脚本开发,肯定是一开始的话用最简单的一个delete语句,然后由于部分表数据量比较大啊,索引比较多啊,会发现删除数据很慢而且影响系统的正常 ...

  4. 简单一招实现json数据可视化

    开发一个内部功能时碰到的需求,要把json数据在页面上展示出来,平时浏览器会安装jsonView这样的扩展来看json数据,但是程序要用到的话该怎么办呢?今天在网上搜索的时候,发现了这个小技巧,分享一 ...

  5. Python使用Tabula提取PDF表格数据

    今天遇到一个批量读取pdf文件中表格数据的需求,样式大体是以下这样: python读取PDF无非就是三种方式(我所了解的),pdfminer.pdf2htmlEX 和 Tabula.综合考虑后,选择了 ...

  6. 在AJAX里 使用【 XML 】 返回数据类型 实现简单的下拉菜单数据

    在AJAX里 使用XML返回数据类型 实现简单的下拉菜单数据 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...

  7. 在AJAX里 使用【 JSON 】 返回数据类型 实现简单的下拉菜单数据

    在AJAX里 使用JSON返回数据类型 实现简单的下拉菜单数据 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...

  8. mtail 提取应用日志数据到时序数据库的工具-支持prometheus

    mtail 是谷歌开源的一款很不错的应用日志提取工具,我们可以方便的用来提取应用的数据 到常见的监控系统(prometheus,stats,collectd,gragphite....) 说明: de ...

  9. 【视频编解码·学习笔记】4. H.264的码流封装格式 & 提取NAL有效数据

    一.码流封装格式简单介绍: H.264的语法元素进行编码后,生成的输出数据都封装为NAL Unit进行传递,多个NAL Unit的数据组合在一起形成总的输出码流.对于不同的应用场景,NAL规定了一种通 ...

随机推荐

  1. [转帖]Linux教程(11)- linux中的计划作业

    Linux教程(11)- linux中的计划作业 2018-08-21 17:13:36 钱婷婷 阅读数 160更多 分类专栏: Linux教程与操作 Linux教程与使用   版权声明:本文为博主原 ...

  2. [转帖]PC虚拟化主流:KVM、XEN、OpenVZ详解

    PC虚拟化主流:KVM.XEN.OpenVZ详解 https://zhuanlan.zhihu.com/p/90920566 1.pc虚拟化——KVM KVM是完整的硬件虚拟化,可以在Windows ...

  3. Django框架深入了解_04(DRF之url控制、解析器、响应器、版本控制、分页)

    一.url控制 基本路由写法:最常用 from django.conf.urls import url from django.contrib import admin from app01 impo ...

  4. Logrotate滚动openresty日志

    一.摘要 Linux服务器上我们用Logrotate来分割归档日志文件,结合crond我们可以指定每天在某个时间自动整理日志等文档.本文主要说明了Centos下Logrotate的使用和配置的方法. ...

  5. CF936C Lock Puzzle 构造

    传送门 好久不做构造题脑子都僵化了qwq 无解的条件是\(s\)包含的字符可重集和\(t\)包含的字符可重集不相等,相等的时候下文会给出一种一定可行的构造方案. 考虑增量构造.定义某个字符串\(x\) ...

  6. go 语言学习 ---解析xml

    实例1 //main package main import ( "bytes" "encoding/xml" "fmt" "io ...

  7. Unity的学习笔记(UGUI文本逐个字输出)

    之前在网上找过各种的逐个输出字,我可能理解能力不好,照着代码复制没有能使用成功,于是自己研究了很多网上说的思路,各种改良出了一个能用的,写完自己测试,觉得还真好用,于是记录下来 用法:将用代码组件挂上 ...

  8. java并发编程之原子操作

    先来看一段简单的代码,稍微有点并发知识的都可以知道打印出结果必然是一个小于20000的值 package com.example.test.cas; import java.io.IOExceptio ...

  9. C# 使用代理实现方法过滤

    一.为什么要进行方法过滤 一些情况下我们需要再方法调用前记录方法的调用时间和使用的参数,再调用后需要记录方法的结束时间和返回结果,当方法出现异常的时候,需要记录异常的堆栈和原因,这些都是与业务无关的代 ...

  10. 通俗易懂的join、left join、right join、full join、cross join

    内连接:列出与连接条件匹配的数据行(join\inner join) 外连接:两表合并,如有不相同的列,另外一个表显示null(left join\right join\full outer join ...