一、基礎爬蟲代碼:
# 導包
import requests
# 從指定網址爬取數據
response = requests.get("http://192.168.34.57:8080")
print(response)
# 獲取數據
print(response.text)
二、使用FastAPI快速搭建網站:
# TODO FastAPI 是一個現代化、快速(高性能)的 Web 框架(可打開其官網具體了解),用于使用基于標準 Python 類型提示的 Python 構建 API。
from fastapi import FastAPI # 負責接收請求,響應資源
from fastapi.responses import Response
import uvicorn # 負責綁定主機端口號啟動異步服務器# 創建 FastAPI 實例
app = FastAPI()# 定義根路徑的 GET 請求接口
@app.get("/")
def read_root():with open("sources/index.html","rb") as f:body = f.read()return Response(body)@app.get("/bar_base.html")
def read_root():with open("sources/bar_base.html", "rb") as f:body = f.read()return Response(body)@app.get("/line_base.html")
def read_root():with open("sources/line_base.html", "rb") as f:body = f.read()return Response(body)@app.get("/pie_base.html")
def read_root():with open("sources/pie_base.html", "rb") as f:body = f.read()return Response(body)@app.get("/favicon.ico")
def read_root():with open("sources/favicon.ico", "rb") as f:body = f.read()return Response(body)@app.get("/hm.jpg")
def read_root():with open("sources/hm.jpg", "rb") as f:body = f.read()return Response(body)# 啟動服務(需要通過 uvicorn 命令運行)
if __name__ == "__main__":uvicorn.run(app, host="192.168.34.57", port=8080)
以上代碼配合寫好的html文件,便可以搭建一個簡易的網站,后期隨時可以根據自己的需求更改、添加(文字、圖片、視頻等)。
今天的分享到此結束。