我正在编写解析天气站点的代码。
我在网站上找到了一个包含所需数据的CSS类。如何从那里拾取一个字符串形式的“10月12日”?(10月12日星期二)
public class Pars {
private static Document getPage() throws IOException {
String url = "https://www.gismeteo.by/weather-mogilev-4251/3-day/";
Document page = Jsoup.parse(new URL(url), 3000);
return page;
}
public static void main(String[] args) throws IOException {
Document page = getPage();
Element Nameday = page.select("div [class=date date-2]").first();
String date = Nameday.select("div [class=date date-2").text();
System.out.println(Nameday);
}
}该代码是为了解析天气站点而编写的。在页面上,我找到了正确的类,其中只需要日期和星期几。但在从类转换数据的阶段,错误会崩溃为字符串。
发布于 2021-10-10 18:16:26
问题出在类选择器上,它应该是这样的:div.date.date-2
工作代码示例:
public class Pars {
private static Document getPage() throws IOException {
String url = "https://www.gismeteo.by/weather-mogilev-4251/3-day/";
return Jsoup.parse(new URL(url), 3000);
}
public static void main(String[] args) throws IOException {
Document page = getPage();
Element dateDiv = page.select("div.date.date-2").first();
if(dateDiv != null) {
String date = dateDiv.text();
System.out.println(date);
}
}
}这是你问题的答案:Jsoup select div having multiple classes
以后,请确保您的问题更加详细和结构合理。这里是“问问题”的指导方针:https://stackoverflow.com/help/how-to-ask
https://stackoverflow.com/questions/69517630
复制相似问题