2017-04-03 84 views
1

我想解析https://www.avito.ru/voronezh。在页有50个元素:在Java中使用JSoup解析CSS

<a class="item-description-title-link" href="/voronezh/mebel_i_interer/kuhni_1132166621" title="Кухни в Воронеже"> 
Кухни 
</a> 

但这些元素具有以下CSS属性:

element.style { 
} 
7cad82b….css?b0b9a4e:1 
.item_table.item-highlight .item-description-title-link { 
    background-color: #fbfe23; 
    line-height: 19px; 
    color: #0091d9; 
    margin-top: -1px; 
    padding: 2px 3px 0; 
    display: inline-block; 
} 

我怎样才能得到只有background-color: #fbfe23;元素?

回答

0

它似乎你正在尝试使用Jsoup。这是一个使用这个库的解决方案。我在页面中获取了类item-description-title-link的所有元素。

import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.select.Elements; 
import java.io.IOException; 
import java.text.ParseException; 


public class SouSoup { 
    private static String url = "https://www.avito.ru/voronezh"; 

    /* Get Data */ 
    public static void getData() throws IOException, ParseException { 
     System.out.println("Fetching file"); 
     Document doc = Jsoup.connect(url).get(); 
     Elements res=doc.getElementsByClass("item-description-title-link"); 
     for (Element elt : res) { 
      System.out.println(res.text()); 
      } 
    } 

    public static void main(String[] args) throws IOException, ParseException{ 
     SouSoup.getData(); 
    } 
}