<!DOCTYPE html><html lang="zh" data-theme="dark"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><title>IBM 全栈开发【7】:数据科学的 Python 基础 · Cytrogen 的个人博客</title><meta name="description" content="本文是 IBM 全栈开发课程的第七篇学习笔记,重点介绍了 Python 在数据科学领域的应用基础。笔记围绕两个核心库 Pandas 和 NumPy,讲解了如何使用 DataFrame 进行数据处理、如何利用多维数组进行高效的数值与矩阵运算。此外,文章还涵盖了数据收集技术,包括调用 REST API、使用 requests 库发送 HTTP 请求,以及通过 Beautiful Soup 进行网页抓取等。这篇笔记为使用 Python 进行数据分析和获取的初学者提供了清晰的入门路径。"><link rel="icon" href="../favicon.png"><link rel="canonical" href="https://cytrogen.icu/posts/b1c6.html"><link rel="webmention" href="https://webmention.io/cytrogen.icu/webmention"><link rel="me" href="https://m.otter.homes/@Cytrogen"><link rel="me" href="https://github.com/cytrogen"><meta name="fediverse:creator" content="@Cytrogen@m.otter.homes"><link rel="preload" href="../fonts/opensans-regular-latin.woff2" as="font" type="font/woff2" crossorigin="anonymous"><style>@font-face {
font-family: 'Open Sans';
src: url('../fonts/opensans-regular-latin.woff2') format('woff2');
font-weight: 400;
font-style: normal;
font-display: swap;
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
size-adjust: 107%;
ascent-override: 97%;
descent-override: 25%;
line-gap-override: 0%;
}
</style><script>(function() {
try {
// 优先级:用户选择 > 系统偏好 > 默认浅色
const saved = localStorage.getItem('theme');
const theme = saved ||
(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
document.documentElement.setAttribute('data-theme', theme);
document.documentElement.style.colorScheme = theme;
} catch (error) {
// 失败时使用默认主题,不阻塞渲染
document.documentElement.setAttribute('data-theme', 'light');
}
})();
</script><link rel="stylesheet" href="../css/ares.css"><script data-netlify-skip-bundle="true">(function() {
document.addEventListener('DOMContentLoaded', function() {
const theme = document.documentElement.getAttribute('data-theme');
const pageWrapper = document.getElementById('page-wrapper');
if (pageWrapper && theme) {
pageWrapper.setAttribute('data-theme', theme);
}
});
})();
</script><!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-math@4.0.0/dist/style.css">
<!-- hexo injector head_end end --><meta name="generator" content="Hexo 8.1.1"><link rel="alternate" href="atom.xml" title="Cytrogen 的个人博客" type="application/atom+xml">
</head><body><div id="page-wrapper"><a class="skip-link" href="#main-content">跳到主要内容</a><div class="wrap"><header><a class="logo-link" href="../index.html"><img src="../favicon.png" alt="logo"></a><div class="h-card visually-hidden"><img class="u-photo" src="https://cytrogen.icu/favicon.png" alt="Cytrogen"><a class="p-name u-url u-uid" href="https://cytrogen.icu">Cytrogen</a><p class="p-note">Cytrogen 的个人博客,Cytrogen's Blog</p><a class="u-url" rel="me noopener" target="_blank" href="https://m.otter.homes/@Cytrogen">Mastodon</a><a class="u-url" rel="me noopener" target="_blank" href="https://github.com/cytrogen">GitHub</a></div><nav class="site-nav"><div class="nav-main"><div class="nav-primary"><ul class="nav-list hidden-mobile"><li class="nav-item"><a class="nav-link" href="../index.html">首页</a></li></ul><div class="nav-tools"><div class="language-menu"><button class="language-toggle" type="button"><svg class="icon icon-globe" width="16" height="16" viewBox="0 0 16 16" fill="currentColor" aria-hidden="true" focusable="false"><path d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8zm7.5-6.923c-.67.204-1.335.82-1.887 1.855A7.97 7.97 0 0 0 5.145 4H7.5V1.077zM4.09 4a9.267 9.267 0 0 1 .64-1.539 6.7 6.7 0 0 1 .597-.933A7.025 7.025 0 0 0 2.255 4H4.09zm-.582 3.5c.03-.877.138-1.718.312-2.5H1.674a6.958 6.958 0 0 0-.656 2.5h2.49zM4.847 5a12.5 12.5 0 0 0-.338 2.5H7.5V5H4.847zM8.5 5v2.5h2.99a12.495 12.495 0 0 0-.337-2.5H8.5zM4.51 8.5a12.5 12.5 0 0 0 .337 2.5H7.5V8.5H4.51zm3.99 0V11h2.653c.187-.765.306-1.608.338-2.5H8.5zM5.145 12c.138.386.295.744.468 1.068.552 1.035 1.218 1.65 1.887 1.855V12H5.145zm.182 2.472a6.696 6.696 0 0 1-.597-.933A9.268 9.268 0 0 1 4.09 12H2.255a7.024 7.024 0 0 0 3.072 2.472zM3.82 11a13.652 13.652 0 0 1-.312-2.5h-2.49c.062.89.291 1.733.656 2.5H3.82zm6.853 3.472A7.024 7.024 0 0 0 13.745 12H11.91a9.27 9.27 0 0 1-.64 1.539 6.688 6.688 0 0 1-.597.933zM8.5 12v2.923c.67-.204 1.335-.82 1.887-1.855A7.97 7.97 0 0 0 10.855 12H8.5zm3.68-1h2.146c.365-.767.594-1.61.656-2.5h-2.49a13.65 13.65 0 0 1-.312 2.5zm2.802-3.5a6.959 6.959 0 0 0-.656-2.5H12.18c.174.782.282 1.623.312 2.5h2.49zM11.27 2.461c.247.464.462.98.64 1.539h1.835a7.024 7.024 0 0 0-3.072-2.472c.218.284.418.598.597.933zM10.855 4a7.966 7.966 0 0 0-.468-1.068C9.835 1.897 9.17 1.282 8.5 1.077V4h2.355z"></path></svg><span>中文</span></button><div class="language-dropdown"></div></div></div><div class="nav-controls"><div class="more-menu hidden-mobile"><button class="more-toggle" type="button"><span>更多</span><svg class="icon icon-chevron-down" width="12" height="12" viewBox="0 0 12 12" fill="currentColor" aria-hidden="true" focusable="false"><path d="M6 8.825c-.2 0-.4-.1-.5-.2l-3.3-3.3c-.3-.3-.3-.8 0-1.1s.8-.3 1.1 0l2.7 2.7 2.7-2.7c.3-.3.8-.3 1.1 0s.3.8 0 1.1l-3.3 3.3c-.1.1-.3.2-.5.2z"></path></svg></button><div class="more-dropdown"><ul class="dropdown-list"><li class="dropdown-item"><a class="nav-link" href="../archives/index.html">归档</a></li><li class="dropdown-item"><a class="nav-link" href="../categories/index.html">分类</a></li><li class="dropdown-item"><a class="nav-link" href="../tags/index.html">标签</a></li><li class="dropdown-item"><a class="nav-link" href="../about/index.html">关于</a></li><li class="dropdown-item"><a class="nav-link" href="../sitemap/index.html">领地地图</a></li></ul></div></div><div class="theme-switcher"><button class="theme-toggle" type="button" role="switch" aria-pressed="false" aria-label="切换主题"><div class="theme-icon moon-icon"><svg class="icon icon-moon" width="16" height="16" viewBox="0 0 16 16" fill="currentColor" aria-hidden="true" focusable="false"><path d="M6 .278a.768.768 0 0 1 .08.858 7.208 7.208 0 0 0-.878 3.46c0 4.021 3.278 7.277 7.318 7.277.527 0 1.04-.055 1.533-.16a.787.787 0 0 1 .81.316.733.733 0 0 1-.031.893A8.349 8.349 0 0 1 8.344 16C3.734 16 0 12.286 0 7.71 0 4.266 2.114 1.312 5.124.06A.752.752 0 0 1 6 .278z"></path></svg></div><div class="theme-icon sun-icon"><svg class="icon icon-sun" width="16" height="16" viewBox="0 0 16 16" fill="currentColor" aria-hidden="true" focusable="false"><path d="M8 11a3 3 0 1 1 0-6 3 3 0 0 1 0 6zm0 1a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM8 0a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 0zm0 13a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 13zm8-5a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2a.5.5 0 0 1 .5.5zM3 8a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2A.5.5 0 0 1 3 8zm10.657-5.657a.5.5 0 0 1 0 .707l-1.414 1.415a.5.5 0 1 1-.707-.708l1.414-1.414a.5.5 0 0 1 .707 0zm-9.193 9.193a.5.5 0 0 1 0 .707L3.05 13.657a.5.5 0 0 1-.707-.707l1.414-1.414a.5.5 0 0 1 .707 0zm9.193 2.121a.5.5 0 0 1-.707 0l-1.414-1.414a.5.5 0 0 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .707zM4.464 4.465a.5.5 0 0 1-.707 0L2.343 3.05a.5.5 0 1 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .708z"></path></svg></div></button></div><details class="mobile-menu-details hidden-desktop"><summary class="hamburger-menu" aria-label="nav.menu"><svg class="icon icon-bars" width="16" height="16" viewBox="0 0 16 16" fill="currentColor" aria-hidden="true" focusable="false"><path d="M2.5 12a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5zm0-4a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5zm0-4a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5z"></path></svg><span class="menu-text">nav.menu</span></summary><div class="mobile-menu-dropdown"><ul class="mobile-nav-list"><li class="mobile-nav-item"><a class="mobile-nav-link" href="../index.html">首页</a></li><li class="mobile-nav-item"><a class="mobile-nav-link" href="../archives/index.html">归档</a></li><li class="mobile-nav-item"><a class="mobile-nav-link" href="../categories/index.html">分类</a></li><li class="mobile-nav-item"><a class="mobile-nav-link" href="../tags/index.html">标签</a></li><li class="mobile-nav-item"><a class="mobile-nav-link" href="../about/index.html">关于</a></li><li class="mobile-nav-item"><a class="mobile-nav-link" href="../sitemap/index.html">领地地图</a></li></ul></div></details></div></div></div></nav></header><main class="container" id="main-content" tabindex="-1"><div class="post"><article class="post-block h-entry"><div class="post-meta p-author h-card visually-hidden"><img class="author-avatar u-photo" src="../favicon.png" alt="Cytrogen"><span class="p-name">Cytrogen</span><a class="u-url" href="https://cytrogen.icu">https://cytrogen.icu</a></div><a class="post-permalink u-url u-uid visually-hidden" href="https://cytrogen.icu/posts/b1c6.html">永久链接</a><div class="p-summary visually-hidden"><p>近期在学习 IBM 全栈应用开发微学士课程,故此记录学习笔记。</p></div><div class="visually-hidden"><a class="p-category" href="../categories/%E7%BC%96%E7%A8%8B%E7%AC%94%E8%AE%B0/">编程笔记</a><a class="p-category" href="../tags/Python/">Python</a><a class="p-category" href="../tags/IBM/">IBM</a></div><h1 class="post-title p-name">IBM 全栈开发【7】:数据科学的 Python 基础</h1><div class="post-info"><time class="post-date dt-published" datetime="2024-05-18T04:27:00.000Z">5/18/2024</time><time class="dt-updated visually-hidden" datetime="2026-02-09T17:16:54.717Z"></time></div><div class="post-content e-content"><html><head></head><body><p>近期在学习 IBM 全栈应用开发微学士课程,故此记录学习笔记。</p>
<span id="more"></span>
<h1 id="1-python基础"><a class="markdownIt-Anchor" href="#1-python基础"></a> 1. Python 基础</h1>
<h1 id="2-python数据结构"><a class="markdownIt-Anchor" href="#2-python数据结构"></a> 2. Python 数据结构</h1>
<h1 id="3-python编程基础"><a class="markdownIt-Anchor" href="#3-python编程基础"></a> 3. Python 编程基础</h1>
<h1 id="4-用python处理数据"><a class="markdownIt-Anchor" href="#4-用python处理数据"></a> 4. 用 Python 处理数据</h1>
<h2 id="41-pandas"><a class="markdownIt-Anchor" href="#41-pandas"></a> 4.1. Pandas</h2>
<p><code>Pandas</code> 是一个功能强大的 Python 库,用于数据处理和分析,提供数据结构和函数来处理结构化数据,如数据帧和序列。</p>
<ul>
<li>可以使用 <code>import</code> 命令导入文件,然后输入文件名。</li>
<li>使用 <code>as</code> 命令可以为文件提供一个更短的名称。</li>
<li>在 Pandas 中,使用数据帧(<code>df</code>)来指定要读取的文件。</li>
<li>数据帧由行和列组成。</li>
<li>可以使用特定 <code>DataFrame</code> 的一列或多列来创建新的 <code>DataFrame</code>。</li>
<li>我们可以处理 <code>DataFrames</code> 中的数据,并将结果保存为不同的格式。</li>
<li>在 Python 中,可以使用 <code>Unique</code> 方法来确定 <code>DataFrames</code> 列中的唯一元素。</li>
<li>您可以使用不等式运算符和 <code>df</code>,为 <code>DataFrames</code> 中选定的列分配一个布尔值。</li>
<li>将新的 <code>DataFrame</code> 保存为不同的 <code>DataFrame</code>,其中可能包含先前 <code>DataFrame</code> 中的值。</li>
</ul>
<h2 id="42-numpy"><a class="markdownIt-Anchor" href="#42-numpy"></a> 4.2. NumPy</h2>
<p><code>NumPy</code> 是一个用于数值和矩阵运算的 Python 库,提供多维数组对象和各种数学函数,可高效处理数据。</p>
<ul>
<li><code>NumPy</code> 是 <code>Pandas</code> 的基础。</li>
<li><code>NumPy</code> 数组或 <code>ND</code> 数组类似于列表,通常具有固定大小和同类元素。</li>
</ul>
<p>一维 <code>NumPy</code> 数组是具有单轴的元素线性序列,就像传统的列表,但针对数值计算和数组操作进行了优化。</p>
<ul>
<li>可以使用索引访问 <code>NumPy</code> 数组中的元素。</li>
<li>使用属性 <code>dtype</code> 可以获取数组元素的数据类型。</li>
<li>你可以使用 <code>nsize</code> 和 <code>ndim</code> 分别获取数组的大小和维度。</li>
<li>可以在 <code>NumPy</code> 中使用索引和切片方法。</li>
<li>向量加法是 Python 中广泛使用的操作。</li>
<li>用线段或箭头来表示向量加法是非常有用的。</li>
<li><code>NumPy</code> 代码的运行速度更快,这对处理大量数据很有帮助。</li>
<li>用负号代替加号,可以执行向量减法。</li>
<li>在 Python 中,数组与标量相乘需要将数组中的每个元素与标量值相乘,从而得到一个新数组,其中的每个元素都按标量缩放。</li>
<li><code>Hadamard</code> 积是指两个相同形状的数组按元素相乘,得到一个新数组,其中每个元素都是输入数组中相应元素的乘积。</li>
<li>Python 中的点乘是两个数组的元素乘积之和,通常用于向量和矩阵操作,以找到相应元素相乘并求和的标量结果。</li>
<li>在使用 <code>NumPy</code> 时,通常会使用 <code>Matplotlib</code> 等库来从存储在 NumPy 数组中的数值数据创建图表和可视化效果。</li>
</ul>
<p>二维 <code>NumPy</code> 数组是一种具有行和列的网格状结构,适合以矩阵或表格的形式表示数据,用于数值计算。</p>
<ul>
<li>在 <code>NumPy</code> 中,<code>shape</code> 指的是数组的维数(行列数),表示数组的大小和结构。</li>
<li>使用属性 <code>size</code> 可以获得数组的大小。</li>
<li>使用矩形属性可以访问数组中的各种元素。</li>
<li>在 <code>NumPy</code> 中使用标量对元素进行乘法运算。</li>
</ul>
<h1 id="5-api和数据收集"><a class="markdownIt-Anchor" href="#5-api和数据收集"></a> 5. API 和数据收集</h1>
<h2 id="51-api"><a class="markdownIt-Anchor" href="#51-api"></a> 5.1. API</h2>
<p>Python 中的简单 API 是应用程序编程接口,它提供了与服务、库或数据交互的简单易用的方法,通常只需最少的配置或复杂度。<br>
API 使两个软件可以相互对话。</p>
<p>在 Python 中使用 API 库需要导入该库,调用其函数或方法来发出 HTTP 请求,并解析响应以访问 API 提供的数据或服务。<br>
<code>Pandas</code> API 通过与其他软件组件通信来处理数据。</p>
<p>当你创建一个字典,然后使用 <code>DataFrames</code> 构造函数创建一个 <code>Pandas</code> 对象时,实例就形成了。</p>
<p>方法 <code>head</code> 将从 <code>DataFrames</code> 的顶部(默认为 <code>5</code>)显示所提及的行数,而方法 <code>means</code> 将计算平均值并返回值。</p>
<h4 id="511-rest-api"><a class="markdownIt-Anchor" href="#511-rest-api"></a> 5.1.1. REST API</h4>
<p>REST API 允许通过互联网进行通信,利用存储、访问更多数据、人工智能算法等资源。</p>
<ul>
<li><code>HTTP</code> 方法通过互联网传输数据。</li>
<li>HTTP 消息通常包含一个 JSON 文件,其中包含操作指令。</li>
<li>包含 JSON 文件的 HTTP 消息会作为网络服务的响应返回给客户端。</li>
<li>处理时间序列数据需要使用 <code>Pandas</code> 时间序列函数。</li>
<li>您可以获取每日蜡烛图数据,并使用 <code>Plotly</code> 绘制蜡烛图。</li>
</ul>
<h4 id="512-http"><a class="markdownIt-Anchor" href="#512-http"></a> 5.1.2. HTTP</h4>
<p>HTTP(超文本传输协议)在客户端(网络浏览器)和万维网服务器之间传输数据,包括网页和资源。</p>
<ul>
<li>HTTP 协议可能包括多种类型的 REST API。</li>
<li>HTTP 响应包括资源类型、资源长度等信息。</li>
<li>统一资源定位符(URL)是在网络上查找资源的最常用方法。</li>
<li>URL 分为三个部分:方案、互联网地址或基本 URL 和路径。</li>
<li><code>GET</code> 方法是请求信息的常用方法之一。其他一些方法也可能包含正文。</li>
<li>响应方法包含响应的版本和正文。</li>
<li><code>POST</code> 向服务器提交数据,<code>PUT</code> 更新服务器上已有的数据,<code>DELETE</code> 从服务器删除数据。</li>
</ul>
<h4 id="513-requests"><a class="markdownIt-Anchor" href="#513-requests"></a> 5.1.3. <code>Requests</code></h4>
<p><code>Requests</code> 是一个 Python 库,可以轻松发送 HTTP / 1.1 请求。</p>
<ul>
<li>可以使用 <code>GET</code> 方法修改查询结果。</li>
<li>可以用一个 Query 字符串从一个 URL 获取多个请求,如名称、ID 等。</li>
</ul>
<h4 id="514-爬虫"><a class="markdownIt-Anchor" href="#514-爬虫"></a> 5.1.4. 爬虫</h4>
<p>Python 中的网络抓取包括从网站中提取和解析数据,以便使用 <code>Beautiful Soup</code> 和 <code>requests</code> 等库为各种应用程序收集信息。</p>
<ul>
<li>HTML 包含由角括号(称为标签)括起来的蓝色文本元素所包围的文本。</li>
<li>Python 中的每个标签名称都是一个类,每个标签都是一个实例。</li>
<li>您可以选择网页上的 HTML 元素来查看网页。</li>
<li>网页可能还包含 CSS 和 JavaScript 以及 HTML 元素。</li>
<li>每个 HTML 文档就像一个 HTML 树,其中可能包含字符串和其他标记。</li>
<li>每个 HTML 表格都有表格标记,并定义了行、页眉、正文等。</li>
</ul>
</body></html></div></article></div></main><footer><div class="paginator"><a class="prev" href="224a.html">上一篇</a><a class="next" href="b5ac.html">下一篇</a></div><!-- Webmention 显示区域--><div class="webmention-section webmention-empty" data-page-url="posts/b1c6.html" data-full-url="https://cytrogen.icu/posts/b1c6.html" data-mode="static">
<h3 class="webmention-title">Webmentions (<span class="webmention-count">0</span>)</h3>
<div class="webmention-list"></div>
<span>暂无 Webmentions</span>
</div><div class="copyright"><p class="footer-links"><a href="../friends/index.html">友链</a><span class="footer-separator"> ·</span><a href="../links/index.html">邻邦</a><span class="footer-separator"> ·</span><a href="../contact/index.html">联络</a><span class="footer-separator"> ·</span><a href="../colophon/index.html">营造记</a><span class="footer-separator"> ·</span><a href="../atom.xml">RSS订阅</a></p><p>© 2025 - 2026 <a href="https://cytrogen.icu">Cytrogen</a>, powered by <a href="https://hexo.io/" target="_blank">Hexo</a> and <a href="https://github.com/cytrogen/hexo-theme-ares" target="_blank">hexo-theme-ares</a>.</p><p><a href="https://blogscn.fun" target="_blank" rel="noopener">BLOGS·CN</a></p></div></footer></div></div><a class="back-to-top" href="#top" aria-label="返回顶部"><svg width="20" height="20" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true"><path d="M3.293 9.707a1 1 0 010-1.414L9.586 2a2 2 0 012.828 0l6.293 6.293a1 1 0 01-1.414 1.414L11 3.414V17a1 1 0 11-2 0V3.414L2.707 9.707a1 1 0 01-1.414 0z"></path></svg></a><script>document.addEventListener('DOMContentLoaded', function() {
const codeBlocks = document.querySelectorAll('figure.highlight');
codeBlocks.forEach(block => {
let caption = block.querySelector('figcaption');
if (!caption) {
caption = document.createElement('figcaption');
block.insertBefore(caption, block.firstChild);
}
const info = document.createElement('div');
info.className = 'info';
const filename = caption.querySelector('span');
if (filename) {
filename.className = 'filename';
info.appendChild(filename);
}
const lang = block.className.split(' ')[1];
if (lang) {
const langSpan = document.createElement('span');
langSpan.className = 'lang-name';
langSpan.textContent = lang;
info.appendChild(langSpan);
}
const sourceLink = caption.querySelector('a');
if (sourceLink) {
sourceLink.className = 'source-link';
info.appendChild(sourceLink);
}
const actions = document.createElement('div');
actions.className = 'actions';
const codeHeight = block.scrollHeight;
const threshold = 300;
if (codeHeight > threshold) {
block.classList.add('folded');
const toggleBtn = document.createElement('button');
toggleBtn.textContent = '展开';
toggleBtn.addEventListener('click', () => {
block.classList.toggle('folded');
toggleBtn.textContent = block.classList.contains('folded') ? '展开' : '折叠';
});
actions.appendChild(toggleBtn);
}
const copyBtn = document.createElement('button');
copyBtn.textContent = '复制';
copyBtn.addEventListener('click', async () => {
const codeLines = block.querySelectorAll('.code .line');
const code = Array.from(codeLines)
.map(line => line.textContent)
.join('\n')
.replace(/\n\n/g, '\n');
try {
await navigator.clipboard.writeText(code);
copyBtn.textContent = '已复制';
copyBtn.classList.add('copied');
setTimeout(() => {
copyBtn.textContent = '复制';
copyBtn.classList.remove('copied');
}, 3000);
} catch (err) {
console.error('复制失败:', err);
copyBtn.textContent = '复制失败';
setTimeout(() => {
copyBtn.textContent = '复制';
}, 3000);
}
});
actions.appendChild(copyBtn);
caption.innerHTML = '';
caption.appendChild(info);
caption.appendChild(actions);
const markedLines = block.getAttribute('data-marked-lines');
if (markedLines) {
const lines = markedLines.split(',');
lines.forEach(range => {
if (range.includes('-')) {
const [start, end] = range.split('-').map(Number);
for (let i = start; i <= end; i++) {
const line = block.querySelector(`.line-${i}`);
if (line) line.classList.add('marked');
}
} else {
const line = block.querySelector(`.line-${range}`);
if (line) line.classList.add('marked');
}
});
}
});
});</script><script async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js" id="MathJax-script"></script><script>(function() {
document.addEventListener('DOMContentLoaded', function() {
const themeToggle = document.querySelector('.theme-toggle');
if (!themeToggle) return;
const getCurrentTheme = () => {
return document.documentElement.getAttribute('data-theme') || 'light';
};
const updateUI = (theme) => {
const isDark = theme === 'dark';
themeToggle.setAttribute('aria-pressed', isDark.toString());
};
const setTheme = (theme) => {
document.documentElement.setAttribute('data-theme', theme);
document.documentElement.style.colorScheme = theme;
const pageWrapper = document.getElementById('page-wrapper');
if (pageWrapper) {
pageWrapper.setAttribute('data-theme', theme);
}
// Find and remove the temporary anti-flicker style tag if it exists.
// This ensures the main stylesheet takes full control after the initial load.
const antiFlickerStyle = document.getElementById('anti-flicker-style');
if (antiFlickerStyle) {
antiFlickerStyle.remove();
}
localStorage.setItem('theme', theme);
updateUI(theme);
};
const toggleTheme = () => {
const current = getCurrentTheme();
const newTheme = current === 'light' ? 'dark' : 'light';
setTheme(newTheme);
};
updateUI(getCurrentTheme());
themeToggle.addEventListener('click', toggleTheme);
if (window.matchMedia) {
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
mediaQuery.addEventListener('change', function(e) {
if (!localStorage.getItem('theme')) {
const theme = e.matches ? 'dark' : 'light';
setTheme(theme);
}
});
}
});
})();
</script><script src="../js/details-toggle.js" defer></script><script>(function() {
document.addEventListener('DOMContentLoaded', function() {
const backToTopBtn = document.querySelector('.back-to-top');
if (!backToTopBtn) return;
const toggleButtonVisibility = () => {
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
const shouldShow = scrollTop > 200;
if (shouldShow) {
backToTopBtn.classList.add('is-visible');
} else {
backToTopBtn.classList.remove('is-visible');
}
};
let ticking = false;
const handleScroll = () => {
if (!ticking) {
requestAnimationFrame(() => {
toggleButtonVisibility();
ticking = false;
});
ticking = true;
}
};
const scrollToTop = (event) => {
event.preventDefault();
window.scrollTo({
top: 0,
behavior: 'smooth'
});
};
window.addEventListener('scroll', handleScroll);
backToTopBtn.addEventListener('click', scrollToTop);
toggleButtonVisibility();
});
})();</script></body></html>