首页 > 解决方案 > JSoup crawling how to crawl from same tag but two items

问题描述

For explaining

I need to crawl all three items in span tag. i have some code but i need a few hints. This is my code until now.

News n = new News();
n.setHeadline(news.getElementsByTag("h2").first().text());
n.setTypeOfSport(news.getElementsByTag("span").first().text());

n.setDate(news.getElementsByTag("span").);

n.setTime(news.getElementsByTag("span").);

标签: javajsoup

解决方案


It looks like you want to pick all span elements from <div class="info"> and access them based on their position (index).

Assuming that your news variable is of type Document or Element(s) you should have access to select(CSSquery) method. If it also at some level holds this <div class="info"> your code can look like:

Elements spans = news.select("div.info span");

//now you can get and handle text from all spans via
spans.get(0).text();
spans.get(1).text();
spans.get(2).text();

For more info about selecting elements using CSS see https://jsoup.org/cookbook/extracting-data/selector-syntax


推荐阅读