简体   繁体   English

Java jSoup元素替换缺少的元素

[英]Java jSoup Elements Replace Missing Element

I'm crawling a website using jSoup to obtain information from the adverts it displays. 我正在使用jSoup爬网网站,以从其显示的广告中获取信息。 Most the adverts contain common elements but for some they are missing. 大多数广告包含共同的元素,但对于某些广告它们却不存在。 I'm trying to crawl for all of the adverts, whilst including those that don't share the common elements, but I'm struggling. 我正在尝试抓取所有广告,同时包括那些不具有共同要素的广告,但我一直在努力。 Here's the basics of what I've got: 这是我所拥有的基本知识:

    Elements gene = doc.select("div.item");

    for (Element c : gene) {

                    Elements monk1 = c.select("li.cool");
                    Elements monk2 = c.getElementsByAttributeValueContaining("src", "/images/notthere.gif");

                    if (monk1.isEmpty()) {
                       monk1 = monk2;
                    }
                    String[] price = {monk1.text()};
                    model1.setValueAt(price[0], xf, 2);
                    xf++;
                    xf++;

                }

It looks for elements within the elements "gene" in the HTML. 它在HTML的元素“ gene”中寻找元素。 "li.cool" is present in most of the "gene" elements, but for some its missing. “ li.cool”存在于大多数“基因”元素中,但有些缺失。 For each time "li.cool" is missing, I want to replace the null element with a simple string. 每当缺少“ li.cool”时,我都想用一个简单的字符串替换null元素。 How do I do this?? 我该怎么做呢??

You can add text using text() method. 您可以使用text()方法添加文本。

if(monk1 == null)
monk1.text("string")

You can view the explanation here . 您可以在此处查看说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM