How to get a link's title and href value separately with html agility pack?

后端 未结 3 843
长发绾君心
长发绾君心 2021-01-27 23:37

Im trying to download a page contain a table like this

Name
相关标签:
3条回答
  • 2021-01-28 00:12
        public const string UrlExtractor = @"(?: href\s*=)(?:[\s""']*)(?!#|mailto|location.|javascript|.*css|.*this\.)(?<url>.*?)(?:[\s>""'])";
    
        public static Match GetMatchRegEx(string text)
        {
            return new Regex(UrlExtractor, RegexOptions.IgnoreCase).Match(text);
        }
    

    Here is how you can extract all Href Url. I'm using that regex in one of my projects, you can modify it to match your needs and rewrite it to match title as well. I guess it is more convenient to match them in bulk

    0 讨论(0)
  • 2021-01-28 00:19

    I can't test it right now, but it should be something among the lines of :

        string name= namenode.Element("a").Element("b").InnerText;
        string url= linknode.Element("a").GetAttributeValue("href","unknown");
    
    0 讨论(0)
  • 2021-01-28 00:20
    nameNode.Attributes["title"]
    linkNode.Attributes["href"]
    

    presuming you are getting the correct Nodes.

    0 讨论(0)
提交回复
热议问题