
如何快速實現REST API集成以優化業務流程
通過百度搜索IP地址,可以看到這樣一個小工具:
通過輸入IP地址,點擊查詢可以獲得到地址信息。通過抓包可以獲得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
結果如下:
對地址進行簡化:
https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=113.57.215.184&resource_id=6006&format=json
簡化后結果成為Json形式:
編寫Python代碼實現:
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)
運行結果:
通過百度搜索天氣,可以獲得到天氣信息:
通過對頁面分析,我們可以看到天氣信息在網頁源碼中可以提現:
也就是說,我們可以通過簡單的頁面分析,就能獲得到天氣數據:
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)
運行結果:
新建云函數:
保存之后,在測試的時候,選擇API網關作為觸發器,進行測試:
測試之后,可以看到結果,便于我們對起進行基本分析:
經過分析可以看到Event中有:
可以獲得這個IP地址:
# -*- coding: utf8 -*-
import json
def main_handler(event, context):
print(event["requestContext"]["sourceIp"])
運行結果:
# -*- 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"]))
測試結果:
選擇API網關:
在與云函數相同區域,建立:
保存之后會提示我們進行API配置:
點擊新建:
因為本文僅是做一個簡單的Demo,所以在此處,我就進行簡單配置,例如鑒權等都選擇了免鑒權,但是在實際中,我還是推薦大家,進行鑒權,這樣更安全,也避免資源被盜用等,除此之外,其他各個參數都需要根據自己需求而定,本文僅是拋磚引玉:
配置完成之后,發布測試環境進行測試:
\
測試發布完成之后,我們通過瀏覽器進行一下簡單測試:
復制地址,并添加我們之前的路徑:
至此,我們完成了一個API網關與SCF結合的小例子。
云函數是一個函數級別的應用,我們可以將它應用在很多領域,例如做Web開發、IOT等,但是云函數本身自己很難完成一個功能,需要和周邊的產品配合,本文主要介紹與API網關結合做一個獲取天氣的HTTP接口。其實仔細想一下,我們是不是可以通過SCF與API網關結合,實現一個Web后端呢?以一個博客系統為例:前段使用Vue.js等框架進行開發,所有的后端邏輯,包括數據庫的增刪改查,包括某些小功能點的實現,全部用云函數來實現?這樣,只需要找一個虛擬空間或者騰訊云的COS,就可以完成前端的部署,而后端的服務器配置、面對用戶激增的服務器運維等,都交給云函數+相關產品來實現,這樣會大大節約資源,降低成本??偨Y來說,合理利用云函數,不僅可以節省項目搭建時間,還能節約資源、降低成本、提高效率。
文章轉自微信公眾號@騰訊云云函數