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

今天的分享到此结束。

相关推荐
Msshu1232 分钟前
消费类,小家电产品如何做Type-C PD快充快速充电
python·单片机·嵌入式硬件·物联网·beautifulsoup·fastapi·tornado
emplace_back15 分钟前
C# 项目
开发语言·c#
lkx0978818 分钟前
第八天的尝试
python
(・Д・)ノ20 分钟前
python打卡day33
开发语言·python
小革36033 分钟前
JAVA批量发送邮件(含excel内容)
开发语言·python·excel
阿梦Anmory34 分钟前
[解决conda创建新的虚拟环境没用python的问题]
开发语言·python·conda
L_cl35 分钟前
【NLP 77、Python环境管理工具之conda】
python·自然语言处理·conda
心扬42 分钟前
python容器
开发语言·python
我漫长的孤独流浪2 小时前
STL中的Vector(顺序表)
开发语言·c++·算法
一刀到底2112 小时前
java 在用redis 的时候,如何合理的处理分页问题? redis应当如何存储性能最佳
java·开发语言·redis