python:基础爬虫、搭建简易网站

一、基础爬虫代码:

复制代码
# 导包
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文件,便可以搭建一个简易的网站,后期随时可以根据自己的需求更改、添加(文字、图片、视频等)。

今天的分享到此结束。

相关推荐
番石榴AI1 分钟前
自己动手做一款ChatExcel数据分析系统,智能分析 Excel 数据
人工智能·python·数据挖掘·excel
纵有疾風起11 分钟前
C++模版:模板初阶及STL简介
开发语言·c++·经验分享·开源
QT 小鲜肉16 分钟前
【个人成长笔记】Qt Creator快捷键终极指南:从入门到精通
开发语言·c++·笔记·qt·学习·学习方法
星期天要睡觉16 分钟前
深度学习——循环神经网络(RNN)
人工智能·python·rnn·深度学习·神经网络
子豪-中国机器人18 分钟前
《C++ STL 基础入门》教案
java·开发语言
消失的旧时光-194328 分钟前
ScheduledExecutorService
android·java·开发语言
勇闯逆流河28 分钟前
【C++】用红黑树封装map与set
java·开发语言·数据结构·c++
山,离天三尺三29 分钟前
深度拷贝详解
开发语言·c++·算法
Blossom.11837 分钟前
把AI“撒”进农田:基于极值量化与状态机的1KB边缘灌溉决策树
人工智能·python·深度学习·算法·目标检测·决策树·机器学习
Red Car37 分钟前
如何向文件夹内所有PDF增加水印
python·pdf