天气预报 正则获取指数信息 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("http://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
天气较好,您在出门的时候无须带雨伞。
            极不易发            
            良            
            易发            
            弱            
            寒冷            
            较不舒适            
            保湿            
            一般            
            较适宜            
            干燥            
            良好            
            一般            
            较不宜            
            较不宜            
            不宜            
            不适宜            
            较不适宜            
            较不宜            
            基本适宜            
            不带伞            
不带伞


目录
相关文章
|
10月前
|
算法 数据挖掘 API
终于让我找到支持任意经纬度生活指数查询API 了
终于让我找到支持任意经纬度生活指数查询API 了
88 0
终于让我找到支持任意经纬度生活指数查询API 了
|
11月前
|
iOS开发 索引
LeetCode--1773. 统计匹配检索规则的物品数量
给你一个数组 items ,其中 items[i] = [typei, colori, namei] ,描述第 i 件物品的类型、颜色以及名称。 另给你一条由两个字符串 ruleKey 和 ruleValue 表示的检索规则。 如果第 i 件物品能满足下述条件之一,则认为该物品与给定的检索规则 匹配 : ruleKey == "type" 且 ruleValue == typei 。 ruleKey == "color" 且 ruleValue == colori 。 ruleKey == "name" 且 ruleValue == namei 。 统计并返回 匹配检索规则的物品数量 。
56 0
|
SQL Oracle 关系型数据库
【SQL开发实战技巧】系列(十五):查找最值所在行数据信息及快速计算总和百之max/min() keep() over()、fisrt_value、last_value、ratio_to_report
本篇文章讲解的主要内容是:***计算部门中那个工资等级的员工最多、通过返回部门10最大工资所在行的员工名称小案例来讲解max/min() keep() over()、通过查询工资最高的人小案例来介绍fisrt_value、last_value、通过计算各个部门的工资合计以及各个部门合计工资占总工资的比例小案例来介绍如何计算百分比及ratio_to_report分析函数的使用***
【SQL开发实战技巧】系列(十五):查找最值所在行数据信息及快速计算总和百之max/min() keep() over()、fisrt_value、last_value、ratio_to_report
【每日一题Day11】LC1773统计匹配检索规则的物品数量
给你一个数组 items ,其中 items[i] = [typei, colori, namei] ,描述第 i 件物品的类型、颜色以及名称。
37 0
|
Windows
【CCCC】L2-005 集合相似度 (25分),维护set数组去重,比较统计
【CCCC】L2-005 集合相似度 (25分),维护set数组去重,比较统计
87 0
|
SQL Oracle 关系型数据库
Oracle 按照每小时分组,求每组数量
Oracle 按照每小时分组,求每组数量
Oracle 按照每小时分组,求每组数量
【CCF-CSP】201712-1-最小差值100分(含详细注释)
【CCF-CSP】201712-1-最小差值100分(含详细注释)
121 0
【CCF-CSP】201712-1-最小差值100分(含详细注释)
|
编译器
PAT (Basic Level) Practice (中文)- 1077 互评成绩计算(20 分)
PAT (Basic Level) Practice (中文)- 1077 互评成绩计算(20 分)
80 0
【TP5.1】查询在数组内的所有数据,并且根据用户id分组,计算消费金额总和大于100的数量
【TP5.1】查询在数组内的所有数据,并且根据用户id分组,计算消费金额总和大于100的数量
165 0
【TP5.1】查询在数组内的所有数据,并且根据用户id分组,计算消费金额总和大于100的数量
|
存储 索引
Google Earth Engine(GEE)——在线统计美国人口和住房数据(以ee.Reducer.sum().repeat().group列表形式呈现)
Google Earth Engine(GEE)——在线统计美国人口和住房数据(以ee.Reducer.sum().repeat().group列表形式呈现)
258 0
Google Earth Engine(GEE)——在线统计美国人口和住房数据(以ee.Reducer.sum().repeat().group列表形式呈现)