396
技术社区[云栖]
天气预报 获取日出日落的代码
正则两次,group(1)获取时,group(2)获取分,中间以:相连
华哥帮忙写了一个LIST,获取GROUP的值,List再依次读出
package com.zzk.cn;
import java.awt.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestSun {
public static void main(String[] args) {
URL url = null;
URLConnection conn = null;
InputStream in = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
url = new URL("https://www.weather.com.cn/weather/101010100.shtml");
conn = url.openConnection();
in = conn.getInputStream();
isr = new InputStreamReader(in,"utf8");
br = new BufferedReader(isr);
String line = "";
String info="";
while (null != (line = br.readLine())) {
//System.out.println(line);
info+=line;
}
//System.out.println(info);
Pattern p=Pattern.compile("div\\sclass=.weatherTopright.*?</div>");//正则获取网页大致信息
Matcher m=p.matcher(info);
String s1="";
while(m.find()) {
s1=m.group();
//System.out.println(s1);
}
String s2="";
Pattern p1=Pattern.compile("bold;.>(\\d+):(\\d+)</strong>");//正则获取网页日出日落信息
//Pattern p1=Pattern.compile("<(/?\\s?br\\b)>");
Matcher m1=p1.matcher(s1);
//System.out.println(s1);
ArrayList<String> a = new ArrayList<String>();
while(m1.find()) {
//m1.group(1);
String s=m1.group(1);
String s0=m1.group(2);
String result = s+":"+s0;
a.add(result);
}
System.out.println(a.get(0));
System.out.println(a.get(1));
// System.out.println(a.get(2));
// System.out.println(a.get(3));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != br) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
br = null;
try {
if (null != isr) {
isr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
isr = null;
try {
if (null != in) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
in = null;
}
}
}
输出:
07:31
16:51
最后更新:2017-04-02 06:52:16
上一篇:
关于MySQL的boolean和tinyint(1)
下一篇:
MySQL Innodb数据库性能实践——热点数据性能
CCAI2017 | 王坚博士现场致辞:人工智能是推动科技发展的动力
阿里云与Intel联合发布加密计算,亚洲首个云上“芯片级”数据保护
阿里无人零售店“淘咖啡”原来长这样...未来线下购物要看脸了?
Ubuntu 12.10 启用 Root 登录
Android提高应用篇之模拟信号示波器
负载均衡进阶:SLB常见问题解决方法
通过阿里云注册的域名怎么设置解析?如何登录万网域名控制台管理域名?
自创数据集,用TensorFlow预测股票教程 !(附代码)
Eclipse Java注释模板设置详解
Jsp中使用绝对路径${pageContext.request.contextPath }