一、依赖

virtualenv -p python3.6 xx

pip install scrapy

pip install pymysql

二、

1、创建项目和spider1

scrapy startproject scraw_swagger

scrapy genspider spider1  xxx.com  (执行之后在项目的spiders目录下会生成一个spider1.py的文件)

以下代码主要实现了将swagger的第一级目录爬下来存在一个叫:interfaces_path的文件下

# -*- coding: utf-8 -*-
import scrapy
import json
from scraw_swagger import settings class Spider1Spider(scrapy.Spider): name = 'spider1'
allowed_domains = ['xxx.com'']
scrawl_domain = settings.interface_domain+'/api-docs'
start_urls = [scrawl_domain] def parse(self, response):
# 调试代码
# filename = 'mid_link'
# open(filename, 'wb').write(response.body)
# /////////
response = response.body
response_dict = json.loads(response)
apis = response_dict['apis']
n = len(apis)
temppath = []
i = 0 domain = settings.interface_domain+'/api-docs'
filename = 'interfaces_path'
file = open(filename, 'w')
for i in range(0, n):
subapi = apis[i]
path = subapi['path']
path = ','+domain + path
temppath.append(path)
file.write(path)

2、创建spider2

scrapy genspider spider2 xxx.com  (执行之后在项目的spiders目录下会生成一个spider2.py的文件)

以下代码主要实现了获取interfaces_path的文件下的地址对应的内容

# # -*- coding: utf-8 -*-
import scrapy
from scraw_swagger.items import ScrawSwaggerItem
import json
from scraw_swagger import settings class Spider2Spider(scrapy.Spider):
name = 'spider2'
allowed_domains = ['xxx.com']
file = open('interfaces_path', 'r')
file = file.read()
list_files = []
files = file.split(',')
n = len(files)
for i in range(1, n):
file = files[i]
list_files.append(file)
start_urls = list_files def parse(self, response):
outitem = ScrawSwaggerItem()
out_interface = []
out_domain = []
out_method = []
out_param_name = []
out_data_type = []
out_param_required = []
# 调试代码
# filename = response.url.split("/")[-1]
# open('temp/'+filename, 'wb').write(response.body)
# ///////
response = response.body
response_dict = json.loads(response)
items = response_dict['apis']
items_len = len(items)
for j in range(0, items_len):
path = items[j]['path']
# interface组成list
operations = items[j]['operations'][0]
method = operations['method']
parameters = operations['parameters']
parameters_len = len(parameters)
param_name = []
param_required = []
data_type = []
for i in range(0, parameters_len):
name = parameters[i]['name']
param_name.append(name)
required = parameters[i]['required']
param_required.append(required)
type = parameters[i]['type']
data_type.append(type) out_interface.append(path)
interface_domain = settings.interface_domain
out_domain.append(interface_domain)
out_method.append(method)
out_data_type.append(data_type)
out_param_name.append(param_name)
out_param_required.append(param_required) outitem['interface'] = out_interface
outitem['domain'] = out_domain
outitem['method'] = out_method
outitem['param_name'] = out_param_name
outitem['param_required'] = out_param_required
outitem['data_type'] = out_data_type
yield outitem

3、settings.py文件

# -*- coding: utf-8 -*-

# Scrapy settings for scraw_swagger project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
# https://doc.scrapy.org/en/latest/topics/settings.html
# https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
# https://doc.scrapy.org/en/latest/topics/spider-middleware.html interface_domain = 'test'
token = 'test' # 调试代码
# interface_domain = 'http://xxxx..net'
# token = 'xxxxxx'
# /////////////// BOT_NAME = 'scraw_swagger' SPIDER_MODULES = ['scraw_swagger.spiders']
NEWSPIDER_MODULE = 'scraw_swagger.spiders' FEED_EXPORT_ENCODING = 'utf-8' # Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'scraw_swagger (+http://www.yourdomain.com)' # Obey robots.txt rules
ROBOTSTXT_OBEY = True # Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32 # Configure a delay for requests for the same website (default: 0)
# See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
#DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16 # Disable cookies (enabled by default)
#COOKIES_ENABLED = False # Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False # Override the default request headers:
#DEFAULT_REQUEST_HEADERS = {
# 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
# 'Accept-Language': 'en',
#} # Enable or disable spider middlewares
# See https://doc.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
# 'scraw_swagger.middlewares.ScrawSwaggerSpiderMiddleware': 543,
#} # Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
# 'scraw_swagger.middlewares.ScrawSwaggerDownloaderMiddleware': 543,
#} # Enable or disable extensions
# See https://doc.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
# 'scrapy.extensions.telnet.TelnetConsole': None,
#} # Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
'scraw_swagger.pipelines.ScrawSwaggerPipeline': 300,
# 'scraw_swagger.pipelines.MysqlTwistedPipline': 200,
} # Enable and configure the AutoThrottle extension (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False # Enable and configure HTTP caching (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage' MYSQL_HOST = ''
MYSQL_DBNAME = ''
MYSQL_USER = ''
MYSQL_PASSWD = ''
MYSQL_PORT = 3306

