【问题标题】:Parsing Tables from a url with Htmlagility [closed]使用 Htmlagility 从 url 解析表 [关闭]
【发布时间】:2023-04-10 16:35:01
【问题描述】:

我是 C# 的初学者,我正在尝试学习如何从具有 htmlagility 的网站获取(提取)表格数据,然后在我的应用程序中显示它。

任何人都知道为什么 .Load 不起作用?或者它仅通过 UWP 编码不起作用?

编译器说“HtmlWeb”不包含“Load”的定义,并且找不到接受“HtmlAgilityPack.HtmlWeb”类型的第一个参数的扩展方法“Load”(您是否缺少 using 指令或程序集参考?)。

为什么会这样以及如何解决?

public class  HtmlWeb{

public HtmlWeb()

{
    string Url = "http://google.de";

    HtmlWeb htmlWeb = new HtmlWeb();

    HtmlAgilityPack.HtmlDocument document = htmlWeb.Load(Url);

谢谢大家X.L

【问题讨论】:

标签: c# parsing html-table html-agility-pack uwp


【解决方案1】:

如前所述,您需要编写自己的 Load(string url) 函数。我很无聊,以前从未听说过HtmlAgilityPack,所以我模拟了一些你可以使用的东西。它读取 url http://www.w3schools.com/html/html_tables.asp 并找到所有 <table> 节点,然后遍历它们以找到所有 <td><th> 节点。然后将所有单元格内部文本写入页面

这是aspx页面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication5.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>HtmlAgilityPack quasi table parser</title>
</head>
<body>
    <h1>Number of tables <asp:Literal ID="litNumTables" runat="server" /></h1>
    <strong>Each &lt;th&gt; and &lt;td&gt; node of each &lt;table&gt; [table][row][cell]</strong>
    <ul>
        <asp:Repeater ID="rptTrNodes" Runat="server">
            <ItemTemplate>
                <li>
                <%# Container.DataItem.ToString() %>
                </li>
            </ItemTemplate>
        </asp:Repeater>
    </ul>
</body>
</html>

这里是 C# 代码:

using System;
using System.Linq;
using HtmlAgilityPack;
using System.Net;
using System.Collections.Generic;

namespace WebApplication5
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var html = new HtmlDocument();
            html.LoadHtml(new WebClient().DownloadString("http://www.w3schools.com/html/html_tables.asp"));
            var root = html.DocumentNode;
            var tableNodes = root.Descendants("table");
            var items = new List<string>();
            foreach(var tbs in tableNodes.Select((tbNodes,i) => new { tbNodes = tbNodes, i = i }))
            {
                items.Add(string.Format("<h2>&lt;table&gt; {0}<h2>", tbs.i));
                var trs = tbs.tbNodes.Descendants("tr");
                foreach(var tr in trs.Select((trNodes, j) => new { trNodes = trNodes, j = j }))
                {
                    var cellCounter = 0;
                    foreach(var cell in tr.trNodes.Descendants("th"))
                    {
                        items.Add(string.Format("&lt;th&gt; [{0}][{1}][{2}] - {3}", tbs.i, tr.j, cellCounter, cell.InnerText.Trim()));
                        cellCounter++;
                    }
                    foreach (var cell in tr.trNodes.Descendants("td"))
                    {
                        items.Add(string.Format("&lt;td&gt; [{0}][{1}][{2}] - {3}", tbs.i, tr.j, cellCounter, cell.InnerText.Trim()));
                        cellCounter++;
                    }
                }
            }
            litNumTables.Text = tableNodes.Count().ToString();
            rptTrNodes.DataSource = items;
            rptTrNodes.DataBind();
        }
    }
}

这应该是不言自明的,但如果它是您正在寻找的,那么我可以进一步解释。

【讨论】:

  • 为什么它被否决了,代码按照用户的要求进行
  • 嗨 @user3639090 我认为 UWP 应用程序不能使用“使用 System.Net;”它会导致2个问题,即1,名称空间'System'中不存在类型或名称空间名称'Web'和2,找不到类型或名称空间名称'WebClient'你知道为什么以及如何解决多谢
  • 如果我想从这个站点解析 link 那么我可以使用命名空间“System.Net.Http.HttpClient”吗?因为 UWP 编码不支持 system.web
  • 我修复了 1. 使用 System.Net.Http.HttpClient 的问题,但是 2,仍然存在,我无法修复它
  • 老实说我不知道​​,从来没有做过任何 UWP 编程。如果您不能使用 using System.Net,那么您可以尝试 System.Net.WebClient() 而不是 WebClient()
猜你喜欢
  • 1970-01-01
  • 2013-12-16
  • 1970-01-01
  • 2012-08-15
  • 1970-01-01
  • 2019-08-06
  • 2015-10-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多