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

今天的分享到此结束。

相关推荐
电商API&Tina1 分钟前
比价 / 选品专用:京东 + 淘宝 核心接口实战(可直接复制运行)
大数据·数据库·人工智能·python·json·音视频
左左右右左右摇晃7 分钟前
ConcurrentHashMap ——put + get
java·开发语言·笔记
love530love13 分钟前
Windows 开源项目部署评估与决策清单(完整版)
人工智能·windows·python·开源·github
今夕资源网16 分钟前
零基础 Python 环境搭建工具 一键安装 Python 环境自动配置 升级 pip、setuptools、wheel
开发语言·python·pip·环境变量·python环境变量·python自动安装
小CC吃豆子26 分钟前
C++ 继承
开发语言·c++
Derrick__128 分钟前
Scrapling 爬取豆瓣电影Top250
开发语言·python·网络爬虫·豆瓣·scrapling
serve the people29 分钟前
ACME 协议流程与AllinSSL 的关系(一)
开发语言
2401_8357925429 分钟前
Java复习上
java·开发语言·python
小昭在路上……31 分钟前
编译与链接的本质:段(Section)的生成与定位
java·linux·开发语言
Ai财富密码1 小时前
AI生成大屏可视化:数据智能驱动下的高维洞察与决策中枢
开发语言·人工智能·python·sdd