4、存入数据库。编写pipelines.py文件。提取返回的item,并将对应的字段存入数据库

# -*- coding: utf-8 -*-
import pymysql
from scraw_swagger import settings
from twisted.enterprise import adbapi
import pymysql.cursors
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html class ScrawSwaggerPipeline(object):
def process_item(self, item, spider):
try:
# 插入数据sql
sql = """
insert into interfaces (domain, interface, method, param_name ,data_type, param_required)
VALUES (%s, %s, %s, %s, %s, %s)
"""
domain = item['domain']
n = len(domain)
for i in range(0, n):
domain = str(item['domain'][i])
interface = str(item["interface"][i])
method = str(item["method"][i])
param_name = str(item["param_name"][i])
data_type = str(item["data_type"][i])
param_required = str(item["param_required"][i])
a = (domain, interface, method, param_name, data_type, param_required)
self.cursor.execute(sql, a)
self.connect.commit()
except Exception as error:
# 出现错误时打印错误日志
print(error)
# self.connect.close()
return item def __init__(self):
# 连接数据库
self.connect = pymysql.connect(
host=settings.MYSQL_HOST,
db=settings.MYSQL_DBNAME,
user=settings.MYSQL_USER,
passwd=settings.MYSQL_PASSWD,
port=settings.MYSQL_PORT,
charset='utf8',
use_unicode=True) # 通过cursor执行增删查改
self.cursor = self.connect.cursor()

swagger上的接口写入数据库的更多相关文章

  1. 开机后将sim/uim卡上的联系人写入数据库

    tyle="margin:20px 0px 0px; font-size:14px; line-height:26px; font-family:Arial; color:rgb(51,51 ...

  2. 通过POI实现上传EXCEL的批量读取数据写入数据库

    最近公司新增功能要求导入excel,并读取其中数据批量写入数据库.于是就开始了这个事情,之前的文章,记录了上传文件,本篇记录如何通过POI读取excel数据并封装为对象上传. 上代码: 1.首先这是一 ...

  3. c#上传文件并将word pdf转化成txt存储并将内容写入数据库

    c#上传文件并将word pdf转化成txt存储并将内容写入数据库 using System; using System.Data; using System.Configuration; using ...

  4. Django上传excel表格并将数据写入数据库

    前言: 最近公司领导要统计技术部门在各个业务条线花费的工时百分比,而 jira 当前的 Tempo 插件只能统计个人工时.于是就写了个报表工具,将 jira 中导出的个人工时excel表格 导入数据库 ...

  5. c#WebApi使用form表单提交excel,实现批量写入数据库

    思路:用户点击下载模板按钮,获取到excel模板,然后向里面填写数据保存.from表单提交的时候选择保存好的excel,实现数据的批量导入过程 先把模板放在服务器的项目目录下面:如 模板我一般放在:F ...

  6. 使用log4j让日志写入数据库

    之前做的一个项目有这么个要求,在日志管理系统里,需要将某些日志信息存储到数据库里,供用户.管理员查看分析.因此我就花了点时间搞了一下这一功能,各位请看. 摘要:我们知道log4j能提供强大的可配置的记 ...

  7. Excel 导入到Datatable 中,再使用常规方法写入数据库

    首先呢?要看你的电脑的office版本,我的是office 2013 .为了使用oledb程序,需要安装一个引擎.名字为AccessDatabaseEngine.exe.这里不过多介绍了哦.它的数据库 ...

  8. SQLBulkCopy使用实例--读取Excel写入数据库/将 Excel 文件转成 DataTable

    MS SQL Server 提供一个称为 bcp 的流行的命令提示符实用工具,用于将数据从一个表移动到另一个表(表可以在不同服务器上). SqlBulkCopy 类允许编写提供类似功能的托管代码解决方 ...

  9. thinkphp + 美图秀秀api 实现图片裁切上传,带数据库

    思路: 1.数据库 创建test2 创建表img,字段id,url,addtime 2.前台页: 1>我用的是bootstrap 引入必要的js,css 2>引入美图秀秀的js 3.后台: ...

随机推荐

  1. Dynamics CRM产生公共签名,避免每次插件换环境重新输入签名密钥账号密码

    在Dynamcs CRM项目维护交接过程中,我们经常会使用其他合作者的插件代码.但是每次拿到别人代码编译的时候插件密钥都要重新输入密钥的账号密码.而且如果密钥都是的话比较麻烦.所以这里就针对这个问题做 ...

  2. 翻译:《实用的Python编程》09_02_Third_party

    目录 | 上一节 (9.1 包) | 下一节 (9.3 版本分发) 9.2 第三方模块 Python 拥有一个包含各种内置模块的大型库(自带电池(batteries included))(译注:&qu ...

  3. java POI(二)

    name.xslx 1 public class Demo6 { 2 3 public static void main(String[] args) throws IOException { 4 I ...

  4. Day17_105_IO_BufferWriter带缓冲区的字符输出流

    BufferWriter带缓冲区的字符输出流 * OutputStreamWriter(); 转换流,可以将文件字节输出流转换为文件字符输出流 * 代码: import java.io.*; publ ...

  5. java面试一日一题:mysql中的自增主键

    问题:请讲下mysql中的自增主键 分析:该问题主要考察对mysql中自增主键的掌握,使用场景及如何设置 回答要点: 主要从以下几点去考虑 1.什么自增主键 2.使用场景是什么: 3.innodb_a ...

  6. CentOS7用yum安装软件提示 cannot find a valid baseurl for repobase7x86_64

    解决办法[亲测有效] 1.打开 vi /etc/sysconfig/network-scripts/ifcfg-enp4s0(每个机子都可能不一样,但格式会是"ifcfg-e..." ...

  7. 【yml】springboot 配置类 yml语法

    参考:https://www.runoob.com/w3cnote/yaml-intro.html YAML 是 "YAML Ain't a Markup Language"(YA ...

  8. 阿里早期Android加固代码的实现分析

    本文博客地址:http://blog.csdn.net/qq1084283172/article/details/78320445 看雪上有作者(寒号鸟二代)将阿里移动早期的Android加固进行了逆 ...

  9. LA3415保守的老师

    题意:       有n个学生,老师要带他们出去玩,但是老师比较保守,怕他们之间萌生爱意,所以带出去的所有同学必须至少满足四个条件中的一组,问最多能带多少人出去玩. 思路:        比较简单二分 ...

  10. Average Score39届亚洲赛牡丹江站A题

    题意:       A班有n个人,B班有m个人,然后现在给你n-1个A班人的成绩,和m个B班人的成绩,然后题目要求求出A班剩下的没给成绩那个人的成绩范围,要求是这个人从A班转到B班后能使A,B的平均分 ...