Eugene Shmorgun
Eugene Shmorgun

Reputation: 2075

HTML Agility pack - How to get URLs, which start with specific text?

The question is in the title, but that is more specific: can I get URL from HTML, which starts with specific text ? may be, is there any case to extract in JQuery-style?

$( "a[href^='event_handler']" )

Upvotes: 1

Views: 1041

Answers (3)

JamesIngold
JamesIngold

Reputation: 374

You can also use linq

doc.DocumentNode.SelectNodes("//li").Where(x => x.FirstChild.Attributes["href"].Value.StartsWith("event_handler")).Select(x => x.FirstChild.Attributes["href"].Value).ToList();

Upvotes: 0

Arran
Arran

Reputation: 25056

HTMLAgilityPack is based on using XPath queries, not CSS selectors (which is what you have in your original post).

If you absolutely must use CSS selectors, there is a tool I've used in the past to do this called Fizzler:

https://code.google.com/p/fizzler/

It sits on top of HTMLAgilityPack, so therefore much of the documentation stays the same.

I'd also say your question is a little confusing. Your CSS selector there is selecting something based on it's href starting with a value, yet you mention you want to select something by it's text - which is different. The below is a direct equivlaent of what your original selector is:

//a[starts-with(@href, 'event_handler')]

However, to match on the actual text, not the href, then it's:

//a[starts-with(text(), 'event_handler')]

Upvotes: 1

Marko Gresak
Marko Gresak

Reputation: 8207

Out-of-the-box library doesn't support jquery type selectors (those are CSS selectors FYI), but only XPATH or XSLT selectors. Of course there are good people who took their time and added a extension to CSS selector support, see Add CSS Selector Query Engine onto HTMLAgilityPack.

Adding this, you can select your links with the string selector you've already provided yourself.

Upvotes: 1

Related Questions