How to select the first n elements in XPath

南楼画角 提交于 2019-12-24 17:33:13

问题


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

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

I have the following query:

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

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

YQL console: open YQL console with above XPath

Is there anything wrong with my XPath query ?

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


回答1:


This XPath expression will select the first 5 img elements:

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

Here's the whole YQL query:

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

You can watch it work on the YQL Console.



来源:https://stackoverflow.com/questions/26221654/how-to-select-the-first-n-elements-in-xpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!