[英]extract and group elements together with jsoup
我正在嘗試獲得以下輸出:
** * * 電影標題: ** * **
二次世界大戰
** * ** 演員表: ** * ***
布拉德·皮特
米雷耶·埃諾斯(Mireille Enos)
詹姆斯·徽章·戴爾** * * 電影標題: ** * **
怪獸大學
** * ** 演員表: ** * ***
約翰尼·德普
屈臣氏初級
<h2 itemprop="name">World War Z</h2>
<div class=info>1hr 56min - Rated PG13 - Action/Drama/Horror - English<br>
- Cast:
<span itemprop="actors">Brad Pitt</span>,
<span itemprop="actors">Mireille Enos</span>,
<span itemprop="actors">James Badge Dale</span>
</div>
<h2 itemprop="name">Monsters University</h2>
<div class=info>2hr 30min - Rated PG13 - Comedy - English<br>
- Cast:
<span itemprop="actors">Johnny Depp</span>,
<span itemprop="actors">Watsons Junior</span>
</div>
我嘗試這樣做:
Elements movieTitle = doc.select("h2");
for (Element src : movieTitle) {
for (int i = 0; i < movieTitle.size(); ++i) {
title += movieTitle.get(i).text() + "\n";
}
break;
}
Elements casts = doc.select("span[itemprop=actors]");
for (Element sr : casts) {
for (int i = 0; i < casts.size(); ++i) {
cast += casts.get(i).text() + "\n";
}
break;
}
System.out.println("*************Movie Titles:************* \n" + title);
System.out.println("*************Casts:************* \n" + cast);
但是輸出是:
** * * 電影標題: ** * **
二次世界大戰
怪獸大學
** * ** 演員表: ** * ***
布拉德·皮特
米雷耶·埃諾斯(Mireille Enos)
詹姆斯·徽章·戴爾
約翰尼·德普
屈臣氏初級
如何根據電影對演員進行分組?
這將為您提供所需格式的結果。
Elements items = doc.select("h2");
for (Element movieElement : items) {
//Here you get movie name from movieElement
Elements castElemets = movieElement.nextElementSibling().select("span[itemprop=actors]");
//loop through the castElemnts for corresponding Movie
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.