java - JSoup crawling how to crawl from same tag but two items
问题描述
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").);
解决方案
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
推荐阅读
- android - 当我在实现设备中运行应用程序并用户单击图像时,我会质疑,它会崩溃。但它不能在模拟器中崩溃
- javascript - 如何访问 if 语句检查值?在 if 块旁边?
- django - Django中数据库的动态菜单
- jquery - 在 AJAX 之后滚动到 div 的底部
- swift - 将 isSelected 设置为 true 不更改图像
- python - Python ZipFle 放气
- javascript - 为什么我的函数没有将表单值推送到数组?
- javascript - 使用 `fetch` API 创建多个 cookie 上下文
- mysql - 根据购买历史在 SQL 中绑定客户 ID
- c++ - 我的扩展消息框 XMessageBox 不刷新自身 - C++