天氣預報 正則獲取指數信息 group(0)指數 group(1)概要 group(2)詳細指數 代碼
如果出現正則內容出現東西過多,再增加一個(),group一下
完全原創,嘔心瀝血呀
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)); Pattern p2=Pattern.compile("div\\sclass=.todayLiving.*?</div>");//正則獲取網頁大致信息 Matcher n=p2.matcher(info); String t1=""; while(n.find()) { t1=n.group(); //System.out.println(t1); } String t2=""; Pattern p3=Pattern.compile("<blockquote>\\s(.*?)\\s</blockquote>");//正則獲取網頁日出日落信息 //Pattern p1=Pattern.compile("<(/?\\s?br\\b)>"); ArrayList<String> b = new ArrayList<String>(); Matcher n1=p3.matcher(t1); while(n1.find()) { t2=n1.group(); //System.out.println(n1.group()); String detail=t2; b.add(detail); } String guoming=b.get(0).replaceAll("<blockquote>", "").replaceAll("</blockquote>", "").replaceAll(" ", ""); String wuran=b.get(1).replaceAll("<blockquote>", "").replaceAll("</blockquote>", "").replaceAll(" ", ""); String ganmao=b.get(2).replaceAll("<blockquote>", "").replaceAll("</blockquote>", "").replaceAll(" ", ""); String ziwaixian=b.get(3).replaceAll("<blockquote>", "").replaceAll("</blockquote>", "").replaceAll(" ", ""); String chuanyi=b.get(4).replaceAll("<blockquote>", "").replaceAll("</blockquote>", "").replaceAll(" ", ""); String shushi=b.get(5).replaceAll("<blockquote>", "").replaceAll("</blockquote>", "").replaceAll(" ", ""); String huazhuang=b.get(6).replaceAll("<blockquote>", "").replaceAll("</blockquote>", "").replaceAll(" ", ""); String meifa=b.get(7).replaceAll("<blockquote>", "").replaceAll("</blockquote>", "").replaceAll(" ", ""); String xiche=b.get(8).replaceAll("<blockquote>", "").replaceAll("</blockquote>", "").replaceAll(" ", ""); String lukuang=b.get(9).replaceAll("<blockquote>", "").replaceAll("</blockquote>", "").replaceAll(" ", ""); String jiaotong=b.get(10).replaceAll("<blockquote>", "").replaceAll("</blockquote>", "").replaceAll(" ", ""); String lvyou=b.get(11).replaceAll("<blockquote>", "").replaceAll("</blockquote>", "").replaceAll(" ", ""); String yundong=b.get(12).replaceAll("<blockquote>", "").replaceAll("</blockquote>", "").replaceAll(" ", ""); String chenlian=b.get(13).replaceAll("<blockquote>", "").replaceAll("</blockquote>", "").replaceAll(" ", ""); String diaoyu=b.get(14).replaceAll("<blockquote>", "").replaceAll("</blockquote>", "").replaceAll(" ", ""); String huachuan=b.get(15).replaceAll("<blockquote>", "").replaceAll("</blockquote>", "").replaceAll(" ", ""); String yuehui=b.get(16).replaceAll("<blockquote>", "").replaceAll("</blockquote>", "").replaceAll(" ", ""); String guangjie=b.get(17).replaceAll("<blockquote>", "").replaceAll("</blockquote>", "").replaceAll(" ", ""); String liangshai=b.get(18).replaceAll("<blockquote>", "").replaceAll("</blockquote>", "").replaceAll(" ", ""); String yusan=b.get(19).replaceAll("<blockquote>", "").replaceAll("</blockquote>", "").replaceAll(" ", ""); String t3=""; Pattern p4=Pattern.compile("<dt>(.*?)</dt>.*?target=.*?blank.*?>(.*?)<b\\sclass=.*?>(.*?)</b>");//正則獲取網頁日出日落信息 //Pattern p1=Pattern.compile("<(/?\\s?br\\b)>"); ArrayList<String> c = new ArrayList<String>(); Matcher n2=p4.matcher(t1); while(n2.find()) { t3=n2.group(2); System.out.println(n2.group(3)); String index=t3; c.add(index); } String iguoming=c.get(0).replaceAll(" ", ""); String iwuran=c.get(1).replaceAll(" ", ""); String iganmao=c.get(2).replaceAll(" ", ""); String iziwaixian=c.get(3).replaceAll(" ", ""); String ichuanyi=c.get(4).replaceAll(" ", ""); String ishushi=c.get(5).replaceAll(" ", ""); String ihuazhuang=c.get(6).replaceAll(" ", ""); String imeifa=c.get(7).replaceAll(" ", ""); String ixiche=c.get(8).replaceAll(" ", ""); String ilukuang=c.get(9).replaceAll(" ", ""); String ijiaotong=c.get(10).replaceAll(" ", ""); String ilvyou=c.get(11).replaceAll(" ", ""); String iyundong=c.get(12).replaceAll(" ", ""); String ichenlian=c.get(13).replaceAll(" ", ""); String idiaoyu=c.get(14).replaceAll(" ", ""); String ihuachuan=c.get(15).replaceAll(" ", ""); String iyuehui=c.get(16).replaceAll(" ", ""); String iguangjie=c.get(17).replaceAll(" ", ""); String iliangshai=c.get(18).replaceAll(" ", ""); String iyusan=c.get(19).replaceAll(" ", ""); //System.out.println(iguoming); } 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:32
16:52
天氣較好,您在出門的時候無須帶雨傘。
極不易發
良
易發
弱
寒冷
較不舒適
保濕
一般
較適宜
幹燥
良好
一般
較不宜
較不宜
不宜
不適宜
較不適宜
較不宜
基本適宜
不帶傘
不帶傘
最後更新:2017-04-02 06:52:16