【问题标题】:Trying to scrape all href from source code. I dont understand what I am doing wrong试图从源代码中抓取所有 href。我不明白我做错了什么
【发布时间】:2015-03-21 22:41:06
【问题描述】:

我正在尝试从标记中的源代码中抓取所有 href 并具有 class= "linked formlink" 。我不明白我做错了什么。我在“链接”中得到空值。

StreamReader sr = new StreamReader(webBrowser1.DocumentStream);
        string sourceCode = sr.ReadToEnd();
        sr.Close();
        //removing illegal path 

        string regexSearch = new string(Path.GetInvalidFileNameChars()) +        new string(Path.GetInvalidPathChars());
        Regex r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
        sourceCode = r.Replace(sourceCode, "");

        HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
        htmlDoc.LoadHtml(sourceCode);

        var links = htmlDoc.DocumentNode
                          .Descendants("a")
                          .Where(x => x.Attributes["class"] != null
                                   && x.Attributes["class"].Value == "linked formlink")
                           .Select(x => x.Attributes["href"].Value.ToString());

【问题讨论】:

  • 你能显示一些源数据吗?

标签: c# winforms linq html-agility-pack


【解决方案1】:

正则表达式正在删除括号以及 html-agile-pack 用于确定标签和类的其他必要字符

删除它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 2023-04-06
    • 2018-06-19
    • 2014-04-19
    • 2020-01-04
    • 1970-01-01
    相关资源
    最近更新 更多