使用curl命令请求百度网站并解析响应

0 次阅读

curl 是 Linux、macOS 以及 Windows 环境中常用的命令行网络工具,可以直接向指定网址发送 HTTP/HTTPS 请求,并将服务器返回的内容输出到终端。对于百度这类公开网站,使用 curl 可以快速验证网络连通性、查看 HTTP 响应、分析响应头以及提取网页正文。

使用 curl 请求百度网站

最简单的请求方式是直接执行:

Bash
curl https://www.baidu.com

执行后,终端通常会输出百度首页返回的 HTML 内容。例如可以看到类似以下结构:

HTML



     charset="utf-8">
    </span><span>百度一下</span><span class="ͼo">


    ...

如果系统提示 curl: command not found,说明当前环境可能没有安装 curl。Linux 可以通过系统软件包管理器安装,Windows 10/11 通常已经自带 curl,可直接在 CMD 或 PowerShell 中运行。

查看百度网站的 HTTP 响应头

如果重点是分析服务器返回的状态码、Content-Type、服务器信息等,可以使用 -I 参数:

Bash
curl -I https://www.baidu.com

该参数执行 HEAD 请求,只获取响应头而不下载完整网页内容。

常见响应头可能类似:

HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: ...
Connection: keep-alive

其中 200 OK 表示请求成功,Content-Type 用于说明响应数据的媒体类型。

需要注意的是,部分网站会针对 HEAD 请求采取与 GET 请求不同的处理策略,因此如果需要验证实际网页内容,仍然应该使用普通的 GET 请求。

显示详细请求和响应过程

排查 curl 请求问题时,-v 参数非常实用:

Bash
curl -v https://www.baidu.com

执行后可以看到 DNS 连接、TCP/TLS 建立过程以及 HTTP 请求和响应头等调试信息。

例如:

* Connected to www.baidu.com
> GET / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/...
>
< HTTP/1.1 200 OK
< Content-Type: text/html

其中:

  • * 开头的内容通常属于 curl 的连接和调试信息;

  • > 表示 curl 发给服务器的 HTTP 请求;

  • < 表示服务器返回的 HTTP 响应。

如果只是想查看 HTTP 层面的请求过程,而不希望输出 TLS 等更底层的信息,也可以结合其他参数进行控制。

只输出百度网页正文

curl 默认会把服务器返回的数据直接输出到标准输出,因此可以直接将结果保存到文件:

Bash
curl https://www.baidu.com -o baidu.html

执行完成后,当前目录会生成 baidu.html。

也可以使用 Shell 重定向:

Bash
curl https://www.baidu.com > baidu.html

两种方式都可以保存响应正文,但 -o 的语义更加明确,也更适合脚本中的文件下载场景。

保存后,可以使用文本工具查看:

Bash
cat baidu.html

或者:

Bash
less baidu.html

获取 HTTP 状态码

实际开发中,很多时候并不需要完整 HTML,只想判断百度是否正常响应,可以使用:

Bash
curl -s -o /dev/null -w "%{http_code}
" https://www.baidu.com

参数含义如下:

  • -s:关闭进度和错误之外的常规输出;

  • -o /dev/null:丢弃网页正文;

  • -w "%{http_code} ":按照指定格式输出 HTTP 状态码。

如果服务器正常返回,可能得到:

200

这种写法非常适合 Shell 脚本中的健康检查。

例如:

