[英]Extracting href from a class within other div/id classes with jsoup
[英]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.