无法从java中的站点中提取子div

时间:2013-08-06 08:09:36

标签: java html jsoup extract

我想从site

中提取数据

例如,当我尝试使用以下代码获取价格时,我不能。

deal.getDetail().setPriceElement(content.select("div#main-new div.buy-now-aligner div.buy-now-price").first());

但我可以从deal.getDetail().setPriceElement(content.select("div#main-new").first());

中提取数据

我无法触及子节目,怎么可能?

1 个答案:

答案 0 :(得分:0)

您正在以错误的方式使用方法first()

查看Jsoup API;

public Element first()
Get the first matched element. 

    Returns: 
    The first matched element, or null if contents is empty. 

这意味着,返回的Element对象是您选择的第一个匹配对象,在您的情况下是第一个buy-now-price div类。

如果您想要该元素的子元素(示例网址中只有一个元素),您可以使用child()方法或children()方法。

第一种方法采用一个参数作为所需子项的索引,第二种方法将Element个对象的集合作为Elements返回。

使用适合您的任何一种。

相关问题