Task: Display a list of 10 cities with the current ambient temperature. List in alphabetical order. Separately display a list of cities where it is snowing now (either it is raining, or overcast, or sunny). Also keep on screen how long the program was running
What is required: To sort the cities in the output alphabetically.
My code is:
package devjatnadcat; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; public class Devjatnadcat1 { public static void main(String[] args) throws IOException { long m = System.currentTimeMillis() / 1000; Document doc = Jsoup.connect("http://travel.ru/weather/russia/").get(); Elements trElements = doc.getElementsByAttributeValue("class", "b-table_row b-forecast"); trElements.forEach(trElement -> { // выбираем элементы по классу b-table_cell Elements elementsByClass = trElement.getElementsByClass("b-table_cell"); // первая колонка - Город System.out.print(elementsByClass.get(0).text() + " "); // Парсим вторую колонку Element element = elementsByClass.get(1); // температура String temp = element.getElementsByClass("b-forecast_temp").text(); System.out.println(temp); }); System.out.println(""); trElements.forEach(trElement -> { // выбираем элементы по классу b-table_cell Elements elementsByClass = trElement.getElementsByClass("b-table_cell"); // первая колонка - Город System.out.print(elementsByClass.get(0).text() + " - "); // Парсим вторую колонку Element element = elementsByClass.get(1); // осадки String precipitation = element.getElementsByClass("b-forecast_description").text(); System.out.println(precipitation); }); System.out.println("Время выполнения : " + ((double) (System.currentTimeMillis() - m))); } }