python 利用toapi庫(kù)自動(dòng)生成api
在學(xué)習(xí)做接口測(cè)試自動(dòng)化的時(shí)候,我們往往會(huì)自己動(dòng)手寫(xiě)一些簡(jiǎn)單的API,比如寫(xiě)一個(gè)簡(jiǎn)單的TODO API之類(lèi)。
不過(guò)自己寫(xiě)API的時(shí)候經(jīng)常需要造一些假數(shù)據(jù),以及處理分頁(yè)邏輯,開(kāi)始的時(shí)候還覺(jué)得比較有意思,但久而久之就顯得比較乏味了。
這時(shí)候你可能會(huì)想,有沒(méi)有什么工具可以自動(dòng)將一個(gè)線上的網(wǎng)站轉(zhuǎn)化成簡(jiǎn)單的API呢?
這樣的工具確實(shí)是存在的,而且不少,其中python語(yǔ)言中比較受歡迎的實(shí)現(xiàn)是https://github.com/gaojiuli/toapi項(xiàng)目,項(xiàng)目名稱(chēng)是toapi。
我們來(lái)簡(jiǎn)單體驗(yàn)一下這個(gè)庫(kù)。
安裝
首先安裝。
pip install toapi pip install cssselect
將重定向科技的課程列表頁(yè)轉(zhuǎn)化成API
http://www.itest.info/courses,這是重定向科技的課程列表頁(yè)面,里面包含了目前我們所開(kāi)設(shè)的全部課程。
現(xiàn)在我們將這個(gè)頁(yè)面轉(zhuǎn)化成API,這個(gè)API 返回每門(mén)課程的名稱(chēng)以及url。
from flask import request
from htmlparsing import Attr, Text
from toapi import Api, Item
api = Api()
@api.site('http://www.itest.info')
@api.list('.col-md-3')
@api.route('/courses?page={page}', '/courses')
@api.route('/courses', '/courses')
class Course(Item):
url = Attr('a', 'href')
title = Text('h4')
api.run(debug=True, host='0.0.0.0', port=12306)
運(yùn)行
python app.py
查看結(jié)果
curl localhost:12306/courses
返回結(jié)果
{
"Course": [
{
"title": "全棧測(cè)試開(kāi)發(fā)班",
"url": "/courses/9"
},
{
"title": "性能測(cè)試從入門(mén)到精通班",
"url": "/courses/7"
},
{
"title": "接口自動(dòng)化測(cè)試開(kāi)發(fā)--Python班",
"url": "/courses/6"
},
{
"title": "Selenium自動(dòng)化測(cè)試--Python班",
"url": "/courses/2"
}
]
}
官方例子
將hacknews網(wǎng)站轉(zhuǎn)成API
from flask import request
from htmlparsing import Attr, Text
from toapi import Api, Item
api = Api()
@api.site('https://news.ycombinator.com')
@api.list('.athing')
@api.route('/posts?page={page}', '/news?p={page}')
@api.route('/posts', '/news?p=1')
class Post(Item):
url = Attr('.storylink', 'href')
title = Text('.storylink')
@api.site('https://news.ycombinator.com')
@api.route('/posts?page={page}', '/news?p={page}')
@api.route('/posts', '/news?p=1')
class Page(Item):
next_page = Attr('.morelink', 'href')
def clean_next_page(self, value):
return api.convert_string('/' + value, '/news?p={page}', request.host_url.strip('/') + '/posts?page={page}')
api.run(debug=True, host='0.0.0.0', port=5000)
結(jié)果
{
"Page": {
"next_page": "http://127.0.0.1:5000/posts?page=2"
},
"Post": [
{
"title": "Mathematicians Crack the Cursed Curve",
"url": "https://www.quantamagazine.org/mathematicians-crack-the-cursed-curve-20171207/"
},
{
"title": "Stuffing a Tesla Drivetrain into a 1981 Honda Accord",
"url": "https://jalopnik.com/this-glorious-madman-stuffed-a-p85-tesla-drivetrain-int-1823461909"
}
]
}
總結(jié)
- toapi使用非常簡(jiǎn)單,實(shí)際上就是把a(bǔ)pi的創(chuàng)建和爬蟲(chóng)結(jié)合起來(lái)了
- toapi提供了比較完備的緩存機(jī)制,非首次訪問(wèn)的速度會(huì)很快
有一定爬蟲(chóng)能力的測(cè)試同學(xué)可以用toapi來(lái)實(shí)現(xiàn)簡(jiǎn)單的mock server,但僅限于get接口
以上就是python 利用toapi庫(kù)自動(dòng)生成api的詳細(xì)內(nèi)容,更多關(guān)于python toapi庫(kù)自動(dòng)生成api的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Windows下的Python 3.6.1的下載與安裝圖文詳解(適合32位和64位)
這篇文章主要介紹了Windows下的Python 3.6.1的下載與安裝圖文詳解(適合32位和64位),需要的朋友可以參考下2018-02-02
python操作ssh實(shí)現(xiàn)服務(wù)器日志下載的方法
這篇文章主要介紹了python操作ssh實(shí)現(xiàn)服務(wù)器日志下載的方法,涉及Python建立ssh連接并下載服務(wù)器日志的相關(guān)技巧,需要的朋友可以參考下2015-06-06
詳解python中DRF框架的數(shù)據(jù)校驗(yàn)方式
這篇文章主要為大家詳細(xì)介紹了python中DRF框架的數(shù)據(jù)校驗(yàn)方式,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下2023-10-10
使用PyTorch將數(shù)據(jù)從CPU移動(dòng)到GPU的四個(gè)方法
這篇文章給大家介紹了在 PyTorch 中,將數(shù)據(jù)從 CPU 移動(dòng)到 GPU 的幾種方法,使用 .to() 方法,使用 .cuda() 方法,使用 torch.Tensor 構(gòu)造函數(shù)和使用 torch.tensor 構(gòu)造函數(shù)這四個(gè)方法,通過(guò)代碼示例介紹非常詳細(xì),需要的朋友可以參考下2024-01-01
python3多線程中使用線程睡眠的方法實(shí)現(xiàn)
線程睡眠是一個(gè)常見(jiàn)且有用的工具,用于控制線程的執(zhí)行順序和速度,本文主要介紹了python3多線程中使用線程睡眠的方法實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-08-08

