在c#中是否有一个类似jquery的CSS/HTML选择器?

时间:2022-09-19 22:33:12

I'm wondering if there's a jQuery-like css selector that can be used in C#.

我想知道在c#中是否有一个类似于jquery的css选择器。

Currently, I'm parsing some html strings using regex and thought it would be much nicer to have something like the css selector in jQuery to match my desired elements.

目前,我正在使用regex解析一些html字符串,并认为使用jQuery中的css选择器来匹配所需的元素会更好。

5 个解决方案

#1


69  

You should definitely see @jamietre's CsQuery. Check out his answer to this question!

你一定会看到@jamietre的CsQuery。看看他对这个问题的回答!

Fizzler and Sharp-Query provide similar functionality, but the projects seem to be abandoned.

Fizzler和Sharp-Query也提供类似的功能,但是项目似乎被放弃了。

#2


78  

Update 10/18/2012

更新10/18/2012

CsQuery is now in release 1.3. The latest release incorporates a C# port of the validator.nu HTML5 parser. As a result CsQuery will now produce a DOM that uses the HTML5 spec for invalid markup handling and is completely standards compliant.

CsQuery现在已经发布了1.3版。最新的版本合并了验证器的c#端口。νHTML5解析器。因此,CsQuery现在将生成一个DOM,它使用HTML5规范进行无效的标记处理,并且完全符合标准。

Original Answer

原来的答案

Old question but new answer. I've recently released version 1.1 of CsQuery, a jQuery port for .NET 4 written in C# that I've been working on for about a year. Also on NuGet as "CsQuery"

老问题,新答案。我最近发布了CsQuery的1.1版本,CsQuery是用c#编写的。net 4的jQuery端口,我已经用c#编写了大约一年了。在NuGet上也有"CsQuery"

The current release implements all CSS2 & CSS3 selectors, all jQuery extensions, and all jQuery DOM manipulation methods. It's got extensive test coverage including all the tests from jQuery and sizzle (the jQuery CSS selection engine). I've also included some performance tests for direct comparisons with Fizzler; for the most part CsQuery dramatically outperforms it. The exception is actually loading the HTML in the first place where Fizzler is faster; I assume this is because fizzler doesn't build an index. You get that time back after your first selection, though.

当前版本实现了所有CSS2和CSS3选择器、所有jQuery扩展和所有jQuery DOM操作方法。它有广泛的测试覆盖范围,包括jQuery和sizzle (jQuery CSS选择引擎)的所有测试。我还包括了一些性能测试,用于与Fizzler进行直接比较;在很大程度上,CsQuery要比它出色得多。唯一的例外是在Fizzler更快的地方加载HTML;我认为这是因为fizzler没有建立索引。不过,在你第一次选择之后,你会得到时间。

There's documentation on the github site, but at a basic level it works like this:

github网站上有文档,但在基本层面上,它是这样工作的:

Create from a string of HTML

从HTML字符串创建

CQ dom = CQ.Create(htmlString);

Load synchronously from the web

从web同步加载

CQ dom = CQ.CreateFromUrl("http://www.jquery.com");

Load asynchronously (non-blocking)

异步加载(阻塞)

CQ.CreateFromUrlAsync("http://www.jquery.com", responseSuccess => {
    Dom = response.Dom;        
}, responseFail => {
    ..
});

Run selectors & do jQuery stuff

运行选择器和jQuery之类的东西

var childSpans = dom["div > span"];
childSpans.AddClass("myclass");

