
如何快速實(shí)現(xiàn)REST API集成以優(yōu)化業(yè)務(wù)流程
通過(guò)百度搜索IP地址,可以看到這樣一個(gè)小工具:
通過(guò)輸入IP地址,點(diǎn)擊查詢可以獲得到地址信息。通過(guò)抓包可以獲得API:
https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=113.57.215.184&co=&resource_id=6006&t=1559922221313&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=baidu&cb=jQuery110205516131051897397_1559921486295&_=1559921486372
結(jié)果如下:
對(duì)地址進(jìn)行簡(jiǎn)化:
https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=113.57.215.184&resource_id=6006&format=json
簡(jiǎn)化后結(jié)果成為Json形式:
編寫(xiě)Python代碼實(shí)現(xiàn):
import urllib.request
import ssl
import json
ssl._create_default_https_context = ssl._create_unverified_context
location_temp = json.loads(urllib.request.urlopen(
"https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=113.57.215.184&resource_id=6006&format=json").read().decode(
"gbk"))["data"][0]["location"]
location = location_temp.split(" ")[0] if " " in location_temp else location_temp
print(location)
運(yùn)行結(jié)果:
通過(guò)百度搜索天氣,可以獲得到天氣信息:
通過(guò)對(duì)頁(yè)面分析,我們可以看到天氣信息在網(wǎng)頁(yè)源碼中可以提現(xiàn):
也就是說(shuō),我們可以通過(guò)簡(jiǎn)單的頁(yè)面分析,就能獲得到天氣數(shù)據(jù):
import urllib.request
import urllib.parse
url = "http://www.baidu.com/s?wd=" + urllib.parse.quote("湖北省武漢市天氣")
page_source = urllib.request.urlopen(url).read().decode("utf-8").replace("\n", "").replace("\r", "")
weather = page_source.split('<p class="op_weather4_twoicon_weath"')[1].split('title="">')[1].split('</p>')[0].strip()
temp = page_source.split('<p class="op_weather4_twoicon_temp">')[1].split('</p>')[0].strip()
print(weather,temp)
運(yùn)行結(jié)果:
新建云函數(shù):
保存之后,在測(cè)試的時(shí)候,選擇API網(wǎng)關(guān)作為觸發(fā)器,進(jìn)行測(cè)試:
測(cè)試之后,可以看到結(jié)果,便于我們對(duì)起進(jìn)行基本分析:
經(jīng)過(guò)分析可以看到Event中有:
可以獲得這個(gè)IP地址:
# -*- coding: utf8 -*-
import json
def main_handler(event, context):
print(event["requestContext"]["sourceIp"])
運(yùn)行結(jié)果:
# -*- coding: utf8 -*-
import json, ssl
import urllib.request
import urllib.parse
ssl._create_default_https_context = ssl._create_unverified_context
def get_loaction(ip):
location_temp = json.loads(urllib.request.urlopen("https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=" + ip + "&resource_id=6006&format=json").read().decode("gbk"))["data"][0]["location"]
return location_temp.split(" ")[0] if " " in location_temp else location_temp
def get_weather(address):
url = "http://www.baidu.com/s?wd=" + urllib.parse.quote(address + "天氣")
page_source = urllib.request.urlopen(url).read().decode("utf-8").replace("\n", "").replace("\r", "")
weather = page_source.split('<p class="op_weather4_twoicon_weath"')[1].split('title="">')[1].split('</p>')[0].strip()
temp = page_source.split('<p class="op_weather4_twoicon_temp">')[1].split('</p>')[0].strip()
return {"weather": weather, "temp": temp}
def main_handler(event, context):
return get_weather(get_loaction(event["requestContext"]["sourceIp"]))
測(cè)試結(jié)果:
選擇API網(wǎng)關(guān):
在與云函數(shù)相同區(qū)域,建立:
保存之后會(huì)提示我們進(jìn)行API配置:
點(diǎn)擊新建:
因?yàn)楸疚膬H是做一個(gè)簡(jiǎn)單的Demo,所以在此處,我就進(jìn)行簡(jiǎn)單配置,例如鑒權(quán)等都選擇了免鑒權(quán),但是在實(shí)際中,我還是推薦大家,進(jìn)行鑒權(quán),這樣更安全,也避免資源被盜用等,除此之外,其他各個(gè)參數(shù)都需要根據(jù)自己需求而定,本文僅是拋磚引玉:
配置完成之后,發(fā)布測(cè)試環(huán)境進(jìn)行測(cè)試:
\
測(cè)試發(fā)布完成之后,我們通過(guò)瀏覽器進(jìn)行一下簡(jiǎn)單測(cè)試:
復(fù)制地址,并添加我們之前的路徑:
至此,我們完成了一個(gè)API網(wǎng)關(guān)與SCF結(jié)合的小例子。
云函數(shù)是一個(gè)函數(shù)級(jí)別的應(yīng)用,我們可以將它應(yīng)用在很多領(lǐng)域,例如做Web開(kāi)發(fā)、IOT等,但是云函數(shù)本身自己很難完成一個(gè)功能,需要和周邊的產(chǎn)品配合,本文主要介紹與API網(wǎng)關(guān)結(jié)合做一個(gè)獲取天氣的HTTP接口。其實(shí)仔細(xì)想一下,我們是不是可以通過(guò)SCF與API網(wǎng)關(guān)結(jié)合,實(shí)現(xiàn)一個(gè)Web后端呢?以一個(gè)博客系統(tǒng)為例:前段使用Vue.js等框架進(jìn)行開(kāi)發(fā),所有的后端邏輯,包括數(shù)據(jù)庫(kù)的增刪改查,包括某些小功能點(diǎn)的實(shí)現(xiàn),全部用云函數(shù)來(lái)實(shí)現(xiàn)?這樣,只需要找一個(gè)虛擬空間或者騰訊云的COS,就可以完成前端的部署,而后端的服務(wù)器配置、面對(duì)用戶激增的服務(wù)器運(yùn)維等,都交給云函數(shù)+相關(guān)產(chǎn)品來(lái)實(shí)現(xiàn),這樣會(huì)大大節(jié)約資源,降低成本。總結(jié)來(lái)說(shuō),合理利用云函數(shù),不僅可以節(jié)省項(xiàng)目搭建時(shí)間,還能節(jié)約資源、降低成本、提高效率。
文章轉(zhuǎn)自微信公眾號(hào)@騰訊云云函數(shù)
對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力
一鍵對(duì)比試用API 限時(shí)免費(fèi)