问题
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