如何在XPath中选择前n个元素 [英] How to select the first n elements in XPath

查看:128
本文介绍了如何在XPath中选择前n个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 YQL 从网站上抓取一些图像.

I am using YQL to scrape some images from a website.

问题是我只想要该网站的前 5 张图片.

The problem is I want only the first 5 images from that website.

我有以下查询:

select * from html where url="http://myanimelist.net/anime/9253/Steins;Gate" and xpath='//img[position()<=5]'

但是,它返回所有图像元素而不是前 5 个.

But, it is returning all image elements instead of the first 5.

YQL 控制台:使用以上 XPath 打开 YQL 控制台

我的 XPath 查询有什么问题吗?

Is there anything wrong with my XPath query ?

PS:我不能使用 LIMIT 5 因为我可能还需要抓取一些其他标签.

PS: I cannot use LIMIT 5 since I may need to scrape some other tags too.

推荐答案

这个 XPath 表达式将选择前 5 个 img 元素:

This XPath expression will select the first 5 img elements:

//img[count(preceding::img) < 5]

这是整个 YQL 查询:

select * from html where url="http://myanimelist.net/anime/9253/Steins;Gate" and xpath='//img[count(preceding::img) < 5]'

您可以在 YQL 控制台.

这篇关于如何在XPath中选择前n个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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