如何使用 symfony dom 爬虫使用其 id 或标签选择链接? [英] How to select a link using its id or its label with the symfony dom crawler?

查看:21
本文介绍了如何使用 symfony dom 爬虫使用其 id 或标签选择链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 symfony 爬虫使用其 id 或类来选择链接?

Is it possible to select a link using its id or its class with the symfony crawler?

我试过了:

$crawler()->selectLink('#return-button')->link();
$crawler()->selectLink('.btn.return')->link();

但我有错误:

InvalidArgumentException: 当前节点列表为空.

InvalidArgumentException: The current node list is empty.

选择器是否只使用 a 标签的内容?

Does the selector only works using the content of the a tag?

推荐答案

是的,它仅适用于链接文本或 alt 属性(如果您的链接是图像).

Yes, it only works with the link text or alt attribute if your link is an image.

filter() 方法使用 CssSelector 组件将选择器转换为 XPath 表达式,然后调用 filterRelativeXPath() 就像 selectLink() 确实如此,因此它们返回相同的类型,您应该可以调用

The filter() method uses the CssSelector component to transform a selector into an XPath expression and then calls filterRelativeXPath() just as selectLink() does, so they return the same type and you should be able to just call

$crawler->filter('#return-button')->link();

对于返回多个匹配项的类选择器,由于 link() 仅适用于第一个节点,因此您需要调用 links() 代替:

In case of a class selector that returns multiple matches, since link() only works on the first node, you'll need to call links() instead:

$crawler->filter('.btn.return')->links();

这篇关于如何使用 symfony dom 爬虫使用其 id 或标签选择链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