the CQ object is like thejQuery object. The property indexer used above is the default method (like $(...).

CQ对象类似于jquery对象。上面使用的属性索引器是默认方法(例如$(…)。

Output:

输出:

string html = dom.Render();

#3


2  

For XML you might use XPath...

对于XML,可以使用XPath……

#4


1  

I'm not entirely clear as to what you're trying to achieve, but if you have a HTML document that you're trying to extract data from, I'd recommend loading it with a parser, and then it becomes fairly trivial to query the object to pull desired elements.

我不太清楚您想要实现什么,但是如果您有一个试图从中提取数据的HTML文档,我建议使用解析器加载它,然后查询对象以提取所需的元素就变得非常简单了。

The parser I linked above allows for use of XPath queries, which sounds like what you are looking for.

上面链接的解析器允许使用XPath查询,这听起来像您要查找的内容。

Let me know if I've misunderstood.

如果我误会了,请告诉我。

#5


1  

Not quite jQuery like, but this may help: http://www.codeplex.com/htmlagilitypack

不太喜欢jQuery,但这可能会有所帮助:http://www.codeplex.com/htmlagilitypack

#1


69  

You should definitely see @jamietre's CsQuery. Check out his answer to this question!

你一定会看到@jamietre的CsQuery。看看他对这个问题的回答!

Fizzler and Sharp-Query provide similar functionality, but the projects seem to be abandoned.

Fizzler和Sharp-Query也提供类似的功能,但是项目似乎被放弃了。

#2


78  

Update 10/18/2012

更新10/18/2012

CsQuery is now in release 1.3. The latest release incorporates a C# port of the validator.nu HTML5 parser. As a result CsQuery will now produce a DOM that uses the HTML5 spec for invalid markup handling and is completely standards compliant.

CsQuery现在已经发布了1.3版。最新的版本合并了验证器的c#端口。νHTML5解析器。因此,CsQuery现在将生成一个DOM,它使用HTML5规范进行无效的标记处理,并且完全符合标准。

Original Answer

原来的答案

Old question but new answer. I've recently released version 1.1 of CsQuery, a jQuery port for .NET 4 written in C# that I've been working on for about a year. Also on NuGet as "CsQuery"

老问题,新答案。我最近发布了CsQuery的1.1版本,CsQuery是用c#编写的。net 4的jQuery端口,我已经用c#编写了大约一年了。在NuGet上也有"CsQuery"

The current release implements all CSS2 & CSS3 selectors, all jQuery extensions, and all jQuery DOM manipulation methods. It's got extensive test coverage including all the tests from jQuery and sizzle (the jQuery CSS selection engine). I've also included some performance tests for direct comparisons with Fizzler; for the most part CsQuery dramatically outperforms it. The exception is actually loading the HTML in the first place where Fizzler is faster; I assume this is because fizzler doesn't build an index. You get that time back after your first selection, though.

当前版本实现了所有CSS2和CSS3选择器、所有jQuery扩展和所有jQuery DOM操作方法。它有广泛的测试覆盖范围,包括jQuery和sizzle (jQuery CSS选择引擎)的所有测试。我还包括了一些性能测试,用于与Fizzler进行直接比较;在很大程度上,CsQuery要比它出色得多。唯一的例外是在Fizzler更快的地方加载HTML;我认为这是因为fizzler没有建立索引。不过,在你第一次选择之后,你会得到时间。

There's documentation on the github site, but at a basic level it works like this:

github网站上有文档,但在基本层面上,它是这样工作的:

Create from a string of HTML

从HTML字符串创建

CQ dom = CQ.Create(htmlString);

Load synchronously from the web

从web同步加载

CQ dom = CQ.CreateFromUrl("http://www.jquery.com");

Load asynchronously (non-blocking)

异步加载(阻塞)

CQ.CreateFromUrlAsync("http://www.jquery.com", responseSuccess => {
    Dom = response.Dom;        
}, responseFail => {
    ..
});

Run selectors & do jQuery stuff

运行选择器和jQuery之类的东西

var childSpans = dom["div > span"];
childSpans.AddClass("myclass");

the CQ object is like thejQuery object. The property indexer used above is the default method (like $(...).

CQ对象类似于jquery对象。上面使用的属性索引器是默认方法(例如$(…)。

Output:

输出:

string html = dom.Render();

#3


2  

For XML you might use XPath...

对于XML,可以使用XPath……

#4


1  

I'm not entirely clear as to what you're trying to achieve, but if you have a HTML document that you're trying to extract data from, I'd recommend loading it with a parser, and then it becomes fairly trivial to query the object to pull desired elements.

我不太清楚您想要实现什么,但是如果您有一个试图从中提取数据的HTML文档,我建议使用解析器加载它,然后查询对象以提取所需的元素就变得非常简单了。

The parser I linked above allows for use of XPath queries, which sounds like what you are looking for.

上面链接的解析器允许使用XPath查询,这听起来像您要查找的内容。

Let me know if I've misunderstood.

如果我误会了,请告诉我。

#5


1  

Not quite jQuery like, but this may help: http://www.codeplex.com/htmlagilitypack

不太喜欢jQuery,但这可能会有所帮助:http://www.codeplex.com/htmlagilitypack