相关文章推荐
帅气的鼠标
·
为何说“美国侵略了台湾” ...
·
2 月前
·
重情义的小熊猫
·
三星电池门_百度百科
·
1 年前
·
逆袭的小笼包
·
Search Results - ...
·
1 年前
·
谈吐大方的铁链
·
強直性脊椎炎|健康資訊— 領康香港
·
1 年前
·
谦逊的水煮肉
·
人工智能趣味入门:光环板程序... ...
·
1 年前
·
小百科
›
从 HTML 表中提取数据 – C# 代码
html代码
大数据
table
幸福的梨子
3 月前
</noscript><div class="header"><div><a rel="nofollow" href="https://www.aspose.com" alt="Aspose Pty Ltd Logo" aria-label="Aspose Pty Ltd Logo" class="logo"/><div class="bar"><ul class="menu"><li class="products desktop-only"><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com">Products</a><ul class="submenu grid-4"><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/total/"><span class="title">Aspose.Total</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/words/"><span class="title">Aspose.Words</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/pdf/"><span class="title">Aspose.PDF</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/cells/"><span class="title">Aspose.Cells</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/email/"><span class="title">Aspose.Email</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/slides/"><span class="title">Aspose.Slides</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/imaging/"><span class="title">Aspose.Imaging</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/barcode/"><span class="title">Aspose.BarCode</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/diagram/"><span class="title">Aspose.Diagram</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/tasks/"><span class="title">Aspose.Tasks</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/ocr/"><span class="title">Aspose.OCR</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/note/"><span class="title">Aspose.Note</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/cad/"><span class="title">Aspose.CAD</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/3d/"><span class="title">Aspose.3D</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/html/"><span class="title">Aspose.HTML</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/gis/"><span class="title">Aspose.GIS</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/zip/"><span class="title">Aspose.ZIP</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/page/"><span class="title">Aspose.Page</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/psd/"><span class="title">Aspose.PSD</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/omr/"><span class="title">Aspose.OMR</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/pub/"><span class="title">Aspose.PUB</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/svg/"><span class="title">Aspose.SVG</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/finance/"><span class="title">Aspose.Finance</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/drawing/"><span class="title">Aspose.Drawing</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/font/"><span class="title">Aspose.Font</span><span class="description">Product Solution</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://products.aspose.com/tex/"><span class="title">Aspose.TeX</span><span class="description">Product Solution</span></a></li></ul></li><li class="desktop-only"><a class="menu-item" rel="noopener nofollow" href="#">Purchase</a><ul class="submenu"><li><a class="menu-item" rel="noopener nofollow" href="https://purchase.aspose.com/buy"><span class="title">Buy Now</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://purchase.aspose.com/pricing"><span class="title">Pricing Information</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://downloads.aspose.com/"><span class="title">Free Trials</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://purchase.aspose.com/temporary-license"><span class="title">Temporary License</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://purchase.aspose.com/policies"><span class="title">Policies</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://purchase.aspose.com/orders"><span class="title">My Orders & Quotes</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://purchase.aspose.com/renew"><span class="title">Renew an Order</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://purchase.aspose.com/upgrade"><span class="title">Upgrade an Order</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://helpdesk.aspose.com/"><span class="title">Paid Support</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://consulting.aspose.com/"><span class="title">Paid Consulting</span></a></li></ul></li><li class="desktop-only"><a class="menu-item" rel="noopener nofollow" href="#">Support</a><ul class="submenu"><li><a class="menu-item" rel="noopener nofollow" href="https://docs.aspose.com/"><span class="title">Docs</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://reference.aspose.com/"><span class="title">API Reference</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://demos.aspose.com/"><span class="title">Live Demos</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://aspose.github.io/"><span class="title">Code Samples</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://forum.aspose.com/"><span class="title">Free Support</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://aspose-free-consulting.github.io/"><span class="title">Free Consulting</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://blog.aspose.com/"><span class="title">Blog</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://kb.aspose.com/"><span class="title">Knowledge Base</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://releases.aspose.com/"><span class="title">New Releases</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://status.aspose.com/"><span class="title">Status</span></a></li></ul></li><li class="desktop-only"><a class="menu-item" rel="noopener nofollow" href="https://websites.aspose.com/">Websites</a><ul class="submenu grid-4"><li><a class="menu-item" rel="noopener nofollow" href="https://websites.aspose.com/aspose/com/"><span class="title">aspose.com</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://websites.aspose.com/aspose/cloud/"><span class="title">aspose.cloud</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://websites.aspose.com/aspose/app/"><span class="title">aspose.app</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://websites.aspose.com/aspose/ai/"><span class="title">aspose.ai</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://websites.aspose.com/groupdocs/com/"><span class="title">groupdocs.com</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://websites.aspose.com/groupdocs/cloud/"><span class="title">groupdocs.cloud</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://websites.aspose.com/groupdocs/app/"><span class="title">groupdocs.app</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://websites.aspose.com/groupdocs/ai/"><span class="title">groupdocs.ai</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://websites.aspose.com/conholdate/com/"><span class="title">conholdate.com</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://websites.aspose.com/conholdate/cloud/"><span class="title">conholdate.cloud</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://websites.aspose.com/conholdate/app/"><span class="title">conholdate.app</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://websites.aspose.com/conholdate/ai/"><span class="title">conholdate.ai</span></a></li></ul></li><li class="desktop-only"><a class="menu-item" rel="noopener nofollow" href="https://about.aspose.com">About</a><ul class="submenu"><li><a class="menu-item" rel="noopener nofollow" href="https://about.aspose.com"><span class="title">About Us</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://about.aspose.com/contact/"><span class="title">Contact</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://about.aspose.com/customers/"><span class="title">Customers</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://about.aspose.com/legal/"><span class="title">Legal</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://about.aspose.com/affiliates/"><span class="title">Affiliates</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://about.aspose.com/events/"><span class="title">Events</span></a></li><li><a class="menu-item" rel="noopener nofollow" href="https://about.aspose.com/acquisition/"><span class="title">Acquisition</span></a></li></ul></li><li class="language"><span class="menu-item"><svg viewbox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" fill="currentcolor"><path d="M12 4c4.4183.0 8 3.58172 8 8 0 4.4183-3.5817 8-8 8-4.41828.0-8-3.5817-8-8 0-4.41828 3.58172-8 8-8zm-.381 11.428L8.57155 15.4289C8.9774 16.7012 9.60471 17.924 10.4358 19.0678 10.8198 19.1532 11.2151 19.2073 11.6191 19.2282L11.619 15.428zM15.4284 15.4289 12.38 15.428 12.3809 19.2282C12.7849 19.2073 13.1802 19.1532 13.5641 19.0687 14.3953 17.924 15.0226 16.7012 15.4284 15.4289zM18.3762 15.4285 16.0984 15.4284C15.7811 16.5746 15.31 17.6754 14.703 18.7169c1.5738-.634 2.8744-1.806 3.6732-3.2884zM7.90159 15.4284 5.62381 15.4285c.79877 1.4824 2.09943 2.6544 3.67446 3.2885-.60825-1.0416-1.07936-2.1424-1.39668-3.2886zM7.4355 12.3803 4.77175 12.3809C4.81342 13.1846 4.98617 13.9535 5.26904 14.6669L7.71488 14.6666c-.15933-.7461-.25402-1.5094-.27938-2.2863zM15.9653 12.3808 12.38 12.38v2.286L15.6434 14.6671c.1843-.7493.2928-1.5134.321899999999999-2.2863zM19.2282 12.3809 16.5645 12.3803C16.5391 13.1572 16.4444 13.9205 16.2851 14.6666L18.731 14.6669C19.0138 13.9535 19.1866 13.1846 19.2282 12.3809zM11.619 12.38 8.03469 12.3808C8.06384 13.1537 8.17229 13.9178 8.35656 14.6671L11.619 14.666V12.38zM7.78068 8.95226 5.43294 8.95224C5.05284 9.76985 4.82095 10.6703 4.77175 11.6191L7.4329 11.6194C7.45634 10.71 7.57476 9.81899 7.78068 8.95226zM15.5678 8.95239 12.38 8.952v2.667h3.588L15.9673 11.5875C15.9382 10.6945 15.8033 9.81313 15.5678 8.95239zM18.5671 8.95224 16.2193 8.95226C16.4252 9.81899 16.5437 10.71 16.5671 11.6194L19.2282 11.6191C19.1791 10.6703 18.9472 9.76985 18.5671 8.95224zM11.619 8.952 8.43217 8.95239C8.16446 9.93108 8.02667 10.9364 8.02667 11.9553L8.031 11.619h3.588V8.952zM9.22964 5.31048 9.02453 5.39982c-1.31708.59469-2.4243 1.57203-3.17995 2.79029L7.98594 8.19044C8.28727 7.19147 8.70587 6.2283 9.22964 5.31048zm3.15126-.53873L12.38 8.19 15.3312 8.18991c-.3925-1.12859-.9602-2.21592-1.6906-3.24091C13.2322 4.85405 12.8116 4.79408 12.3809 4.77175zm2.3884.53883L14.9321 5.60148C15.3809 6.43124 15.7444 7.2965 16.0141 8.19044L18.1554 8.19011C17.3607 6.9088 16.177 5.89399 14.7693 5.31058zm-4.1992-.40747L10.3594 4.949C9.629 5.97399 9.06128 7.06132 8.66884 8.18991L11.619 8.19 11.6191 4.77175C11.262 4.79027 10.9117 4.83467 10.5701 4.90311z"/></svg><span>简体中文</span></span><ul class="submenu grid-3"><li><a href="https://products.aspose.com/html/net/extract-data-from-html-table/" class="menu-item"><span class="title">English</span></a></li><li><a href="https://products.aspose.com/html/es/net/extract-data-from-html-table/" class="menu-item"><span class="title">Español</span></a></li><li><a href="https://products.aspose.com/html/fr/net/extract-data-from-html-table/" class="menu-item"><span class="title">Français</span></a></li><li><a href="https://products.aspose.com/html/ja/net/extract-data-from-html-table/" class="menu-item"><span class="title">日本語</span></a></li><li><a href="https://products.aspose.com/html/ru/net/extract-data-from-html-table/" class="menu-item"><span class="title">Русский</span></a></li><li><a href="https://products.aspose.com/html/id/net/extract-data-from-html-table/" class="menu-item"><span class="title">Indonesian</span></a></li><li><a href="https://products.aspose.com/html/de/net/extract-data-from-html-table/" class="menu-item"><span class="title">Deutsch</span></a></li><li><a href="https://products.aspose.com/html/pt/net/extract-data-from-html-table/" class="menu-item"><span class="title">Português</span></a></li><li><a href="https://products.aspose.com/html/ko/net/extract-data-from-html-table/" class="menu-item"><span class="title">한국인</span></a></li><li><a href="https://products.aspose.com/html/uk/net/extract-data-from-html-table/" class="menu-item"><span class="title">Українська</span></a></li><li><a href="https://products.aspose.com/html/th/net/extract-data-from-html-table/" class="menu-item"><span class="title">ไทย</span></a></li></ul></li></ul></div></div></div><link href="/html/css/breadcrumb.css" rel="stylesheet"/><div class="container-fluid bg-light-primary"><div class="container"><div class="row"><div class="col-md-12 p-0 overflow-attr"><ol class="d-flex align-items-center m-0 productbreadcrumbs p-0" itemscope="" itemtype="https://schema.org/BreadcrumbList"><li itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem"><a href="/zh/" itemprop="item"><span itemprop="name">产品</span></a><meta itemprop="position" content="1"/></li><li itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem"><i class="fa fa-angle-right"> </i> <a itemprop="item" href="/html/"><span itemprop="name">Aspose.HTML</span></a><meta itemprop="position" content="2"/></li><li itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem"><i class="fa fa-angle-right"> </i> <span itemprop="name">Extract data from HTML table</span><meta itemprop="position" content="4"/></li></ol></div></div></div></div><div class="container-fluid td-default td-outer set-min-height"><main role="main" class="td-main"><div class="pf-landing-page-com autogen"><div class="container-fluid header1 header2 productfamilyheader minify-header banner-media_modify"><div class="container"><div class="row pt-lg-5"><div class="col-md-12 padding0"><div class="col-lg-3 col-md-3 pull-right"><div class="tr"><div class="aspose_product_img"><div class="circleiconsformat"><em class="fileiconsmall">XHTML</em> <em class="fileiconsmall">MHTML</em></div><img class="lazyloaded" width="220" height="220" src="/html/images/aspose_html-for-net.svg" loading="lazy" alt=" Product Family"/><div class="conversiontag">HTML</div></div></div></div><div class="padding0 col-lg-9"><h1>从 HTML 表中提取数据 – C#</h1><h2 class="pr-h2">使用 Aspose.HTML C# 库以编程方式从 HTML 文档中查找和提取表格数据!</h2><a class="btn btn-warning btn-lg" href="https://www.nuget.org/packages/Aspose.Html">Download from NuGet</a></div></div></div></div></div><div class="container-fluid productfamilypage bg-white"><div class="container-fluid sub-menu-container"><div id="sticky1" class="stickytopheight"> </div><div class="container"><div class="row"><div class="col-md-2 title2"><h4><span class="spanclass"><img width="42" height="42" src="/html/images/aspose_html-for-net.svg" alt="Aspose.HTML for .NET" class="lazyloaded"/> </span>Aspose.HTML <small>for .NET</small></h4></div><div class="col-md-6"><ul class="nav nav-pills mb-hide"><li><a class="zap-btn-apps" href="https://products.aspose.app/html/conversion">Live Demos</a></li><li><a class="zap-btn-cloud" href="https://aspose.conholdate.cloud/html/">Cloud</a></li><li><a class="zap-btn-docs" href="https://docs.aspose.com/html/net/">Documentation</a></li><li><a class="zap-btn-code" href="https://purchase.aspose.com/temporary-license/">Free Trial</a></li><li><a class="zap-btn-api" href="https://reference.aspose.com/html/net/">API Reference</a></li></ul><div class="submenu"><ul class="submenul"><li class="dropdown smenu-li"><a class="dropdown-toggle arrow-dd" href="/" data-toggle="dropdown">Overview</a><ul class="dropdown-menu smenu-dd"><li><a href="#overview">Overview</a></li><li><a class="zap-btn-apps" href="https://products.aspose.app/html/conversion">Live Demos</a></li><li><a class="zap-btn-cloud" href="https://aspose.conholdate.cloud/html/">Cloud</a></li><li><a class="zap-btn-docs" href="https://docs.aspose.com/html/net/">Documentation</a></li><li><a class="zap-btn-code" href="https://purchase.aspose.com/temporary-license/">Free Trial</a></li><li><a class="zap-btn-api" href="https://reference.aspose.com/html/net/">API Reference</a></li></ul></li></ul></div></div><div class="col-md-4 sub-rhs col-sm-12"><div class="btn-group"><a class="btn btn-primary zap-btn-download" href="https://releases.aspose.com/html/net/"><em class="fa fa-download"> </em> Download</a> Pricing</a> <a class="btn btn-primary zap-btn-buy" href="https://purchase.aspose.com/buy/cart?ppId=97464">Buy</a></div></div></div></div></div><div class="autogen container-fluid bg-white agp-content section"><div class="container"><div class="row"><div class="col-md-12 tl"><h2 class="bold-h2">如何从 HTML 表格中提取数据</h2><p>HTML 表格在网络上广泛用于显示信息。从 HTML 表格中提取数据在网络抓取、数据分析和自动化中很常见。构建解析器时,通常需要从 HTML 表中提取数据并将其转换为结构化格式,例如 JSON、CSV 或 Excel。让我们探讨如何从 HTML 表中提取数据。</p><p>首先,确保您的项目中安装了 <b>Aspose.HTML for .NET</b>。 这个库的安装过程非常简单。 打开 NuGet 包管理器,搜索 Aspose.HTML,然后安装。 您还可以从包管理器控制台使用以下命令:</p><br/><div class="col-md-12 tl"><div id="code" class="codeblock"><h3>安装 Aspose.HTML for .NET</h3><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cs" data-lang="cs"><span style="display:flex"><span>Install-Package Aspose.HTML </span></span></code></pre></div><p><br/><br/></p></div></div><h2>使用 C# 从 HTML 表中提取数据</h2><p><a href="/html/zh/net/" target="_blank" rel="nofollow">Aspose.HTML for .NET</a> 是一个强大的库,它提供了一组强大的工具来解析和收集 HTML 文档中的信息。以下示例演示如何查找 HTML 文档中的所有<code><table></code>元素,提取表数据,并以 JSON 格式输出。假设 HTML 中的一个表包含一个测试列表,其中每个测试都有一个 ID、名称、注释以及指向测试内容的超链接。这是我们要从以下示例中的表中提取的信息:</p><br/><div class="col-md-12 tl"><div id="code" class="codeblock"><h3>从 HTML 表中提取数据的 C# 代码</h3><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cs" data-lang="cs"><span style="display:flex"><span><span style="color:#66d9ef">using</span> Aspose.Html; </span></span><span style="display:flex"><span><span style="color:#66d9ef">using</span> System.IO; </span></span><span style="display:flex"><span><span style="color:#66d9ef">using</span> System.Linq; </span></span><span style="display:flex"><span><span style="color:#66d9ef">using</span> System.Text.Json; </span></span><span style="display:flex"><span><span style="color:#66d9ef">using</span> System.Collections.Generic; </span></span><span style="display:flex"><span> <span style="color:#75715e">// Open the document from which you want to extract table data</span> </span></span><span style="display:flex"><span> <span style="color:#66d9ef">using</span> var document = <span style="color:#66d9ef">new</span> HTMLDocument(Path.Combine(DataDir, <span style="color:#e6db74">"chapter-9.htm"</span>)); </span></span><span style="display:flex"><span> <span style="color:#75715e">// Check if there are any table elements in the document</span> </span></span><span style="display:flex"><span> <span style="color:#66d9ef">var</span> tables = document.GetElementsByTagName(<span style="color:#e6db74">"table"</span>); </span></span><span style="display:flex"><span> <span style="color:#66d9ef">if</span> (tables.Any()) </span></span><span style="display:flex"><span> <span style="color:#66d9ef">var</span> result = <span style="color:#66d9ef">new</span> List<Dictionary<<span style="color:#66d9ef">string</span>, <span style="color:#66d9ef">string</span>>>(); </span></span><span style="display:flex"><span> <span style="color:#75715e">//var i = 0;</span> </span></span><span style="display:flex"><span> <span style="color:#66d9ef">foreach</span> (<span style="color:#66d9ef">var</span> table <span style="color:#66d9ef">in</span> tables) </span></span><span style="display:flex"><span> <span style="color:#75715e">// extract data from html table</span> </span></span><span style="display:flex"><span> <span style="color:#66d9ef">var</span> tbodies = table.GetElementsByTagName(<span style="color:#e6db74">"tbody"</span>); </span></span><span style="display:flex"><span> <span style="color:#66d9ef">foreach</span> (<span style="color:#66d9ef">var</span> tbody <span style="color:#66d9ef">in</span> tbodies) </span></span><span style="display:flex"><span> <span style="color:#66d9ef">if</span> (tbody.Children.Length > <span style="color:#ae81ff">1</span>) </span></span><span style="display:flex"><span> <span style="color:#66d9ef">foreach</span> (<span style="color:#66d9ef">var</span> row <span style="color:#66d9ef">in</span> tbody.Children) </span></span><span style="display:flex"><span> <span style="color:#66d9ef">if</span> (row.HasAttribute(<span style="color:#e6db74">"id"</span>)) </span></span><span style="display:flex"><span> <span style="color:#75715e">//test row</span> </span></span><span style="display:flex"><span> <span style="color:#66d9ef">var</span> data = <span style="color:#66d9ef">new</span> Dictionary<<span style="color:#66d9ef">string</span>, <span style="color:#66d9ef">string</span>>(); </span></span><span style="display:flex"><span> data[<span style="color:#e6db74">"Id"</span>] = row.GetAttribute(<span style="color:#e6db74">"id"</span>); </span></span><span style="display:flex"><span> <span style="color:#66d9ef">if</span> (row.Children.Length > <span style="color:#ae81ff">0</span>) </span></span><span style="display:flex"><span> <span style="color:#66d9ef">var</span> td = row.Children[<span style="color:#ae81ff">0</span>]; </span></span><span style="display:flex"><span> <span style="color:#66d9ef">if</span> (td.Children.Length > <span style="color:#ae81ff">0</span>) </span></span><span style="display:flex"><span> <span style="color:#66d9ef">var</span> element = td.Children[<span style="color:#ae81ff">0</span>].TagName == <span style="color:#e6db74">"STRONG"</span> </span></span><span style="display:flex"><span> ? td.Children[<span style="color:#ae81ff">0</span>].Children[<span style="color:#ae81ff">0</span>] </span></span><span style="display:flex"><span> : td.Children[<span style="color:#ae81ff">0</span>]; </span></span><span style="display:flex"><span> <span style="color:#66d9ef">var</span> href = ((HTMLAnchorElement)element).Href; </span></span><span style="display:flex"><span> data[<span style="color:#e6db74">"Href"</span>] = href; </span></span><span style="display:flex"><span> data[<span style="color:#e6db74">"TestName"</span>] = Path.GetFileNameWithoutExtension(href); </span></span><span style="display:flex"><span> data[<span style="color:#e6db74">"TestComment"</span>] = <span style="color:#66d9ef">string</span>.Join(<span style="color:#e6db74">" "</span>, </span></span><span style="display:flex"><span> row.Children[<span style="color:#ae81ff">3</span>].TextContent </span></span><span style="display:flex"><span> .Split(<span style="color:#66d9ef">new</span> <span style="color:#66d9ef">char</span>[<span style="color:#ae81ff">0</span>], StringSplitOptions.RemoveEmptyEntries).ToList() </span></span><span style="display:flex"><span> .Select(x => x.Trim())); </span></span><span style="display:flex"><span> result.Add(data); </span></span><span style="display:flex"><span> <span style="color:#66d9ef">var</span> json = JsonSerializer.Serialize(result); </span></span><span style="display:flex"><span> Console.WriteLine(json); </span></span><span style="display:flex"><span> <span style="color:#75715e">// Handle the case where no tables are found</span> </span></span><span style="display:flex"><span> Console.WriteLine(<span style="color:#e6db74">"No tables found in the document."</span>); </span></span></code></pre></div><p><br/><br/></p></div></div><h2>从 HTML 表中提取数据的步骤</h2><p>通过执行这些步骤,您可以从 HTML 中提取表格数据(例如超链接和文本内容),用于各种目的,包括数据分析或报告。</p><ol><li>使用 <a href="https://reference.aspose.com/html/net/aspose.html/htmldocument/htmldocument/" target="_blank" rel="nofollow">HTMLDocument()</a> 构造函数初始化 HTML 文档。将源 HTML 文件的路径作为参数传递给构造函数。</li><li>使用 <a href="https://reference.aspose.com/html/net/aspose.html.dom/document/getelementsbytagname/" target="_blank" rel="nofollow">GetElementsByTagName(<code>"table"</code>)</a> 方法收集所有 <code><table></code> 元素。该方法返回 HTML 文档的<code><table></code>元素的列表。将表元素的集合存储在<code>tables</code>变量中。</li><li>使用 LINQ <code>Any()</code> 方法检查 HTML 文档中是否存在任何 <code><table></code> 元素。这确保有可以从中提取数据的表。</li><li>使用<code>foreach</code>循环遍历文档中找到的每个表:<ul><li>使用<code>GetElementsByTagName("tbody")</code>方法检索所有<code><tbody></code>元素(表体)。</li><li>在循环内,迭代每个<code><tbody></code>元素以访问使用另一个<code>foreach</code>循环获得的数据行。</li><li>根据特定条件或属性从每一行中提取相关数据。</li><li>从所有行中提取数据后,使用 <code>JsonSerializer.Serialize()</code> 方法将包含提取数据的字典列表序列化为 JSON 格式。</li></ul></li><li>使用 <code>Console.WriteLine()</code> 方法输出序列化的 JSON 以在控制台中显示。</li><li>如果文档不包含表格,则向控制台打印一条消息,指示未找到表格。</li></ol><p>要了解有关 Aspose.HTML API 的更多信息,请访问我们的 指南。 Aspose.HTML for .NET 是一个高级 HTML 解析库,允许您创建、编辑和转换 HTML、XHTML、MD、EPUB 和 MHTML 文件。 <a href="https://docs.aspose.com/html/net/data-extraction/" target="_blank" rel="nofollow">Data Extraction</a>
推荐文章
帅气的鼠标
·
为何说“美国侵略了台湾” 蔚蓝色的战场-5 刘本新|刘本新|日本|美国 ...
2 月前
重情义的小熊猫
·
三星电池门_百度百科
1 年前
逆袭的小笼包
·
Search Results - Southern University Law Center
1 年前
谈吐大方的铁链
·
強直性脊椎炎|健康資訊— 領康香港
1 年前
谦逊的水煮肉
·
人工智能趣味入门:光环板程序... 刘育红定价:59 元
1 年前