Bash
status=$(curl -s -o /dev/null -w "%{http_code}" https://www.baidu.com)

if [ "$status" = "200" ]; then
    echo "百度访问正常"
else
    echo "请求异常,HTTP状态码:$status"
fi

查看响应时间

curl 的 -w 参数不仅可以获取状态码,还能输出请求耗时:

Bash
curl -s -o /dev/null 
-w "HTTP: %{http_code}
Time: %{time_total}s
" 
https://www.baidu.com

可能得到:

HTTP: 200
Time: 0.245123s

time_total 表示从请求开始到传输完成的大致总耗时。

如果需要进行网络性能分析,还可以查看 DNS、TCP、TLS 等阶段的耗时:

Bash
curl -s -o /dev/null 
-w "DNS: %{time_namelookup}s
Connect: %{time_connect}s
TLS: %{time_appconnect}s
Start: %{time_starttransfer}s
Total: %{time_total}s
" 
https://www.baidu.com

这种方式对于定位“DNS 慢”“建立连接慢”或“服务器响应慢”等问题很有帮助。

查看百度返回的页面内容并解析 HTML

curl 本身主要负责发送网络请求和获取响应,并不是专门的 HTML 解析器。

例如:

Bash
curl -s https://www.baidu.com

得到的是 HTML 文本。如果需要进一步提取标题、链接或其他元素,可以把 curl 的输出交给其他命令行工具处理。

例如简单搜索 HTML 中的 </code>:</p> <pre class="overflow-visible! px-0!" data-start="2874" data-end="2935"><div class="relative w-full mt-4 mb-1"><div class=""><div class="contents"><div class="border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl"><div class="relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback"><div class="select-none sticky z-2 top-(--sticky-padding-top)"><div class="flex w-full items-center justify-between py-1.5 ps-4 pe-1.5 font-sans md:ps-5 bg-(--code-block-surface)"><div class="flex max-w-[75%] min-w-0 cursor-default items-center text-sm font-medium justify-self-start text-token-text-primary"><use href="/cdn/assets/sprites-core-bef2fe87.svg#72c6a7" fill="currentColor"></use>Bash</div><div class="flex flex-row items-center gap-0.5 justify-self-end"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use></div></div></div><div class="pointer-events-none absolute inset-x-4 top-12 bottom-4"><div class="pointer-events-none sticky z-40 shrink-0 z-1!" style="top: calc(calc(var(--sticky-padding-top) + 48px) - 1px * 3); margin-bottom: calc(-4px); height: calc(4px); mask-image: linear-gradient(transparent 25%, white 75%)"><div class="sticky bg-token-border-light" style="top: calc(var(--sticky-padding-top) + 48px); height: 1px"></div></div></div><div class="relative"><div class="h-full min-h-0 min-w-0"><div class="h-full min-h-0 min-w-0" style="width: 100%; transform: none"><div class=""><div class="relative"><div class="" style="transform: none"><div class="relative z-0 flex max-w-full"><div id="code-block-viewer" dir="ltr" class="q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch ͼd ͼr"><div class="cm-scroller"><pre class="cm-content q9tKkq_readonly m-0"><code><span class="ͼl">curl</span><span> </span><span class="ͼn">-s</span><span> https://www.baidu.com | </span><span class="ͼl">grep</span><span> </span><span class="ͼn">-i</span><span> </span><span class="ͼk">"<title>"</span></code></pre></div></div></div></div></div></div></div></div><div class=""><div class="" style="width: 100%; transform: none"></div></div></div></div></div></div></div></div></pre> <p dir="auto" data-start="2937" data-end="2957">如果只需要查看包含关键词的内容,也可以:</p> <pre class="overflow-visible! px-0!" data-start="2959" data-end="3015"><div class="relative w-full mt-4 mb-1"><div class=""><div class="contents"><div class="border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl"><div class="relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback"><div class="select-none sticky z-2 top-(--sticky-padding-top)"><div class="flex w-full items-center justify-between py-1.5 ps-4 pe-1.5 font-sans md:ps-5 bg-(--code-block-surface)"><div class="flex max-w-[75%] min-w-0 cursor-default items-center text-sm font-medium justify-self-start text-token-text-primary"><use href="/cdn/assets/sprites-core-bef2fe87.svg#72c6a7" fill="currentColor"></use>Bash</div><div class="flex flex-row items-center gap-0.5 justify-self-end"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use></div></div></div><div class="pointer-events-none absolute inset-x-4 top-12 bottom-4"><div class="pointer-events-none sticky z-40 shrink-0 z-1!" style="top: calc(calc(var(--sticky-padding-top) + 48px) - 1px * 3); margin-bottom: calc(-4px); height: calc(4px); mask-image: linear-gradient(transparent 25%, white 75%)"><div class="sticky bg-token-border-light" style="top: calc(var(--sticky-padding-top) + 48px); height: 1px"></div></div></div><div class="relative"><div class="h-full min-h-0 min-w-0"><div class="h-full min-h-0 min-w-0" style="width: 100%; transform: none"><div class=""><div class="relative"><div class="" style="transform: none"><div class="relative z-0 flex max-w-full"><div id="code-block-viewer" dir="ltr" class="q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch ͼd ͼr"><div class="cm-scroller"><pre class="cm-content q9tKkq_readonly m-0"><code><span class="ͼl">curl</span><span> </span><span class="ͼn">-s</span><span> https://www.baidu.com | </span><span class="ͼl">grep</span><span> </span><span class="ͼn">-i</span><span> </span><span class="ͼk">"百度"</span></code></pre></div></div></div></div></div></div></div></div><div class=""><div class="" style="width: 100%; transform: none"></div></div></div></div></div></div></div></div></pre> <p dir="auto" data-start="3017" data-end="3067">不过,<code data-start="3020" data-end="3026">grep</code> 只是文本匹配工具,并不理解 HTML 的 DOM 结构,因此不适合处理复杂网页。</p> <p dir="auto" data-start="3069" data-end="3151">对于结构化 HTML 解析,可以使用 <code data-start="3088" data-end="3097">xmllint</code>、Python、BeautifulSoup 等工具。例如使用 Python 处理 curl 保存下来的页面:</p> <pre class="overflow-visible! px-0!" data-start="3153" data-end="3208"><div class="relative w-full mt-4 mb-1"><div class=""><div class="contents"><div class="border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl"><div class="relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback"><div class="select-none sticky z-2 top-(--sticky-padding-top)"><div class="flex w-full items-center justify-between py-1.5 ps-4 pe-1.5 font-sans md:ps-5 bg-(--code-block-surface)"><div class="flex max-w-[75%] min-w-0 cursor-default items-center text-sm font-medium justify-self-start text-token-text-primary"><use href="/cdn/assets/sprites-core-bef2fe87.svg#72c6a7" fill="currentColor"></use>Bash</div><div class="flex flex-row items-center gap-0.5 justify-self-end"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use></div></div></div><div class="pointer-events-none absolute inset-x-4 top-12 bottom-4"><div class="pointer-events-none sticky z-40 shrink-0 z-1!" style="top: calc(calc(var(--sticky-padding-top) + 48px) - 1px * 3); margin-bottom: calc(-4px); height: calc(4px); mask-image: linear-gradient(transparent 25%, white 75%)"><div class="sticky bg-token-border-light" style="top: calc(var(--sticky-padding-top) + 48px); height: 1px"></div></div></div><div class="relative"><div class="h-full min-h-0 min-w-0"><div class="h-full min-h-0 min-w-0" style="width: 100%; transform: none"><div class=""><div class="relative"><div class="" style="transform: none"><div class="relative z-0 flex max-w-full"><div id="code-block-viewer" dir="ltr" class="q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch ͼd ͼr"><div class="cm-scroller"><pre class="cm-content q9tKkq_readonly m-0"><code><span class="ͼl">curl</span><span> </span><span class="ͼn">-s</span><span> https://www.baidu.com </span><span class="ͼn">-o</span><span> baidu.html</span></code></pre></div></div></div></div></div></div></div></div><div class=""><div class="" style="width: 100%; transform: none"></div></div></div></div></div></div></div></div></pre> <p dir="auto" data-start="3210" data-end="3213">然后:</p> <pre class="overflow-visible! px-0!" data-start="3215" data-end="3443"><div class="relative w-full mt-4 mb-1"><div class=""><div class="contents"><div class="border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl"><div class="relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback"><div class="select-none sticky z-2 top-(--sticky-padding-top)"><div class="flex w-full items-center justify-between py-1.5 ps-4 pe-1.5 font-sans md:ps-5 bg-(--code-block-surface)"><div class="flex max-w-[75%] min-w-0 cursor-default items-center text-sm font-medium justify-self-start text-token-text-primary"><use href="/cdn/assets/sprites-core-bef2fe87.svg#72c6a7" fill="currentColor"></use>Python</div><div class="flex flex-row items-center gap-0.5 justify-self-end"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use><div class="flex w-full items-center justify-center gap-1.5"><use href="/cdn/assets/sprites-core-bef2fe87.svg#6ef7a4" fill="currentColor"></use>运行</div></div></div></div><div class="pointer-events-none absolute inset-x-4 top-12 bottom-4"><div class="pointer-events-none sticky z-40 shrink-0 z-1!" style="top: calc(calc(var(--sticky-padding-top) + 48px) - 1px * 3); margin-bottom: calc(-4px); height: calc(4px); mask-image: linear-gradient(transparent 25%, white 75%)"><div class="sticky bg-token-border-light" style="top: calc(var(--sticky-padding-top) + 48px); height: 1px"></div></div></div><div class="relative"><div class="h-full min-h-0 min-w-0"><div class="h-full min-h-0 min-w-0" style="width: 100%; transform: none"><div class=""><div class="relative"><div class="" style="transform: none"><div class="relative z-0 flex max-w-full"><div id="code-block-viewer" dir="ltr" class="q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch ͼd ͼr"><div class="cm-scroller"><pre class="cm-content q9tKkq_readonly m-0"><code><span class="ͼg">from</span><span> </span><span class="ͼm">bs4</span><span> </span><span class="ͼg">import</span><span> </span><span class="ͼm">BeautifulSoup</span><span> </span><span class="ͼg">with</span><span> </span><span class="ͼm">open</span><span>(</span><span class="ͼk">"baidu.html"</span><span>, </span><span class="ͼk">"r"</span><span>, </span><span class="ͼm">encoding</span><span class="ͼg">=</span><span class="ͼk">"utf-8"</span><span>) </span><span class="ͼg">as</span><span> </span><span class="ͼm">f</span><span>: </span><span class="ͼm">html</span><span> </span><span class="ͼg">=</span><span> </span><span class="ͼm">f</span><span class="ͼg">.</span><span>read() </span><span class="ͼm">soup</span><span> </span><span class="ͼg">=</span><span> </span><span class="ͼm">BeautifulSoup</span><span>(</span><span class="ͼm">html</span><span>, </span><span class="ͼk">"html.parser"</span><span>) </span><span class="ͼm">print</span><span>(</span><span class="ͼm">soup</span><span class="ͼg">.</span><span>title</span><span class="ͼg">.</span><span>get_text(</span><span class="ͼm">strip</span><span class="ͼg">=</span><span class="ͼj">True</span><span>) </span><span class="ͼg">if</span><span> </span><span class="ͼm">soup</span><span class="ͼg">.</span><span>title </span><span class="ͼg">else</span><span> </span><span class="ͼk">"没有找到标题"</span><span>)</span></code></pre></div></div></div></div></div></div></div></div><div class=""><div class="" style="width: 100%; transform: none"></div></div></div></div></div></div></div></div></pre> <p dir="auto" data-start="3445" data-end="3475">这样就能将“请求网页”和“解析 HTML”两个环节分开处理。</p> <h3 data-section-id="6tzmk0" dir="auto" data-start="3477" data-end="3502">使用 User-Agent 模拟浏览器请求</h3> <p dir="auto" data-start="3504" data-end="3568">服务器有时会根据 User-Agent 判断客户端类型。curl 默认使用类似 <code data-start="3544" data-end="3554">curl/版本号</code> 的 User-Agent。</p> <p dir="auto" data-start="3570" data-end="3584">可以通过 <code data-start="3575" data-end="3579">-A</code> 自定义:</p> <pre class="overflow-visible! px-0!" data-start="3586" data-end="3641"><div class="relative w-full mt-4 mb-1"><div class=""><div class="contents"><div class="border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl"><div class="relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback"><div class="select-none sticky z-2 top-(--sticky-padding-top)"><div class="flex w-full items-center justify-between py-1.5 ps-4 pe-1.5 font-sans md:ps-5 bg-(--code-block-surface)"><div class="flex max-w-[75%] min-w-0 cursor-default items-center text-sm font-medium justify-self-start text-token-text-primary"><use href="/cdn/assets/sprites-core-bef2fe87.svg#72c6a7" fill="currentColor"></use>Bash</div><div class="flex flex-row items-center gap-0.5 justify-self-end"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use></div></div></div><div class="pointer-events-none absolute inset-x-4 top-12 bottom-4"><div class="pointer-events-none sticky z-40 shrink-0 z-1!" style="top: calc(calc(var(--sticky-padding-top) + 48px) - 1px * 3); margin-bottom: calc(-4px); height: calc(4px); mask-image: linear-gradient(transparent 25%, white 75%)"><div class="sticky bg-token-border-light" style="top: calc(var(--sticky-padding-top) + 48px); height: 1px"></div></div></div><div class="relative"><div class="h-full min-h-0 min-w-0"><div class="h-full min-h-0 min-w-0" style="width: 100%; transform: none"><div class=""><div class="relative"><div class="" style="transform: none"><div class="relative z-0 flex max-w-full"><div id="code-block-viewer" dir="ltr" class="q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch ͼd ͼr"><div class="cm-scroller"><pre class="cm-content q9tKkq_readonly m-0"><code><span class="ͼl">curl</span><span> </span><span class="ͼn">-A</span><span> </span><span class="ͼk">"Mozilla/5.0"</span><span> https://www.baidu.com</span></code></pre></div></div></div></div></div></div></div></div><div class=""><div class="" style="width: 100%; transform: none"></div></div></div></div></div></div></div></div></pre> <p dir="auto" data-start="3643" data-end="3667">也可以指定更完整的浏览器 User-Agent:</p> <pre class="overflow-visible! px-0!" data-start="3669" data-end="3804"><div class="relative w-full mt-4 mb-1"><div class=""><div class="contents"><div class="border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl"><div class="relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback"><div class="select-none sticky z-2 top-(--sticky-padding-top)"><div class="flex w-full items-center justify-between py-1.5 ps-4 pe-1.5 font-sans md:ps-5 bg-(--code-block-surface)"><div class="flex max-w-[75%] min-w-0 cursor-default items-center text-sm font-medium justify-self-start text-token-text-primary"><use href="/cdn/assets/sprites-core-bef2fe87.svg#72c6a7" fill="currentColor"></use>Bash</div><div class="flex flex-row items-center gap-0.5 justify-self-end"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use></div></div></div><div class="pointer-events-none absolute inset-x-4 top-12 bottom-4"><div class="pointer-events-none sticky z-40 shrink-0 z-1!" style="top: calc(calc(var(--sticky-padding-top) + 48px) - 1px * 3); margin-bottom: calc(-4px); height: calc(4px); mask-image: linear-gradient(transparent 25%, white 75%)"><div class="sticky bg-token-border-light" style="top: calc(var(--sticky-padding-top) + 48px); height: 1px"></div></div></div><div class="relative"><div class="h-full min-h-0 min-w-0"><div class="h-full min-h-0 min-w-0" style="width: 100%; transform: none"><div class=""><div class="relative"><div class="" style="transform: none"><div class="relative z-0 flex max-w-full"><div id="code-block-viewer" dir="ltr" class="q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch ͼd ͼr"><div class="cm-scroller"><pre class="cm-content q9tKkq_readonly m-0"><code><span class="ͼl">curl</span><span> </span><span class="ͼn">-A</span><span> </span><span class="ͼk">"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/128.0.0.0 Safari/537.36"</span><span> https://www.baidu.com</span></code></pre></div></div></div></div></div></div></div></div><div class=""><div class="" style="width: 100%; transform: none"></div></div></div></div></div></div></div></div></pre> <p dir="auto" data-start="3806" data-end="3861">需要注意,修改 User-Agent 只是改变请求头中的客户端标识,并不能保证网站一定按照浏览器方式返回内容。</p> <h3 data-section-id="13qv13s" dir="auto" data-start="3863" data-end="3880">自动处理 HTTP 重定向</h3> <p dir="auto" data-start="3882" data-end="3931">网站可能通过 <code data-start="3889" data-end="3894">301</code> 或 <code data-start="3897" data-end="3902">302</code> 将请求跳转到其他地址。curl 默认不会自动跟随重定向。</p> <p dir="auto" data-start="3933" data-end="3943">可以添加 <code data-start="3938" data-end="3942">-L</code>:</p> <pre class="overflow-visible! px-0!" data-start="3945" data-end="3986"><div class="relative w-full mt-4 mb-1"><div class=""><div class="contents"><div class="border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl"><div class="relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback"><div class="select-none sticky z-2 top-(--sticky-padding-top)"><div class="flex w-full items-center justify-between py-1.5 ps-4 pe-1.5 font-sans md:ps-5 bg-(--code-block-surface)"><div class="flex max-w-[75%] min-w-0 cursor-default items-center text-sm font-medium justify-self-start text-token-text-primary"><use href="/cdn/assets/sprites-core-bef2fe87.svg#72c6a7" fill="currentColor"></use>Bash</div><div class="flex flex-row items-center gap-0.5 justify-self-end"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use></div></div></div><div class="pointer-events-none absolute inset-x-4 top-12 bottom-4"><div class="pointer-events-none sticky z-40 shrink-0 z-1!" style="top: calc(calc(var(--sticky-padding-top) + 48px) - 1px * 3); margin-bottom: calc(-4px); height: calc(4px); mask-image: linear-gradient(transparent 25%, white 75%)"><div class="sticky bg-token-border-light" style="top: calc(var(--sticky-padding-top) + 48px); height: 1px"></div></div></div><div class="relative"><div class="h-full min-h-0 min-w-0"><div class="h-full min-h-0 min-w-0" style="width: 100%; transform: none"><div class=""><div class="relative"><div class="" style="transform: none"><div class="relative z-0 flex max-w-full"><div id="code-block-viewer" dir="ltr" class="q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch ͼd ͼr"><div class="cm-scroller"><pre class="cm-content q9tKkq_readonly m-0"><code><span class="ͼl">curl</span><span> </span><span class="ͼn">-L</span><span> https://www.baidu.com</span></code></pre></div></div></div></div></div></div></div></div><div class=""><div class="" style="width: 100%; transform: none"></div></div></div></div></div></div></div></div></pre> <p dir="auto" data-start="3988" data-end="4006">如果希望同时查看最终响应状态,可以:</p> <pre class="overflow-visible! px-0!" data-start="4008" data-end="4085"><div class="relative w-full mt-4 mb-1"><div class=""><div class="contents"><div class="border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl"><div class="relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback"><div class="select-none sticky z-2 top-(--sticky-padding-top)"><div class="flex w-full items-center justify-between py-1.5 ps-4 pe-1.5 font-sans md:ps-5 bg-(--code-block-surface)"><div class="flex max-w-[75%] min-w-0 cursor-default items-center text-sm font-medium justify-self-start text-token-text-primary"><use href="/cdn/assets/sprites-core-bef2fe87.svg#72c6a7" fill="currentColor"></use>Bash</div><div class="flex flex-row items-center gap-0.5 justify-self-end"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use></div></div></div><div class="pointer-events-none absolute inset-x-4 top-12 bottom-4"><div class="pointer-events-none sticky z-40 shrink-0 z-1!" style="top: calc(calc(var(--sticky-padding-top) + 48px) - 1px * 3); margin-bottom: calc(-4px); height: calc(4px); mask-image: linear-gradient(transparent 25%, white 75%)"><div class="sticky bg-token-border-light" style="top: calc(var(--sticky-padding-top) + 48px); height: 1px"></div></div></div><div class="relative"><div class="h-full min-h-0 min-w-0"><div class="h-full min-h-0 min-w-0" style="width: 100%; transform: none"><div class=""><div class="relative"><div class="" style="transform: none"><div class="relative z-0 flex max-w-full"><div id="code-block-viewer" dir="ltr" class="q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch ͼd ͼr"><div class="cm-scroller"><pre class="cm-content q9tKkq_readonly m-0"><code><span class="ͼl">curl</span><span> </span><span class="ͼn">-s</span><span> </span><span class="ͼn">-L</span><span> </span><span class="ͼn">-o</span><span> /dev/null </span><span class="ͼn">-w</span><span> </span><span class="ͼk">"%{http_code} "</span><span> https://www.baidu.com</span></code></pre></div></div></div></div></div></div></div></div><div class=""><div class="" style="width: 100%; transform: none"></div></div></div></div></div></div></div></div></pre> <p dir="auto" data-start="4087" data-end="4124">对于需要跟踪跳转链的场景,可以结合 <code data-start="4105" data-end="4109">-v</code> 观察每一次 HTTP 响应。</p> <h3 data-section-id="h3napx" dir="auto" data-start="4126" data-end="4143">将响应头和响应正文一起查看</h3> <p dir="auto" data-start="4145" data-end="4167">如果希望完整观察服务器返回的数据,可以使用:</p> <pre class="overflow-visible! px-0!" data-start="4169" data-end="4210"><div class="relative w-full mt-4 mb-1"><div class=""><div class="contents"><div class="border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl"><div class="relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback"><div class="select-none sticky z-2 top-(--sticky-padding-top)"><div class="flex w-full items-center justify-between py-1.5 ps-4 pe-1.5 font-sans md:ps-5 bg-(--code-block-surface)"><div class="flex max-w-[75%] min-w-0 cursor-default items-center text-sm font-medium justify-self-start text-token-text-primary"><use href="/cdn/assets/sprites-core-bef2fe87.svg#72c6a7" fill="currentColor"></use>Bash</div><div class="flex flex-row items-center gap-0.5 justify-self-end"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use></div></div></div><div class="pointer-events-none absolute inset-x-4 top-12 bottom-4"><div class="pointer-events-none sticky z-40 shrink-0 z-1!" style="top: calc(calc(var(--sticky-padding-top) + 48px) - 1px * 3); margin-bottom: calc(-4px); height: calc(4px); mask-image: linear-gradient(transparent 25%, white 75%)"><div class="sticky bg-token-border-light" style="top: calc(var(--sticky-padding-top) + 48px); height: 1px"></div></div></div><div class="relative"><div class="h-full min-h-0 min-w-0"><div class="h-full min-h-0 min-w-0" style="width: 100%; transform: none"><div class=""><div class="relative"><div class="" style="transform: none"><div class="relative z-0 flex max-w-full"><div id="code-block-viewer" dir="ltr" class="q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch ͼd ͼr"><div class="cm-scroller"><pre class="cm-content q9tKkq_readonly m-0"><code><span class="ͼl">curl</span><span> </span><span class="ͼn">-i</span><span> https://www.baidu.com</span></code></pre></div></div></div></div></div></div></div></div><div class=""><div class="" style="width: 100%; transform: none"></div></div></div></div></div></div></div></div></pre> <p dir="auto" data-start="4212" data-end="4238"><code data-start="4212" data-end="4216">-i</code> 会将 HTTP 响应头和响应正文同时输出。</p> <p dir="auto" data-start="4240" data-end="4243">例如:</p> <pre class="overflow-visible! px-0!" data-start="4245" data-end="4336"><div class="relative w-full mt-4 mb-1"><div class=""><div class="contents"><div class="relative"><div class="h-full min-h-0 min-w-0"><div class="h-full min-h-0 min-w-0" style="width: 100%; transform: none"><div class="border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl"><div class="h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback"><div class="pointer-events-none absolute end-1.5 top-1 z-2 md:end-2 md:top-1"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use></div><div class="relative"><div class="pe-11 pt-3" style="transform: none"><div class="relative z-0 flex max-w-full"><div id="code-block-viewer" dir="ltr" class="q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch ͼd ͼr"><div class="cm-scroller"><pre class="cm-content q9tKkq_readonly m-0"><code><span>HTTP/1.1 200 OK Content-Type: text/html ... <!DOCTYPE html> <html> ... </html></span></code></pre></div></div></div></div></div></div></div></div></div><div class=""><div class="" style="width: 100%; transform: none"></div></div></div></div></div></div></pre> <p dir="auto" data-start="4338" data-end="4394">与 <code data-start="4340" data-end="4344">-I</code> 不同,<code data-start="4348" data-end="4352">-I</code> 主要获取响应头,而 <code data-start="4363" data-end="4367">-i</code> 则是在正常 GET 请求的基础上把响应头也显示出来。</p> <h3 data-section-id="1v2ayyt" dir="auto" data-start="4396" data-end="4416">查看百度请求过程中是否发生重定向</h3> <p dir="auto" data-start="4418" data-end="4423">可以执行:</p> <pre class="overflow-visible! px-0!" data-start="4425" data-end="4466"><div class="relative w-full mt-4 mb-1"><div class=""><div class="contents"><div class="border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl"><div class="relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback"><div class="select-none sticky z-2 top-(--sticky-padding-top)"><div class="flex w-full items-center justify-between py-1.5 ps-4 pe-1.5 font-sans md:ps-5 bg-(--code-block-surface)"><div class="flex max-w-[75%] min-w-0 cursor-default items-center text-sm font-medium justify-self-start text-token-text-primary"><use href="/cdn/assets/sprites-core-bef2fe87.svg#72c6a7" fill="currentColor"></use>Bash</div><div class="flex flex-row items-center gap-0.5 justify-self-end"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use></div></div></div><div class="pointer-events-none absolute inset-x-4 top-12 bottom-4"><div class="pointer-events-none sticky z-40 shrink-0 z-1!" style="top: calc(calc(var(--sticky-padding-top) + 48px) - 1px * 3); margin-bottom: calc(-4px); height: calc(4px); mask-image: linear-gradient(transparent 25%, white 75%)"><div class="sticky bg-token-border-light" style="top: calc(var(--sticky-padding-top) + 48px); height: 1px"></div></div></div><div class="relative"><div class="h-full min-h-0 min-w-0"><div class="h-full min-h-0 min-w-0" style="width: 100%; transform: none"><div class=""><div class="relative"><div class="" style="transform: none"><div class="relative z-0 flex max-w-full"><div id="code-block-viewer" dir="ltr" class="q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch ͼd ͼr"><div class="cm-scroller"><pre class="cm-content q9tKkq_readonly m-0"><code><span class="ͼl">curl</span><span> </span><span class="ͼn">-I</span><span> https://www.baidu.com</span></code></pre></div></div></div></div></div></div></div></div><div class=""><div class="" style="width: 100%; transform: none"></div></div></div></div></div></div></div></div></pre> <p dir="auto" data-start="4468" data-end="4478">如果需要跟随重定向:</p> <pre class="overflow-visible! px-0!" data-start="4480" data-end="4524"><div class="relative w-full mt-4 mb-1"><div class=""><div class="contents"><div class="border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl"><div class="relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback"><div class="select-none sticky z-2 top-(--sticky-padding-top)"><div class="flex w-full items-center justify-between py-1.5 ps-4 pe-1.5 font-sans md:ps-5 bg-(--code-block-surface)"><div class="flex max-w-[75%] min-w-0 cursor-default items-center text-sm font-medium justify-self-start text-token-text-primary"><use href="/cdn/assets/sprites-core-bef2fe87.svg#72c6a7" fill="currentColor"></use>Bash</div><div class="flex flex-row items-center gap-0.5 justify-self-end"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use></div></div></div><div class="pointer-events-none absolute inset-x-4 top-12 bottom-4"><div class="pointer-events-none sticky z-40 shrink-0 z-1!" style="top: calc(calc(var(--sticky-padding-top) + 48px) - 1px * 3); margin-bottom: calc(-4px); height: calc(4px); mask-image: linear-gradient(transparent 25%, white 75%)"><div class="sticky bg-token-border-light" style="top: calc(var(--sticky-padding-top) + 48px); height: 1px"></div></div></div><div class="relative"><div class="h-full min-h-0 min-w-0"><div class="h-full min-h-0 min-w-0" style="width: 100%; transform: none"><div class=""><div class="relative"><div class="" style="transform: none"><div class="relative z-0 flex max-w-full"><div id="code-block-viewer" dir="ltr" class="q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch ͼd ͼr"><div class="cm-scroller"><pre class="cm-content q9tKkq_readonly m-0"><code><span class="ͼl">curl</span><span> </span><span class="ͼn">-I</span><span> </span><span class="ͼn">-L</span><span> https://www.baidu.com</span></code></pre></div></div></div></div></div></div></div></div><div class=""><div class="" style="width: 100%; transform: none"></div></div></div></div></div></div></div></div></pre> <p dir="auto" data-start="4526" data-end="4583">对于部分服务器来说,HEAD 请求与 GET 请求的响应并不完全一致。如果需要准确模拟实际页面访问过程,可以使用:</p> <pre class="overflow-visible! px-0!" data-start="4585" data-end="4647"><div class="relative w-full mt-4 mb-1"><div class=""><div class="contents"><div class="border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl"><div class="relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback"><div class="select-none sticky z-2 top-(--sticky-padding-top)"><div class="flex w-full items-center justify-between py-1.5 ps-4 pe-1.5 font-sans md:ps-5 bg-(--code-block-surface)"><div class="flex max-w-[75%] min-w-0 cursor-default items-center text-sm font-medium justify-self-start text-token-text-primary"><use href="/cdn/assets/sprites-core-bef2fe87.svg#72c6a7" fill="currentColor"></use>Bash</div><div class="flex flex-row items-center gap-0.5 justify-self-end"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use></div></div></div><div class="pointer-events-none absolute inset-x-4 top-12 bottom-4"><div class="pointer-events-none sticky z-40 shrink-0 z-1!" style="top: calc(calc(var(--sticky-padding-top) + 48px) - 1px * 3); margin-bottom: calc(-4px); height: calc(4px); mask-image: linear-gradient(transparent 25%, white 75%)"><div class="sticky bg-token-border-light" style="top: calc(var(--sticky-padding-top) + 48px); height: 1px"></div></div></div><div class="relative"><div class="h-full min-h-0 min-w-0"><div class="h-full min-h-0 min-w-0" style="width: 100%; transform: none"><div class=""><div class="relative"><div class="" style="transform: none"><div class="relative z-0 flex max-w-full"><div id="code-block-viewer" dir="ltr" class="q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch ͼd ͼr"><div class="cm-scroller"><pre class="cm-content q9tKkq_readonly m-0"><code><span class="ͼl">curl</span><span> </span><span class="ͼn">-s</span><span> </span><span class="ͼn">-D</span><span> </span><span class="ͼn">-</span><span> </span><span class="ͼn">-o</span><span> /dev/null </span><span class="ͼn">-L</span><span> https://www.baidu.com</span></code></pre></div></div></div></div></div></div></div></div><div class=""><div class="" style="width: 100%; transform: none"></div></div></div></div></div></div></div></div></pre> <p dir="auto" data-start="4649" data-end="4694">其中 <code data-start="4652" data-end="4658">-D -</code> 表示把响应头输出到标准输出,<code data-start="4673" data-end="4687">-o /dev/null</code> 则丢弃正文。</p> <h3 data-section-id="1h5gvoe" dir="auto" data-start="4696" data-end="4713">处理 HTTPS 证书问题</h3> <p dir="auto" data-start="4715" data-end="4751">访问百度使用的是 HTTPS,因此 curl 会进行 TLS 证书校验。</p> <p dir="auto" data-start="4753" data-end="4763">正常情况下直接执行:</p> <pre class="overflow-visible! px-0!" data-start="4765" data-end="4803"><div class="relative w-full mt-4 mb-1"><div class=""><div class="contents"><div class="border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl"><div class="relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback"><div class="select-none sticky z-2 top-(--sticky-padding-top)"><div class="flex w-full items-center justify-between py-1.5 ps-4 pe-1.5 font-sans md:ps-5 bg-(--code-block-surface)"><div class="flex max-w-[75%] min-w-0 cursor-default items-center text-sm font-medium justify-self-start text-token-text-primary"><use href="/cdn/assets/sprites-core-bef2fe87.svg#72c6a7" fill="currentColor"></use>Bash</div><div class="flex flex-row items-center gap-0.5 justify-self-end"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use></div></div></div><div class="pointer-events-none absolute inset-x-4 top-12 bottom-4"><div class="pointer-events-none sticky z-40 shrink-0 z-1!" style="top: calc(calc(var(--sticky-padding-top) + 48px) - 1px * 3); margin-bottom: calc(-4px); height: calc(4px); mask-image: linear-gradient(transparent 25%, white 75%)"><div class="sticky bg-token-border-light" style="top: calc(var(--sticky-padding-top) + 48px); height: 1px"></div></div></div><div class="relative"><div class="h-full min-h-0 min-w-0"><div class="h-full min-h-0 min-w-0" style="width: 100%; transform: none"><div class=""><div class="relative"><div class="" style="transform: none"><div class="relative z-0 flex max-w-full"><div id="code-block-viewer" dir="ltr" class="q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch ͼd ͼr"><div class="cm-scroller"><pre class="cm-content q9tKkq_readonly m-0"><code><span class="ͼl">curl</span><span> https://www.baidu.com</span></code></pre></div></div></div></div></div></div></div></div><div class=""><div class="" style="width: 100%; transform: none"></div></div></div></div></div></div></div></div></pre> <p dir="auto" data-start="4805" data-end="4808">即可。</p> <p dir="auto" data-start="4810" data-end="4864">如果测试环境出现类似证书验证失败的问题,可以先检查系统时间、CA 证书以及网络环境,而不是直接关闭安全校验。</p> <p dir="auto" data-start="4866" data-end="4875">curl 提供了:</p> <pre class="overflow-visible! px-0!" data-start="4877" data-end="4918"><div class="relative w-full mt-4 mb-1"><div class=""><div class="contents"><div class="border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl"><div class="relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback"><div class="select-none sticky z-2 top-(--sticky-padding-top)"><div class="flex w-full items-center justify-between py-1.5 ps-4 pe-1.5 font-sans md:ps-5 bg-(--code-block-surface)"><div class="flex max-w-[75%] min-w-0 cursor-default items-center text-sm font-medium justify-self-start text-token-text-primary"><use href="/cdn/assets/sprites-core-bef2fe87.svg#72c6a7" fill="currentColor"></use>Bash</div><div class="flex flex-row items-center gap-0.5 justify-self-end"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use></div></div></div><div class="pointer-events-none absolute inset-x-4 top-12 bottom-4"><div class="pointer-events-none sticky z-40 shrink-0 z-1!" style="top: calc(calc(var(--sticky-padding-top) + 48px) - 1px * 3); margin-bottom: calc(-4px); height: calc(4px); mask-image: linear-gradient(transparent 25%, white 75%)"><div class="sticky bg-token-border-light" style="top: calc(var(--sticky-padding-top) + 48px); height: 1px"></div></div></div><div class="relative"><div class="h-full min-h-0 min-w-0"><div class="h-full min-h-0 min-w-0" style="width: 100%; transform: none"><div class=""><div class="relative"><div class="" style="transform: none"><div class="relative z-0 flex max-w-full"><div id="code-block-viewer" dir="ltr" class="q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch ͼd ͼr"><div class="cm-scroller"><pre class="cm-content q9tKkq_readonly m-0"><code><span class="ͼl">curl</span><span> </span><span class="ͼn">-k</span><span> https://www.baidu.com</span></code></pre></div></div></div></div></div></div></div></div><div class=""><div class="" style="width: 100%; transform: none"></div></div></div></div></div></div></div></div></pre> <p dir="auto" data-start="4920" data-end="4989">用于跳过证书验证,但这会降低 HTTPS 连接的安全性。<strong data-start="4948" data-end="4966">生产环境不建议使用 <code data-start="4960" data-end="4964">-k</code></strong>,尤其不要把它作为解决证书配置问题的长期方案。</p> <h3 data-section-id="17phwe4" dir="auto" data-start="4991" data-end="5003">设置连接超时时间</h3> <p dir="auto" data-start="5005" data-end="5048">脚本访问外部网站时,如果服务器长时间没有响应,程序可能一直等待。因此可以设置超时时间:</p> <pre class="overflow-visible! px-0!" data-start="5050" data-end="5122"><div class="relative w-full mt-4 mb-1"><div class=""><div class="contents"><div class="border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl"><div class="relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback"><div class="select-none sticky z-2 top-(--sticky-padding-top)"><div class="flex w-full items-center justify-between py-1.5 ps-4 pe-1.5 font-sans md:ps-5 bg-(--code-block-surface)"><div class="flex max-w-[75%] min-w-0 cursor-default items-center text-sm font-medium justify-self-start text-token-text-primary"><use href="/cdn/assets/sprites-core-bef2fe87.svg#72c6a7" fill="currentColor"></use>Bash</div><div class="flex flex-row items-center gap-0.5 justify-self-end"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use></div></div></div><div class="pointer-events-none absolute inset-x-4 top-12 bottom-4"><div class="pointer-events-none sticky z-40 shrink-0 z-1!" style="top: calc(calc(var(--sticky-padding-top) + 48px) - 1px * 3); margin-bottom: calc(-4px); height: calc(4px); mask-image: linear-gradient(transparent 25%, white 75%)"><div class="sticky bg-token-border-light" style="top: calc(var(--sticky-padding-top) + 48px); height: 1px"></div></div></div><div class="relative"><div class="h-full min-h-0 min-w-0"><div class="h-full min-h-0 min-w-0" style="width: 100%; transform: none"><div class=""><div class="relative"><div class="" style="transform: none"><div class="relative z-0 flex max-w-full"><div id="code-block-viewer" dir="ltr" class="q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch ͼd ͼr"><div class="cm-scroller"><pre class="cm-content q9tKkq_readonly m-0"><code><span class="ͼl">curl</span><span> </span><span class="ͼn">--connect-timeout</span><span> </span><span class="ͼj">5</span><span> </span><span class="ͼn">--max-time</span><span> </span><span class="ͼj">10</span><span> https://www.baidu.com</span></code></pre></div></div></div></div></div></div></div></div><div class=""><div class="" style="width: 100%; transform: none"></div></div></div></div></div></div></div></div></pre> <p dir="auto" data-start="5124" data-end="5127">这里:</p> <ul data-start="5129" data-end="5199"> <li data-section-id="9rrtyn" data-start="5129" data-end="5166"> <p dir="auto" data-start="5131" data-end="5166"><code data-start="5131" data-end="5152">--connect-timeout 5</code>:建立连接最多等待 5 秒;</p> </li> <li data-section-id="10twy0z" data-start="5167" data-end="5199"> <p dir="auto" data-start="5169" data-end="5199"><code data-start="5169" data-end="5184">--max-time 10</code>:整个请求最多运行 10 秒。</p> </li> </ul> <p dir="auto" data-start="5201" data-end="5221">这样可以避免网络异常导致脚本长时间阻塞。</p> <h3 data-section-id="kpsda2" dir="auto" data-start="5223" data-end="5245">使用 curl 判断百度是否可以访问</h3> <p dir="auto" data-start="5247" data-end="5264">如果目的是简单检测网络,可以采用:</p> <pre class="overflow-visible! px-0!" data-start="5266" data-end="5361"><div class="relative w-full mt-4 mb-1"><div class=""><div class="contents"><div class="border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl"><div class="relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback"><div class="select-none sticky z-2 top-(--sticky-padding-top)"><div class="flex w-full items-center justify-between py-1.5 ps-4 pe-1.5 font-sans md:ps-5 bg-(--code-block-surface)"><div class="flex max-w-[75%] min-w-0 cursor-default items-center text-sm font-medium justify-self-start text-token-text-primary"><use href="/cdn/assets/sprites-core-bef2fe87.svg#72c6a7" fill="currentColor"></use>Bash</div><div class="flex flex-row items-center gap-0.5 justify-self-end"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use></div></div></div><div class="pointer-events-none absolute inset-x-4 top-12 bottom-4"><div class="pointer-events-none sticky z-40 shrink-0 z-1!" style="top: calc(calc(var(--sticky-padding-top) + 48px) - 1px * 3); margin-bottom: calc(-4px); height: calc(4px); mask-image: linear-gradient(transparent 25%, white 75%)"><div class="sticky bg-token-border-light" style="top: calc(var(--sticky-padding-top) + 48px); height: 1px"></div></div></div><div class="relative"><div class="h-full min-h-0 min-w-0"><div class="h-full min-h-0 min-w-0" style="width: 100%; transform: none"><div class=""><div class="relative"><div class="" style="transform: none"><div class="relative z-0 flex max-w-full"><div id="code-block-viewer" dir="ltr" class="q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch ͼd ͼr"><div class="cm-scroller"><pre class="cm-content q9tKkq_readonly m-0"><code><span class="ͼl">curl</span><span> </span><span class="ͼn">-sS</span><span> </span><span class="ͼn">-o</span><span> /dev/null </span><span class="ͼn">-w</span><span> </span><span class="ͼk">"%{http_code} "</span><span> </span><span class="ͼn">--connect-timeout</span><span> </span><span class="ͼj">5</span><span> https://www.baidu.com</span></code></pre></div></div></div></div></div></div></div></div><div class=""><div class="" style="width: 100%; transform: none"></div></div></div></div></div></div></div></div></pre> <p dir="auto" data-start="5363" data-end="5396">其中 <code data-start="5366" data-end="5371">-sS</code> 在保持安静输出的同时,遇到错误仍然显示错误信息。</p> <p dir="auto" data-start="5398" data-end="5415">进一步结合 Shell 可以写成:</p> <pre class="overflow-visible! px-0!" data-start="5417" data-end="5541"><div class="relative w-full mt-4 mb-1"><div class=""><div class="contents"><div class="border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl"><div class="relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback"><div class="select-none sticky z-2 top-(--sticky-padding-top)"><div class="flex w-full items-center justify-between py-1.5 ps-4 pe-1.5 font-sans md:ps-5 bg-(--code-block-surface)"><div class="flex max-w-[75%] min-w-0 cursor-default items-center text-sm font-medium justify-self-start text-token-text-primary"><use href="/cdn/assets/sprites-core-bef2fe87.svg#72c6a7" fill="currentColor"></use>Bash</div><div class="flex flex-row items-center gap-0.5 justify-self-end"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use></div></div></div><div class="pointer-events-none absolute inset-x-4 top-12 bottom-4"><div class="pointer-events-none sticky z-40 shrink-0 z-1!" style="top: calc(calc(var(--sticky-padding-top) + 48px) - 1px * 3); margin-bottom: calc(-4px); height: calc(4px); mask-image: linear-gradient(transparent 25%, white 75%)"><div class="sticky bg-token-border-light" style="top: calc(var(--sticky-padding-top) + 48px); height: 1px"></div></div></div><div class="relative"><div class="h-full min-h-0 min-w-0"><div class="h-full min-h-0 min-w-0" style="width: 100%; transform: none"><div class=""><div class="relative"><div class="" style="transform: none"><div class="relative z-0 flex max-w-full"><div id="code-block-viewer" dir="ltr" class="q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch ͼd ͼr"><div class="cm-scroller"><pre class="cm-content q9tKkq_readonly m-0"><code><span class="ͼg">if</span><span> </span><span class="ͼl">curl</span><span> </span><span class="ͼn">-sS</span><span> </span><span class="ͼn">--connect-timeout</span><span> </span><span class="ͼj">5</span><span> </span><span class="ͼn">-o</span><span> /dev/null https://www.baidu.com; </span><span class="ͼg">then</span><span> </span><span class="ͼl">echo</span><span> </span><span class="ͼk">"请求成功"</span><span> </span><span class="ͼg">else</span><span> </span><span class="ͼl">echo</span><span> </span><span class="ͼk">"请求失败"</span><span> </span><span class="ͼg">fi</span></code></pre></div></div></div></div></div></div></div></div><div class=""><div class="" style="width: 100%; transform: none"></div></div></div></div></div></div></div></div></pre> <p dir="auto" data-start="5543" data-end="5573">这种检测方式比单纯判断是否输出 HTML 更适合自动化脚本。</p> <h3 data-section-id="172o6vz" dir="auto" data-start="5575" data-end="5588">常见问题及解决方法</h3> <p dir="auto" data-start="5590" data-end="5613"><strong data-start="5590" data-end="5613">1. 执行 curl 没有任何网页内容</strong></p> <p dir="auto" data-start="5615" data-end="5620">首先使用:</p> <pre class="overflow-visible! px-0!" data-start="5622" data-end="5663"><div class="relative w-full mt-4 mb-1"><div class=""><div class="contents"><div class="border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl"><div class="relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback"><div class="select-none sticky z-2 top-(--sticky-padding-top)"><div class="flex w-full items-center justify-between py-1.5 ps-4 pe-1.5 font-sans md:ps-5 bg-(--code-block-surface)"><div class="flex max-w-[75%] min-w-0 cursor-default items-center text-sm font-medium justify-self-start text-token-text-primary"><use href="/cdn/assets/sprites-core-bef2fe87.svg#72c6a7" fill="currentColor"></use>Bash</div><div class="flex flex-row items-center gap-0.5 justify-self-end"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use></div></div></div><div class="pointer-events-none absolute inset-x-4 top-12 bottom-4"><div class="pointer-events-none sticky z-40 shrink-0 z-1!" style="top: calc(calc(var(--sticky-padding-top) + 48px) - 1px * 3); margin-bottom: calc(-4px); height: calc(4px); mask-image: linear-gradient(transparent 25%, white 75%)"><div class="sticky bg-token-border-light" style="top: calc(var(--sticky-padding-top) + 48px); height: 1px"></div></div></div><div class="relative"><div class="h-full min-h-0 min-w-0"><div class="h-full min-h-0 min-w-0" style="width: 100%; transform: none"><div class=""><div class="relative"><div class="" style="transform: none"><div class="relative z-0 flex max-w-full"><div id="code-block-viewer" dir="ltr" class="q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch ͼd ͼr"><div class="cm-scroller"><pre class="cm-content q9tKkq_readonly m-0"><code><span class="ͼl">curl</span><span> </span><span class="ͼn">-v</span><span> https://www.baidu.com</span></code></pre></div></div></div></div></div></div></div></div><div class=""><div class="" style="width: 100%; transform: none"></div></div></div></div></div></div></div></div></pre> <p dir="auto" data-start="5665" data-end="5692">检查 DNS、TCP、TLS 和 HTTP 请求过程。</p> <p dir="auto" data-start="5694" data-end="5713"><strong data-start="5694" data-end="5713">2. 返回 301 或 302</strong></p> <p dir="auto" data-start="5715" data-end="5720">可以添加:</p> <pre class="overflow-visible! px-0!" data-start="5722" data-end="5763"><div class="relative w-full mt-4 mb-1"><div class=""><div class="contents"><div class="border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl"><div class="relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback"><div class="select-none sticky z-2 top-(--sticky-padding-top)"><div class="flex w-full items-center justify-between py-1.5 ps-4 pe-1.5 font-sans md:ps-5 bg-(--code-block-surface)"><div class="flex max-w-[75%] min-w-0 cursor-default items-center text-sm font-medium justify-self-start text-token-text-primary"><use href="/cdn/assets/sprites-core-bef2fe87.svg#72c6a7" fill="currentColor"></use>Bash</div><div class="flex flex-row items-center gap-0.5 justify-self-end"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use></div></div></div><div class="pointer-events-none absolute inset-x-4 top-12 bottom-4"><div class="pointer-events-none sticky z-40 shrink-0 z-1!" style="top: calc(calc(var(--sticky-padding-top) + 48px) - 1px * 3); margin-bottom: calc(-4px); height: calc(4px); mask-image: linear-gradient(transparent 25%, white 75%)"><div class="sticky bg-token-border-light" style="top: calc(var(--sticky-padding-top) + 48px); height: 1px"></div></div></div><div class="relative"><div class="h-full min-h-0 min-w-0"><div class="h-full min-h-0 min-w-0" style="width: 100%; transform: none"><div class=""><div class="relative"><div class="" style="transform: none"><div class="relative z-0 flex max-w-full"><div id="code-block-viewer" dir="ltr" class="q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch ͼd ͼr"><div class="cm-scroller"><pre class="cm-content q9tKkq_readonly m-0"><code><span class="ͼl">curl</span><span> </span><span class="ͼn">-L</span><span> https://www.baidu.com</span></code></pre></div></div></div></div></div></div></div></div><div class=""><div class="" style="width: 100%; transform: none"></div></div></div></div></div></div></div></div></pre> <p dir="auto" data-start="5765" data-end="5780">让 curl 自动跟随重定向。</p> <p dir="auto" data-start="5782" data-end="5795"><strong data-start="5782" data-end="5795">3. 页面显示乱码</strong></p> <p dir="auto" data-start="5797" data-end="5811">先检查响应头中的字符集信息:</p> <pre class="overflow-visible! px-0!" data-start="5813" data-end="5854"><div class="relative w-full mt-4 mb-1"><div class=""><div class="contents"><div class="border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl"><div class="relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback"><div class="select-none sticky z-2 top-(--sticky-padding-top)"><div class="flex w-full items-center justify-between py-1.5 ps-4 pe-1.5 font-sans md:ps-5 bg-(--code-block-surface)"><div class="flex max-w-[75%] min-w-0 cursor-default items-center text-sm font-medium justify-self-start text-token-text-primary"><use href="/cdn/assets/sprites-core-bef2fe87.svg#72c6a7" fill="currentColor"></use>Bash</div><div class="flex flex-row items-center gap-0.5 justify-self-end"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use></div></div></div><div class="pointer-events-none absolute inset-x-4 top-12 bottom-4"><div class="pointer-events-none sticky z-40 shrink-0 z-1!" style="top: calc(calc(var(--sticky-padding-top) + 48px) - 1px * 3); margin-bottom: calc(-4px); height: calc(4px); mask-image: linear-gradient(transparent 25%, white 75%)"><div class="sticky bg-token-border-light" style="top: calc(var(--sticky-padding-top) + 48px); height: 1px"></div></div></div><div class="relative"><div class="h-full min-h-0 min-w-0"><div class="h-full min-h-0 min-w-0" style="width: 100%; transform: none"><div class=""><div class="relative"><div class="" style="transform: none"><div class="relative z-0 flex max-w-full"><div id="code-block-viewer" dir="ltr" class="q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch ͼd ͼr"><div class="cm-scroller"><pre class="cm-content q9tKkq_readonly m-0"><code><span class="ͼl">curl</span><span> </span><span class="ͼn">-I</span><span> https://www.baidu.com</span></code></pre></div></div></div></div></div></div></div></div><div class=""><div class="" style="width: 100%; transform: none"></div></div></div></div></div></div></div></div></pre> <p dir="auto" data-start="5856" data-end="5928">如果服务器返回的内容使用特定编码,而终端编码不匹配,就可能出现乱码。保存 HTML 后,可以再根据实际字符集进行转换或使用支持该编码的解析工具。</p> <p dir="auto" data-start="5930" data-end="5941"><strong data-start="5930" data-end="5941">4. 请求超时</strong></p> <p dir="auto" data-start="5943" data-end="5948">可以设置:</p> <pre class="overflow-visible! px-0!" data-start="5950" data-end="6022"><div class="relative w-full mt-4 mb-1"><div class=""><div class="contents"><div class="border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl"><div class="relative h-full w-full border-radius-3xl bg-(--code-block-surface) corner-superellipse/1.1 overflow-clip rounded-3xl [--code-block-surface:var(--bg-elevated-secondary)] dark:[--code-block-surface:var(--composer-surface-primary)] lxnfua_clipPathFallback"><div class="select-none sticky z-2 top-(--sticky-padding-top)"><div class="flex w-full items-center justify-between py-1.5 ps-4 pe-1.5 font-sans md:ps-5 bg-(--code-block-surface)"><div class="flex max-w-[75%] min-w-0 cursor-default items-center text-sm font-medium justify-self-start text-token-text-primary"><use href="/cdn/assets/sprites-core-bef2fe87.svg#72c6a7" fill="currentColor"></use>Bash</div><div class="flex flex-row items-center gap-0.5 justify-self-end"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use></div></div></div><div class="pointer-events-none absolute inset-x-4 top-12 bottom-4"><div class="pointer-events-none sticky z-40 shrink-0 z-1!" style="top: calc(calc(var(--sticky-padding-top) + 48px) - 1px * 3); margin-bottom: calc(-4px); height: calc(4px); mask-image: linear-gradient(transparent 25%, white 75%)"><div class="sticky bg-token-border-light" style="top: calc(var(--sticky-padding-top) + 48px); height: 1px"></div></div></div><div class="relative"><div class="h-full min-h-0 min-w-0"><div class="h-full min-h-0 min-w-0" style="width: 100%; transform: none"><div class=""><div class="relative"><div class="" style="transform: none"><div class="relative z-0 flex max-w-full"><div id="code-block-viewer" dir="ltr" class="q9tKkq_viewer cm-editor z-10 light:cm-light dark:cm-light flex h-full w-full flex-col items-stretch ͼd ͼr"><div class="cm-scroller"><pre class="cm-content q9tKkq_readonly m-0"><code><span class="ͼl">curl</span><span> </span><span class="ͼn">--connect-timeout</span><span> </span><span class="ͼj">5</span><span> </span><span class="ͼn">--max-time</span><span> </span><span class="ͼj">15</span><span> https://www.baidu.com</span></code></pre></div></div></div></div></div></div></div></div><div class=""><div class="" style="width: 100%; transform: none"></div></div></div></div></div></div></div></div></pre> <p dir="auto" data-start="6024" data-end="6060">同时通过 <code data-start="6029" data-end="6033">-v</code> 判断问题发生在 DNS、连接建立还是服务器响应阶段。</p> <p dir="auto" data-start="6062" data-end="6079"><strong data-start="6062" data-end="6079">5. HTTPS 证书报错</strong></p> <p dir="auto" data-start="6081" data-end="6157">优先检查系统 CA 证书、系统时间和网络代理配置。测试时可以临时使用 <code data-start="6116" data-end="6120">-k</code> 定位问题,但不应依赖关闭证书验证来解决生产环境的 HTTPS 安全问题。</p> <h3 data-section-id="f8c24y" dir="auto" data-start="6159" data-end="6179">curl 请求百度的常用命令汇总</h3> <div dir="auto" class="group TyagGW_tableContainer"><div tabindex="-1" class="TyagGW_tableWrapper flex flex-col-reverse w-fit"><table data-start="6181" data-end="6761" dir="auto" class="w-fit min-w-(--thread-content-width)"><thead data-start="6181" data-end="6192"><tr data-start="6181" data-end="6192"><th data-start="6181" data-end="6186" data-col-size="sm" class="last:pe-10">需求</th><th data-start="6186" data-end="6192" data-col-size="md" class="last:pe-10">命令</th></tr></thead><tbody data-start="6203" data-end="6761"><tr data-start="6203" data-end="6242"><td data-start="6203" data-end="6210" data-col-size="sm">获取网页</td><td data-start="6210" data-end="6242" data-col-size="md"><code data-start="6212" data-end="6240">curl https://www.baidu.com</code></td></tr><tr data-start="6243" data-end="6286"><td data-start="6243" data-end="6251" data-col-size="sm">查看响应头</td><td data-start="6251" data-end="6286" data-col-size="md"><code data-start="6253" data-end="6284">curl -I https://www.baidu.com</code></td></tr><tr data-start="6287" data-end="6333"><td data-start="6287" data-end="6298" data-col-size="sm">查看详细请求过程</td><td data-start="6298" data-end="6333" data-col-size="md"><code data-start="6300" data-end="6331">curl -v https://www.baidu.com</code></td></tr><tr data-start="6334" data-end="6378"><td data-start="6334" data-end="6343" data-col-size="sm">响应头+正文</td><td data-col-size="md" data-start="6343" data-end="6378"><code data-start="6345" data-end="6376">curl -i https://www.baidu.com</code></td></tr><tr data-start="6379" data-end="6423"><td data-start="6379" data-end="6388" data-col-size="sm">自动跟随跳转</td><td data-col-size="md" data-start="6388" data-end="6423"><code data-start="6390" data-end="6421">curl -L https://www.baidu.com</code></td></tr><tr data-start="6424" data-end="6477"><td data-start="6424" data-end="6431" data-col-size="sm">保存网页</td><td data-start="6431" data-end="6477" data-col-size="md"><code data-start="6433" data-end="6475">curl -o baidu.html https://www.baidu.com</code></td></tr><tr data-start="6478" data-end="6554"><td data-start="6478" data-end="6486" data-col-size="sm">获取状态码</td><td data-start="6486" data-end="6554" data-col-size="md"><code data-start="6488" data-end="6552">curl -s -o /dev/null -w "%{http_code} " https://www.baidu.com</code></td></tr><tr data-start="6555" data-end="6633"><td data-start="6555" data-end="6564" data-col-size="sm">查看请求耗时</td><td data-col-size="md" data-start="6564" data-end="6633"><code data-start="6566" data-end="6631">curl -s -o /dev/null -w "%{time_total} " https://www.baidu.com</code></td></tr><tr data-start="6634" data-end="6699"><td data-start="6634" data-end="6650" data-col-size="sm">设置 User-Agent</td><td data-start="6650" data-end="6699" data-col-size="md"><code data-start="6652" data-end="6697">curl -A "Mozilla/5.0" https://www.baidu.com</code></td></tr><tr data-start="6700" data-end="6761"><td data-start="6700" data-end="6709" data-col-size="sm">设置连接超时</td><td data-col-size="md" data-start="6709" data-end="6761"><code data-start="6711" data-end="6759">curl --connect-timeout 5 https://www.baidu.com</code></td></tr></tbody></table><div class="relative h-0 self-end select-none"><div class="absolute end-0 flex items-end" style="height: 33px"><span class="" data-state="closed"><use href="/cdn/assets/sprites-core-bef2fe87.svg#7fccfb" fill="currentColor"></use></span></div></div></div></div> <p dir="auto" data-start="6763" data-end="6921">通过这些参数,可以将 curl 从简单的“网页下载命令”扩展为一个实用的 HTTP 调试工具。请求百度时,通常可以按照“先确认连通性,再查看响应头,最后分析正文”的思路排查问题。若需要对 HTML 中的标题、链接或特定节点进行准确提取,则应将 curl 与专业的 HTML 解析工具结合,而不是单独依赖文本匹配命令。</p> </div> </div> </article> <div class="bg-white rounded-lg shadow-card border border-slate-100 p-5 mb-6"> <div class="flex items-center gap-2 mb-2.5"> <i class="fa-solid fa-tags text-slate-400 text-xs"></i> <span class="text-sm font-semibold text-slate-700">文章标签</span> </div> <div class="flex flex-wrap gap-2"> <a href="https://www.nzw6.cn/tag/标签1" class="bg-slate-50 hover:bg-primary hover:text-white text-slate-600 text-xs px-3 py-1.5 rounded-lg font-medium border border-slate-200 hover:border-primary transition-custom">标签1</a> <a href="https://www.nzw6.cn/tag/标签2" class="bg-slate-50 hover:bg-primary hover:text-white text-slate-600 text-xs px-3 py-1.5 rounded-lg font-medium border border-slate-200 hover:border-primary transition-custom">标签2</a> <a href="https://www.nzw6.cn/tag/标签3" class="bg-slate-50 hover:bg-primary hover:text-white text-slate-600 text-xs px-3 py-1.5 rounded-lg font-medium border border-slate-200 hover:border-primary transition-custom">标签3</a> <a href="https://www.nzw6.cn/tag/标签4" class="bg-slate-50 hover:bg-primary hover:text-white text-slate-600 text-xs px-3 py-1.5 rounded-lg font-medium border border-slate-200 hover:border-primary transition-custom">标签4</a> <a href="https://www.nzw6.cn/tag/标签5" class="bg-slate-50 hover:bg-primary hover:text-white text-slate-600 text-xs px-3 py-1.5 rounded-lg font-medium border border-slate-200 hover:border-primary transition-custom">标签5</a> </div> </div> <style> #prev-next-bar { margin-bottom: 24px; padding: 20px; background: #fff; border: 1px solid #f1f5f9; border-radius: 8px; box-shadow: 0 2px 8px rgba(0, 0, 0, .04); } #prev-next-bar .article-nav { display: flex; gap: 24px; } #prev-next-bar a.article-nav-link { display: flex; align-items: center; gap: 8px; flex: 1 1 0; min-width: 0; margin: 0; padding: 0; color: #475569; font-size: 14px; text-decoration: none; } #prev-next-bar .article-nav-next { justify-content: flex-end; } #prev-next-bar .article-nav-label, #prev-next-bar .article-nav-arrow { flex: 0 0 auto; color: #94a3b8; white-space: nowrap; } #prev-next-bar .article-nav-title { display: block; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } #prev-next-bar a.article-nav-link:hover { color: #2563eb; } @media (max-width: 640px) { #prev-next-bar .article-nav { flex-direction: column; gap: 14px; } #prev-next-bar .article-nav-next { justify-content: flex-start; } } </style> <div id="prev-next-bar"> <div class="article-nav"> <a class="article-nav-link" href="/kaifa/1833.html"> <i class="article-nav-arrow fa-solid fa-chevron-left"></i> <span class="article-nav-label">上一篇:</span> <span class="article-nav-title">解决.NET Framework 4.8与.NET 8项目引用不兼容问题</span> </a> <a class="article-nav-link article-nav-next" href="/kaifa/1835.html"> <span class="article-nav-label">下一篇:</span> <span class="article-nav-title">EBOM、PBOM、MBOM的核心区别与应用场景解析</span> <i class="article-nav-arrow fa-solid fa-chevron-right"></i> </a> </div> </div> <div class="related-recommend bg-white rounded-lg shadow-card border border-slate-100 p-6" style="display:none;"> <div class="flex items-center gap-3 mb-5"> <h3 class="text-base font-semibold text-slate-900">相关文章</h3> <div class="flex-1 h-px bg-slate-100"></div> </div> <div class="space-y-2"> <a href="/kaifa/2084.html" class="flex items-center justify-between p-3 rounded-lg hover:bg-slate-50 transition-custom group"> <span class="text-sm text-slate-600 group-hover:text-primary transition-custom line-clamp-1 flex-1 mr-3">ECharts图表配置技巧:Y轴名称与标记线箭头控制</span> <i class="fa-solid fa-angle-right text-slate-300 group-hover:text-primary transition-custom text-xs"></i> </a> <a href="/kaifa/2082.html" class="flex items-center justify-between p-3 rounded-lg hover:bg-slate-50 transition-custom group"> <span class="text-sm text-slate-600 group-hover:text-primary transition-custom line-clamp-1 flex-1 mr-3">MobaXterm文件上传:SFTP/FTP与自动化脚本实践</span> <i class="fa-solid fa-angle-right text-slate-300 group-hover:text-primary transition-custom text-xs"></i> </a> <a href="/kaifa/2071.html" class="flex items-center justify-between p-3 rounded-lg hover:bg-slate-50 transition-custom group"> <span class="text-sm text-slate-600 group-hover:text-primary transition-custom line-clamp-1 flex-1 mr-3">C/C++中未定义标识符错误排查与解决方案</span> <i class="fa-solid fa-angle-right text-slate-300 group-hover:text-primary transition-custom text-xs"></i> </a> <a href="/kaifa/2053.html" class="flex items-center justify-between p-3 rounded-lg hover:bg-slate-50 transition-custom group"> <span class="text-sm text-slate-600 group-hover:text-primary transition-custom line-clamp-1 flex-1 mr-3">C语言多线程编程:使用pthread库创建与管理线程</span> <i class="fa-solid fa-angle-right text-slate-300 group-hover:text-primary transition-custom text-xs"></i> </a> <a href="/kaifa/2039.html" class="flex items-center justify-between p-3 rounded-lg hover:bg-slate-50 transition-custom group"> <span class="text-sm text-slate-600 group-hover:text-primary transition-custom line-clamp-1 flex-1 mr-3">Tcl脚本中列表遍历与条件判断的实现方法</span> <i class="fa-solid fa-angle-right text-slate-300 group-hover:text-primary transition-custom text-xs"></i> </a> <a href="/kaifa/2026.html" class="flex items-center justify-between p-3 rounded-lg hover:bg-slate-50 transition-custom group"> <span class="text-sm text-slate-600 group-hover:text-primary transition-custom line-clamp-1 flex-1 mr-3">C# FolderBrowserDialog动态定位移动硬盘路径方案</span> <i class="fa-solid fa-angle-right text-slate-300 group-hover:text-primary transition-custom text-xs"></i> </a> </div> </div> <div class="random-recommend bg-white rounded-lg shadow-card border border-slate-100 p-6" style="display:none;"> <div class="flex items-center gap-3 mb-5"> <h3 class="text-base font-semibold text-slate-900">推荐阅读</h3> <div class="flex-1 h-px bg-slate-100"></div> </div> <div class="space-y-2"> <a href="/kaifa/1601.html" class="flex items-center justify-between p-3 rounded-lg hover:bg-slate-50 transition-custom group"> <span class="text-sm text-slate-600 group-hover:text-primary transition-custom line-clamp-1 flex-1 mr-3">Python生成链接时避免重复的技巧</span> <i class="fa-solid fa-angle-right text-slate-300 group-hover:text-primary transition-custom text-xs"></i> </a> <a href="/kaifa/807.html" class="flex items-center justify-between p-3 rounded-lg hover:bg-slate-50 transition-custom group"> <span class="text-sm text-slate-600 group-hover:text-primary transition-custom line-clamp-1 flex-1 mr-3">STM32外部中断配置与中断处理机制详解</span> <i class="fa-solid fa-angle-right text-slate-300 group-hover:text-primary transition-custom text-xs"></i> </a> <a href="/kaifa/861.html" class="flex items-center justify-between p-3 rounded-lg hover:bg-slate-50 transition-custom group"> <span class="text-sm text-slate-600 group-hover:text-primary transition-custom line-clamp-1 flex-1 mr-3">解决npm报错CERT_HAS_EXPIRED:淘宝镜像证书过期问题</span> <i class="fa-solid fa-angle-right text-slate-300 group-hover:text-primary transition-custom text-xs"></i> </a> <a href="/kaifa/644.html" class="flex items-center justify-between p-3 rounded-lg hover:bg-slate-50 transition-custom group"> <span class="text-sm text-slate-600 group-hover:text-primary transition-custom line-clamp-1 flex-1 mr-3">Java中的线程休眠:Thread.sleep()方法详解</span> <i class="fa-solid fa-angle-right text-slate-300 group-hover:text-primary transition-custom text-xs"></i> </a> <a href="/kaifa/848.html" class="flex items-center justify-between p-3 rounded-lg hover:bg-slate-50 transition-custom group"> <span class="text-sm text-slate-600 group-hover:text-primary transition-custom line-clamp-1 flex-1 mr-3">Spring Boot中spring-boot-starter-parent版本升级实践</span> <i class="fa-solid fa-angle-right text-slate-300 group-hover:text-primary transition-custom text-xs"></i> </a> <a href="/kaifa/1875.html" class="flex items-center justify-between p-3 rounded-lg hover:bg-slate-50 transition-custom group"> <span class="text-sm text-slate-600 group-hover:text-primary transition-custom line-clamp-1 flex-1 mr-3">Unity3D开发古代写实风实时战略游戏全攻略</span> <i class="fa-solid fa-angle-right text-slate-300 group-hover:text-primary transition-custom text-xs"></i> </a> </div> </div> </div> <aside class="w-full space-y-5" aria-label="相关推荐"> <a href="/fuwuqi/" class="group relative block min-h-[250px] overflow-hidden rounded-lg border border-red-200/40 bg-[#b51f2c] p-5 text-white shadow-card transition-custom hover:-translate-y-0.5 hover:shadow-card-hover"> <img src="/static/images/server-promo-bg.svg" alt="" aria-hidden="true" loading="lazy" decoding="async" class="absolute inset-0 h-full w-full object-cover"> <div class="absolute inset-0 bg-gradient-to-r from-[#741126]/95 via-[#a71927]/82 to-[#d7352a]/20"></div> <div class="relative z-10 flex min-h-[210px] flex-col"> <div class="mb-3 text-sm font-semibold text-amber-100">阿里云 · 腾讯云</div> <h3 class="text-2xl font-bold leading-tight">云服务器特惠</h3> <div class="my-3 flex items-end gap-1 text-[#ffe179]"> <span class="pb-1 text-lg font-bold">¥</span> <strong class="text-[38px] leading-none tracking-tight">99</strong> <span class="pb-1 text-sm font-semibold text-white">/年起</span> </div> <p class="text-base font-medium text-red-50">国内、香港及海外机型可选</p> <span class="mt-auto inline-flex w-fit items-center gap-2 rounded-lg bg-white px-4 py-2.5 text-base font-bold text-[#a71927] shadow-md transition-custom group-hover:bg-[#fff4d6]"> 查看优惠 <i class="fa-solid fa-arrow-right text-xs transition-transform group-hover:translate-x-1"></i> </span> </div> </a> <div class="bg-white rounded-lg shadow-card border border-slate-100 p-5"> <div class="flex items-center gap-3 mb-4"> <h3 class="text-sm font-semibold text-slate-900">推荐模板</h3> <div class="flex-1 h-px bg-slate-100"></div> </div> <div class="space-y-2.5"> <a href="/templates/1103.html" class="flex items-center gap-3 p-2 rounded-lg hover:bg-slate-50 transition-custom group"> <div class="w-14 h-14 shrink-0 rounded-lg overflow-hidden"> <img src="/static/upload/image/20260902/s736.webp" alt="律师事务所网站模板整站源码" width="56" height="56" loading="lazy" decoding="async" class="w-full h-full object-cover"> </div> <div class="flex-1 min-w-0"> <div class="text-[14px] text-slate-700 group-hover:text-primary transition-custom line-clamp-2 font-medium leading-snug">律师事务所网站模板整站源码</div> <div class="text-xs text-red-500 font-bold mt-1">¥80</div> </div> </a> <a href="/templates/1102.html" class="flex items-center gap-3 p-2 rounded-lg hover:bg-slate-50 transition-custom group"> <div class="w-14 h-14 shrink-0 rounded-lg overflow-hidden"> <img src="/static/upload/image/20260902/s735.webp" alt="财务财税企业记账网站模板整站源码" width="56" height="56" loading="lazy" decoding="async" class="w-full h-full object-cover"> </div> <div class="flex-1 min-w-0"> <div class="text-[14px] text-slate-700 group-hover:text-primary transition-custom line-clamp-2 font-medium leading-snug">财务财税企业记账网站模板整站源码</div> <div class="text-xs text-red-500 font-bold mt-1">¥80</div> </div> </a> <a href="/templates/1101.html" class="flex items-center gap-3 p-2 rounded-lg hover:bg-slate-50 transition-custom group"> <div class="w-14 h-14 shrink-0 rounded-lg overflow-hidden"> <img src="/static/upload/image/20260902/s734.webp" alt="招标投标信息网站模板整站源码" width="56" height="56" loading="lazy" decoding="async" class="w-full h-full object-cover"> </div> <div class="flex-1 min-w-0"> <div class="text-[14px] text-slate-700 group-hover:text-primary transition-custom line-clamp-2 font-medium leading-snug">招标投标信息网站模板整站源码</div> <div class="text-xs text-red-500 font-bold mt-1">¥80</div> </div> </a> <a href="/templates/1100.html" class="flex items-center gap-3 p-2 rounded-lg hover:bg-slate-50 transition-custom group"> <div class="w-14 h-14 shrink-0 rounded-lg overflow-hidden"> <img src="/static/upload/image/20260902/s733.webp" alt="机电设备网站模板 - 产品带视频 带三级栏目" width="56" height="56" loading="lazy" decoding="async" class="w-full h-full object-cover"> </div> <div class="flex-1 min-w-0"> <div class="text-[14px] text-slate-700 group-hover:text-primary transition-custom line-clamp-2 font-medium leading-snug">机电设备网站模板 - 产品带视频 带三级栏目</div> <div class="text-xs text-red-500 font-bold mt-1">¥80</div> </div> </a> <a href="/templates/1099.html" class="flex items-center gap-3 p-2 rounded-lg hover:bg-slate-50 transition-custom group"> <div class="w-14 h-14 shrink-0 rounded-lg overflow-hidden"> <img src="/static/upload/image/20260821/1787310069259663.webp" alt="LED照明灯具网站模板整站源码 - 带三级栏目" width="56" height="56" loading="lazy" decoding="async" class="w-full h-full object-cover"> </div> <div class="flex-1 min-w-0"> <div class="text-[14px] text-slate-700 group-hover:text-primary transition-custom line-clamp-2 font-medium leading-snug">LED照明灯具网站模板整站源码 - 带三级栏目</div> <div class="text-xs text-red-500 font-bold mt-1">¥80</div> </div> </a> </div> </div> <div class="bg-white rounded-lg shadow-card border border-slate-100 p-5"> <div class="flex items-center gap-3 mb-4"> <h3 class="text-sm font-semibold text-slate-900">推荐服务</h3> <div class="flex-1 h-px bg-slate-100"></div> </div> <div class="space-y-2"> <a href="/service/33.html" class="flex items-center justify-between p-2.5 rounded-lg hover:bg-slate-50 transition-custom group"> <span class="text-sm text-slate-600 group-hover:text-primary transition-custom line-clamp-1 flex-1 mr-2 font-medium">PbootCMS被黑恢复与安全加固</span> <i class="fa-solid fa-angle-right text-slate-300 group-hover:text-primary transition-custom text-xs"></i> </a> <a href="/service/1143.html" class="flex items-center justify-between p-2.5 rounded-lg hover:bg-slate-50 transition-custom group"> <span class="text-sm text-slate-600 group-hover:text-primary transition-custom line-clamp-1 flex-1 mr-2 font-medium">WordPress插件开发定制</span> <i class="fa-solid fa-angle-right text-slate-300 group-hover:text-primary transition-custom text-xs"></i> </a> <a href="/service/1172.html" class="flex items-center justify-between p-2.5 rounded-lg hover:bg-slate-50 transition-custom group"> <span class="text-sm text-slate-600 group-hover:text-primary transition-custom line-clamp-1 flex-1 mr-2 font-medium">协会官网制作</span> <i class="fa-solid fa-angle-right text-slate-300 group-hover:text-primary transition-custom text-xs"></i> </a> <a href="/service/1222.html" class="flex items-center justify-between p-2.5 rounded-lg hover:bg-slate-50 transition-custom group"> <span class="text-sm text-slate-600 group-hover:text-primary transition-custom line-clamp-1 flex-1 mr-2 font-medium">证书查询系统开发</span> <i class="fa-solid fa-angle-right text-slate-300 group-hover:text-primary transition-custom text-xs"></i> </a> <a href="/service/1231.html" class="flex items-center justify-between p-2.5 rounded-lg hover:bg-slate-50 transition-custom group"> <span class="text-sm text-slate-600 group-hover:text-primary transition-custom line-clamp-1 flex-1 mr-2 font-medium">酒店预订小程序开发</span> <i class="fa-solid fa-angle-right text-slate-300 group-hover:text-primary transition-custom text-xs"></i> </a> </div> </div> </aside> </div> </div> </section> </main> <footer class="bg-dark text-slate-400 py-8 lg:py-10"> <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8"> <div class="grid grid-cols-2 md:grid-cols-4 gap-6 lg:gap-8 mb-6"> <div class="col-span-2 md:col-span-1"> <div class="mb-4"> <img src="/2022022108032469.png" alt="牛站云"/> </div> <p class="text-[15px] leading-relaxed text-slate-400">牛站云专注于互联网数字化服务,提供网站建设、APP开发、小程序开发、系统定制、功能插件及云服务器等产品与服务,助力企业和个人快速开展线上业务。</p> </div> <div> <h4 class="text-[15px] font-semibold text-white mb-4">技术方案</h4> <ul class="space-y-3 text-[15px]"> <li><a href="/solutions/index.html" class="hover:text-white transition-colors duration-200">SEO 优化服务</a></li> <li><a href="/wangzhanjianshe/" class="hover:text-white transition-colors duration-200 line-clamp-1">国内企业官网建设整体解决方案</a></li> <li><a href="/waimaodulizhan/" class="hover:text-white transition-colors duration-200 line-clamp-1">外贸独立站英文多语言营销型官网建设</a></li> <li><a href="/xitongdingzhi/" class="hover:text-white transition-colors duration-200 line-clamp-1">企业业务系统定制</a></li> <li><a href="/xiangmuweihu/" class="hover:text-white transition-colors duration-200 line-clamp-1">已有项目接手与升级</a></li> </ul> </div> <div> <h4 class="text-[15px] font-semibold text-white mb-4">推荐阅读</h4> <ul class="space-y-3 text-[15px]"> <li><a href="/kaifa/1631.html" class="hover:text-white transition-colors duration-200 line-clamp-1">MyBatis XML中SQL特殊符号(大于、小于等)的正确写法</a></li> <li><a href="/kaifa/1409.html" class="hover:text-white transition-colors duration-200 line-clamp-1">解决ValueError: Columns must be same length as key错误</a></li> <li><a href="/kaifa/869.html" class="hover:text-white transition-colors duration-200 line-clamp-1">Angular中父组件触发子组件刷新的实现方法</a></li> <li><a href="/kaifa/2090.html" class="hover:text-white transition-colors duration-200 line-clamp-1">Python pip本地安装:离线包与本地目录安装全流程解析</a></li> <li><a href="/kaifa/1756.html" class="hover:text-white transition-colors duration-200 line-clamp-1">Java中iText库BaseFont.createFont()字体设置详解</a></li> </ul> </div> <div class="col-span-2 md:col-span-1"> <h4 class="text-[15px] font-semibold text-white mb-4">客服微信</h4> <div class="flex gap-4"> <div class="flex-1 text-center"> <div class="bg-white rounded-lg p-2 mb-2"> <img src="/static/upload/image/20260713/1783949484149422.png" alt="定制开发" class="w-full aspect-square object-contain"> </div> <span class="text-sm text-slate-500">定制开发</span> </div> <div class="flex-1 text-center"> <div class="bg-white rounded-lg p-2 mb-2"> <img src="/static/upload/image/20260713/1783949541877769.png" alt="业务咨询" class="w-full aspect-square object-contain"> </div> <span class="text-sm text-slate-500">业务咨询</span> </div> </div> </div> </div> <div class="border-t border-slate-700/60 pt-8 flex flex-col md:flex-row justify-between items-center gap-4"> <p class="text-sm text-slate-500">© 2026 牛站云. 保留所有权利。</p> <div class="flex flex-wrap items-center gap-x-6 gap-y-2 text-sm text-slate-500"> <a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank" class="hover:text-white transition-colors duration-200">闽ICP备18018384号-6</a> <span>闽公网安备35020602002865号</span> <a href="https://www.nzw6.cn/privacy.html" class="hover:text-white transition-colors duration-200">隐私政策</a> <a href="https://www.nzw6.cn/terms.html" class="hover:text-white transition-colors duration-200">服务条款</a> <a href="https://www.nzw6.cn/cookies.html" class="hover:text-white transition-colors duration-200">Cookie 政策</a> </div> </div> </div> </footer> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?46998839740903d8bc8aa57f51751056"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> <script> document.addEventListener('DOMContentLoaded', function() { document.querySelectorAll('.article-content pre').forEach(function(pre) { if (pre.querySelector('.code-copy-btn')) return; // 已有则跳过 var btn = document.createElement('button'); btn.className = 'code-copy-btn'; btn.setAttribute('title', '复制代码'); btn.innerHTML = '<i class="fa-regular fa-copy"></i>'; btn.addEventListener('click', function() { var code = pre.querySelector('code') ? pre.querySelector('code') : pre; var text = code.textContent || code.innerText; var success = function() { btn.innerHTML = '<i class="fa-solid fa-check"></i>'; btn.classList.add('copied'); setTimeout(function() { btn.innerHTML = '<i class="fa-regular fa-copy"></i>'; btn.classList.remove('copied'); }, 2000); }; if (navigator.clipboard && navigator.clipboard.writeText) { navigator.clipboard.writeText(text).then(success); } else { var textarea = document.createElement('textarea'); textarea.value = text; textarea.style.cssText = 'position:fixed;left:-9999px'; document.body.appendChild(textarea); textarea.select(); document.execCommand('copy'); document.body.removeChild(textarea); success(); } }); pre.appendChild(btn); }); var prevLink = document.querySelector('#prev-next-bar .prev-link'); var prevEmpty = document.querySelector('#prev-next-bar .prev-empty'); if (prevLink) { var ptxt = prevLink.textContent.replace('上一篇', '').trim(); if (!ptxt || /[\[\{<]/.test(ptxt)) { prevLink.style.display = 'none'; if (prevEmpty) prevEmpty.style.display = ''; } } var nextLink = document.querySelector('#prev-next-bar .next-link'); var nextEmpty = document.querySelector('#prev-next-bar .next-empty'); if (nextLink) { var ntxt = nextLink.textContent.replace('下一篇', '').trim(); if (!ntxt || /[\[\{<]/.test(ntxt)) { nextLink.style.display = 'none'; if (nextEmpty) nextEmpty.style.display = ''; } } var relatedBox = document.querySelector('.related-recommend'); var randomBox = document.querySelector('.random-recommend'); if (relatedBox && randomBox) { var links = relatedBox.querySelectorAll('a[href]'); var validCount = 0; for (var i = 0; i < links.length; i++) { var linkText = links[i].textContent.trim(); if (linkText && linkText.indexOf('[list:title]') === -1 && linkText.indexOf('{') === -1) { validCount++; } } if (validCount > 1) { relatedBox.style.display = ''; } else { randomBox.style.display = ''; } } }); </script> <script src='/Spider/?url=%2Fkaifa%2F1834.html' async='async'></script> <script src='/?p=/Do/visits/id/1834' async='async'></script> </body> </html>