使用JSoup框架,我試圖遍歷div的下面,並將每個<p>
標籤中的文本提取到數組中。由於<div>
和<p>
的列表是無限長的,所以do/while循環或for循環將是獲取<p>
中信息的首選方法。JSoup - 通過標籤/數組遞增
我不知道如何遍歷下面的<div>
標籤,因爲我不知道如何跟蹤我正在存儲到數組中的<p>
標籤。如果答案是顯而易見的,我會抱歉,因爲我對Java和編程有點新鮮。
非常感謝您的幫助。讓我知道,如果有什麼我可以補充,這將有助於。
例HTML(假設重複幾百次):
<div class="happy-div"> // want everything within this div to be in one array element
<p>good text here.</p>
<p>More good Text here.</p>
<p>Some good stuff here.</p>
</div>
<div class="sad-div"> // want everything within this div to be in a separate array element
<p>Some unhappy text here.</p>
<p>More unhappy Text here.</p>
<p>Some unhappy stuff here.</p>
</div>
<div class="depressed-div"> // everything within this div to be in a separate array element
<p>Some melancholy text here.</p>
<p>More melancholy Text here.</p>
<p>Some melancholy stuff here.</p>
</div>
.... repeats hundreds of times
僞代碼:
String[] arrayOfP;
for (int i = 0; i < numberOfDivs; i++)
{
arrayOfP[i] = doc.select("All of the text in the <p> tags within the div we've incremented to")
System.out.println(arrayOfP[i])
}
預期結果:
當打印字符串的內容數組元素值,I w烏爾德希望看到這一點:
arrayofP[1] Some good text here. More good Text Here. Some good stuff here.
arrayofP[2] Some unhappy text here. More unhappy Text Here. Some unhappy stuff here.
arrayofP[3] Some melancholy text here. More melancholy Text Here. Some melancholy stuff here.
....
後樣品數組值。 – newuser
我澄清了'預期結果'領域。這有幫助嗎? –