PHP CSS 选择器库?

发布于 2024-07-08 20:14:32 字数 110 浏览 5 评论 0原文

是否有 PHP 类/库允许我使用 CSS 选择器查询 XHTML 文档? 如果我能以某种方式使用 CSS 选择器(jQuery 宠坏了我!),我需要抓取一些页面来获取非常容易访问的数据。 有任何想法吗?

Is there a PHP class/library that would allow me to query an XHTML document with CSS selectors? I need to scrape some pages for data that is very easily accessible if I could somehow use CSS selectors (jQuery has spoiled me!). Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

寂寞美少年 2024-07-15 20:14:32

进一步谷歌搜索后(最初的结果不是很有帮助),似乎实际上有一个 Zend Framework 库,以及其他一些库:

After Googling further (initial results weren't very helpful), it seems there is actually a Zend Framework library for this, along with some others:

南汐寒笙箫 2024-07-15 20:14:32

XPath 是访问 XML(和 XHTML)节点的相当标准的方法,并且提供比 CSS 更高的精度。

XPath is a fairly standard way to access XML (and XHTML) nodes, and provides much more precision than CSS.

-小熊_ 2024-07-15 20:14:32

一个很棒的组件是 symfony 2 的一个组件, CssSelector\Parser 简介。 它将 CSS 选择器转换为 XPath 表达式。 看看 =)

源代码

A great one is a component of symfony 2, CssSelector\Parser­Introduction. It converts CSS selectors into XPath expressions. Take a look =)

Source code

甜心小果奶 2024-07-15 20:14:32

对于 jQuery 用户来说,最感兴趣的可能是 将 jQuery 移植到 PHP,即 < a href="http://code.google.com/p/phpquery/" rel="noreferrer">phpQuery。 库的几乎所有部分都已移植。 此外,它还包含WebBrowser插件,可用于Web抓取整个站点的路径/进程(例如,访问登录后可用的数据)。 它只是模拟服务器上的 Web 浏览器(还有事件和 cookie)。 最新版本对 XML 命名空间 和 CSS3 "|" 选择器提供了实验性支持。

For jQuery users most interesting may be port of jQuery to PHP, which is phpQuery. Almost all sections of the library are ported. Additionally it contains WebBrowser plugin, which can be used for Web Scraping whole site's path/processes (eg accessing data available after logging in). It simply simulates web browser on the server (events and cookies too). Latest versions has experimental support for XML namespaces and CSS3 "|" selector.

梦屿孤独相伴 2024-07-15 20:14:32

我最终使用了 PHP Query Lite,它非常简单并且拥有我需要的一切。

I ended up using PHP Query Lite, it's very simple and has all I need.

木格 2024-07-15 20:14:32

对于文档解析,我使用 DOM。 如果您知道标签名称(在本例中为“div”),这可以很容易地解决您的问题:

 $doc = new DOMDocument();
 $doc->loadHTML($html);

 $elements = $doc->getElementsByTagName("div");
 foreach ($elements as $e){
  if ($e->getAttribute("class")!="someclass") continue;

  //its a div.classname
 }

不确定 DOM 是否允许您一次获取文档的所有元素...您可能必须进行树遍历。

For document parsing I use DOM. This can quite easily solve your problem if you know the tag name (in this example "div"):

 $doc = new DOMDocument();
 $doc->loadHTML($html);

 $elements = $doc->getElementsByTagName("div");
 foreach ($elements as $e){
  if ($e->getAttribute("class")!="someclass") continue;

  //its a div.classname
 }

Not sure if DOM lets you get all elements of a document at once... you might have to do a tree traversal.

网白 2024-07-15 20:14:32

我写了我的,基于Mootools CSS选择器引擎 http://selectors.svn.exyks.org/。 它依赖于 simplexml 扩展能力(因此,它是只读的)

I wrote mine, based on Mootools CSS selector engine http://selectors.svn.exyks.org/. it rely on simplexml extension ability (so, it's read-only)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文