Java学习随笔--爬虫--天气预报

Posted atesetenginner

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java学习随笔--爬虫--天气预报相关的知识,希望对你有一定的参考价值。

public class Spiderweather {
public static void main(String[] args) {
List<String> list = null;
BufferedReader bufr = null;
BufferedWriter bufw = null;
try {
bufr = new BufferedReader(new FileReader(new File("D:\bianma.txt")));
list = new ArrayList<String>();
String line = "";
Pattern p = Pattern.compile("\d{2,}");
while ((line = bufr.readLine()) != null) {
Matcher m = p.matcher(line);
while (m.find())
list.add(m.group());
}
} catch (Exception e1) {
e1.printStackTrace();
}
Iterator<String> it = list.iterator();
File file = new File("D:\forecast.txt");
if (!file.exists())
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
bufw = new BufferedWriter(new FileWriter(file));
} catch (IOException e1) {
e1.printStackTrace();
}
String bm = "";
while (it.hasNext()) {
bm = it.next();
String url = "http://www.weather.com.cn/weather/" + bm + ".shtml";
try {
Document doc = Jsoup.connect(url).get();
Elements content = doc.getElementsByClass("con today clearfix");
for (Element e : content) {
Document conDoc = Jsoup.parse(e.toString());
Elements cru = conDoc.getElementsByClass("crumbs fl");
Elements sky = content.select("li[class^=sky skyid lv]");
bufw.write(cru.text());// 地点
bufw.newLine();
for (Element sk : sky) {
bufw.write(sk.text());
bufw.newLine();
}
bufw.newLine();
}
bufw.newLine();
bufw.flush();
} catch (Exception e) {
e.printStackTrace();
}

}
try {
System.out.println("天气查询完毕!!");
if (bufw != null)
bufw.close();
if (bufr != null)
bufr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}




































































以上是关于Java学习随笔--爬虫--天气预报的主要内容,如果未能解决你的问题,请参考以下文章

十一随笔|读书

新年计划随笔

Java学习随笔之9:AWT编程

java多线程学习随笔

Java学习随笔---常用API

第一次的Java随笔