簡體   English   中英

使用jsoup從另一個div類中提取div類

[英]Extracting div class from within another div class with jsoup

我正在嘗試從另一個div類中的div類提取href。 我嘗試使用的一個代碼片段的示例是:

<div class="productData"> 
           <div class="productTitle">
            <a href="https://rads.stackoverflow.com/amzn/click/com/0786866020" rel="nofollow noreferrer"> Fish! A Remarkable Way to Boost Morale and Improve Results</a> 
            <span class="ptBrand">by <a href="/Stephen-C.-Lundin/e/B001H6UE16">Stephen C.     Lundin</a>, <a href="/Harry-Paul/e/B001H9XQJA">Harry Paul</a>, <a href="/John-    Christensen/e/B003VKXJ04">John Christensen</a> and Ken Blanchard</span>
            <span class="binding"> (<span class="format">Hardcover</span> - Mar. 8, 2000)    </span>
           </div> 

我正在嘗試使用以下代碼從此示例中提取innter類productTitle:

Document doc = Jsoup.connect(fullST).timeout(10*1000).get();
            Element title = doc.getElementById("div.productTitle");
            System.out.println(title);

我得到空。 嘗試提取更高的元素,例如:

Element title = doc.getElementById("div.productData");

我也得到空。 我嘗試了許多代碼組合,但無法弄清楚從內部div類或內部id中提取的語法。

任何幫助,將不勝感激。

您正在嘗試使用getElementById()通過ID選擇元素。 錯了 那些div沒有ID。 相反,它們具有一個類名。 您應該改用select()方法。

Element title = doc.select("div.productTitle").first();

請注意,類名選擇器不一定返回單個元素。 文檔中可以有多個。 我假設您需要第一個也是唯一的Element ,所以我在示例中添加了first()調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM