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

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();
}
}
}

猜你喜欢

转载自www.cnblogs.com/AtesetEnginner/p/9956642.html
今日推荐