码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-11-14 11:03:43      阅读:354      评论:0      收藏:0      [点我收藏+]

标签:ace   list   readline   create   exist   cond   print   line   --   

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学习随笔(2)--爬虫--天气预报

标签:ace   list   readline   create   exist   cond   print   line   --   

原文地址:https://www.cnblogs.com/AtesetEnginner/p/9956642.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!