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 rain, or overcast, or sunny).

Also to keep on screen how long the program was running.

My decision course:

Chose a site: http://www.travel.ru/weather/russia/

Connected library: jsoup

Does not work :

Pull out the name of the city from the tr tag and put it into a variable.

Pull out the weather forecast and put it into a variable.

In general, Houston, we have problems.

My code is:

package devjatnadcat; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; public class Devjatnadcat1{ public static void main(String[] args) throws IOException { List<Article> articleList = new ArrayList<>(); Document doc = Jsoup.connect("http://travel.ru/weather/russia/").get(); Elements trElements = doc.getElementsByAttributeValue("class", "b-table_row b-forecast"); trElements.forEach(trElement ->{ Element }); } } class Article{ private String url; private String name; public Article(String url,String name){ this.url=url; this.name=name; } public String getUrl(){ return url; } public void setUrl(){ this.url=url; } public String getName(){ return name; } public void setName(){ this.name=name; } } 
  • so what's the problem? - Vartlok
  • @ Vartlok Does not work: Pull out the name of the city from the tr tag and put it into a variable. Pull out the weather forecast and put it into a variable. In general, Houston, we have problems. - Marat Zimnurov
  • some kind of suspicious code inside the lambda trElement ->{ Element } : D - Sublihim
  • specify the question. make it narrower. for example, how to get the contents of an HTML element using JSOUP. check jsoup. - Mikhail Vaysman
  • @MikhailVaysman is a question-task, and the use of any libraries are ways to solve the problem, I’m not going to solve this problem only with JSOUP. - Marat Zimnurov

1 answer 1

I do not really understand what the problem is, but if you do this:

 trElements.forEach(trElement ->{ Elements elementsByClass = trElement.getElementsByClass("b-table_cell"); // выбираем элементы по классу b-table_cell System.out.print(elementsByClass.get(0).text() + " "); // первая колонка - Город // Парсим вторую колонку Element element = elementsByClass.get(1); String temp = element.getElementsByClass("b-forecast_temp").text(); // температура String precipitation = element.getElementsByClass("b-forecast_description").text(); // осадки System.out.println(temp + " " + precipitation); }); 

That output will be:

Yalta +1 / -1 snow
Irkutsk -8 / -17 clear
Krasnaya Polyana +4 / 0 Chance of Rain
Yakutsk -34 / -36 cloudy
Murmansk -1 / -4 partly cloudy
Vyborg +2 / +1 snow
Salekhard -18 / -25 partly cloudy
Yuzhno-Sakhalinsk -9 / -19 cloudy with clear clouds
Rostov the Great -4 / -6 cloudy
Armavir +1 / 0 cloudy

Then you can go deeper and disassemble each column, pull out degrees and precipitation separately. How to work with JSOUP can be found here .

  • how to sort the result in alphabetical order? - Marat Zimnurov
  • @ MaratZimnurov I don’t know what kind of data structure you have in the end, but the strings have a compareTo method - en.stackoverflow.com/a/494097/177499 - Vartlok
  • well thank you. Concerning the task: the conclusion is also needed how long the program has worked. I know two ways System.nanoTime () and system.currenttimemillis () but they give great value. Which way to round? - Marat Zimnurov
  • Personally, I always count in milliseconds. Well, for 1k + iterations. And if you want to ask a separate question, all the same CO is the knowledge base. - Vartlok
  • according to the condition of the problem written above, there is what you need to display the execution time) - Marat Zimnurov