【问题标题】:HTMLAGILITYPACK Saving Whole HTML Source As A StringHTMLAGILITYPACK 将整个 HTML 源代码保存为字符串
【发布时间】:2018-05-31 18:46:14
【问题描述】:

所以基本上这就是我想要做的,我想将整个 html 源文本保存到一个字符串中,我将检查它是否包含 myvar。 我已经看到许多其他主题说如何做到这一点,但我尝试了它们并最终出现错误,我要么在使用 .load("example.com"); 时进入中断状态;否则字符串最终将包含 URL 而不是实际的 HTML 代码。

这是我的代码:

        string myString = "Pastebin";


        HtmlAgilityPack.HtmlDocument page = new HtmlAgilityPack.HtmlDocument();
        page.Load("https://pastebin.com");
        string text = page.DocumentNode.OuterHtml;


        if (text.Contains(myString))
        {
             MessageBox.Show("Yay!\n Match!");
            Instance = this;
            InitializeComponent();
            timer1.Start();
        }
        else
        {
            MessageBox.Show("Error...\nThe Var Doesnt match");
            Application.Exit();
        }
    }

结果:

使用 .load("example.com");应用程序进入中断状态。 使用 .loadhtml("example.com");应用程序存储 URL 而不是 HTML

【问题讨论】:

    标签: c# web html-agility-pack


    【解决方案1】:

    这里是documentation。使用HtmlWeb通过url加载html页面:

    using HtmlAgilityPack;
    //...
    
        HtmlWeb htmlWeb = new HtmlWeb();
        HtmlDocument htmlDoc = htmlWeb.Load("https://pastebin.com");
        string text = htmlDoc.Text;
    

    【讨论】:

    • 哇,我现在感觉很愚蠢,是的,这完全有效,为什么其他人如此坚持认为另一种方式有效?也许它在不同的版本中工作。我用这个得到了我以前的信息:stackoverflow.com/questions/5183385/…
    • @pOleander 使用page.Load(来自您的代码)您可以从textfile 加载html,但不能从url 加载。这是所有可用的 html 解析器的link 以获取详细信息。
    • 哦!我没有意识到那是如何工作的,所以我猜我是否已经有一个 html 文件,或者有 html 代码,这将用于加载代码,是的,这是有道理的,也有点酷。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 2017-06-22
    • 2012-04-15
    • 1970-01-01
    相关资源
    最近更新 更多