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

今天的分享到此结束。

相关推荐
数据智能老司机2 小时前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机3 小时前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i4 小时前
django中的FBV 和 CBV
python·django
c8i4 小时前
python中的闭包和装饰器
python
这里有鱼汤8 小时前
小白必看:QMT里的miniQMT入门教程
后端·python
TF男孩18 小时前
ARQ:一款低成本的消息队列,实现每秒万级吞吐
后端·python·消息队列
该用户已不存在1 天前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?
后端·python·rust
站大爷IP1 天前
Java调用Python的5种实用方案:从简单到进阶的全场景解析
python
用户8356290780511 天前
从手动编辑到代码生成:Python 助你高效创建 Word 文档
后端·python
侃侃_天下1 天前
最终的信号类
开发语言·c++·算法