Html parsing with JSoup

后端 未结 5 1029
天涯浪人
天涯浪人 2021-01-01 08:17

I am trying to parse the html of the following URL:

http://ocw.mit.edu/courses/aeronautics-and-astronautics/16-050-thermal-energy-fall-2002/

to obtain the te

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-01 08:30

    Here's a short example:

    // Connect to the website and parse it into a document
    Document doc = Jsoup.connect("http://ocw.mit.edu/courses/aeronautics-and-astronautics/16-050-thermal-energy-fall-2002/").get();
    
    // Select all elements you need (se below for documentation)
    Elements elements = doc.select("div[class=chpstaff] p");
    
    // Get the text of the first element
    String instructor = elements.first().text();
    
    // eg. print the result
    System.out.println(instructor);
    

    Take a look at the documentation of the jsoup selector api here: Jsoup Codebook
    Its not very difficult to use but very powerful.

提交回复
热议问题