从 curl -i 的输出里认东西

👁️ 2 人浏览 💬 0 人评论 ❤️ 添加收藏

curl -i 打出来的响应长这样。先在这里练怎么抠状态码、内容类型和 title:

resp = "HTTP/1.0 200 OK\nServer: BaseHTTP/0.6 Python/3.11.2\nContent-Type: text/html; charset=utf-8\nContent-Length: 233\n\n<!doctype html><html><head><meta charset=\"utf-8\"><title>小站 · 首页</title></head><body>...</body></html>"
head, body = resp.split("\n\n", 1)
lines = head.split("\n")
status = lines[0].split()[1]
ctype = [l for l in lines if l.lower().startswith("content-type:")][0].split(": ", 1)[1]
title = body.split("<title>")[1].split("</title>")[0]
print(status + "/" + ctype.replace(" ", "_") + "/" + title.replace(" ", "_"))
提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论

全部评论