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

今天的分享到此结束。

相关推荐
MYX_3092 分钟前
第四章 多层感知机
开发语言·python
盼哥PyAI实验室3 分钟前
《Python爬虫 + 飞书自动化上传》全流程详细讲解
爬虫·python·飞书
彬彬醤29 分钟前
如何正确选择住宅IP?解析适配跨境、流媒体的网络工具
服务器·开发语言·网络·网络协议·tcp/ip
Yeats_Liao1 小时前
Go Web 编程快速入门 06 - 响应 ResponseWriter:状态码与头部
开发语言·后端·golang
chao1898441 小时前
C#模拟鼠标键盘操作的多种实现方案
开发语言·c#·计算机外设
mit6.8241 小时前
[Agent可视化] 编排工作流(Go) | Temporal引擎 | DAG调度器 | ReAct模式实现
开发语言·后端·golang
Devil枫1 小时前
HarmonyOS鸿蒙应用:仓颉语言与JavaScript核心差异深度解析
开发语言·javascript·ecmascript
时空无限1 小时前
conda 管理 python 版本和虚拟环境
python·conda
惺忪97981 小时前
回调函数的概念
开发语言·前端·javascript
隔壁程序员老王1 小时前
基于 Python 的坦克大战小程序,使用 Pygame 库开发
python·小程序·pygame·1024程序员节