案例1:获取天气预报信息
需求说明
搭建开发环境,实现从“”中获取当地天气预报信息,从控制台输出结果
分析
访问网址:
分析网站URL、文档内容特征
获取网页内容
拆分出需求内容
控制台输出结果
import java.io.BufferedReader;import java.io.InputStreamReader;import .URL;import java.util.regex.Matcher;import java.util.regex.Pattern;public class WeatheDemo {public static void main(String[] args) {// 根据网页结构编写正则,创建pattern对象Pattern p_city = pile("<span class=\"weather2-item\" data-hook=\"name\">(.*?)</span>");Pattern p_info_today = pile("<div class=\"weather2__info-txt\" data-hook=\"weather\" slider-type=\"item\">(.*?)</div>");Pattern p_info_temperature_today = pile("<div class=\"weather2__temperature\" data-hook=\"tempera\">(.*?)</div>");Pattern p_info_tomorrow = pile("<div class=\"weather2__info-txt\" data-hook=\"weather-tomorrow\">(.*?)</div>");Pattern p_info_temperature_tomorrow = pile("<div class=\"weather2__temperature\" data-hook=\"tempera-tomorrow\">(.*?)</div>");Matcher m_city;Matcher m_info_today;Matcher m_info_temperature_today;Matcher m_info_tomorrow;Matcher m_info_temperature_tomorrow;System.out.println("------------------开始获取天气信息-------------------");try {// 创建页面的url对象URL url = new URL("");// 创建网络读取流BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "utf8"));// 读取网络内容网络流BufferReaderString str = null;while ((str = reader.readLine()) != null) {m_city = p_city.matcher(str.toString());m_info_today = p_info_today.matcher(str.toString());m_info_temperature_today = p_info_temperature_today.matcher(str.toString());m_info_tomorrow = p_info_tomorrow.matcher(str.toString());m_info_temperature_tomorrow = p_info_temperature_tomorrow.matcher(str.toString());// 获取地区Boolean isEx = m_city.find();if (isEx) {String city = m_city.group();// 清洗得到的数据city = city.replace("<span class=\"weather2-item\" data-hook=\"name\">","").replace("</span>", "");System.out.println("城市:" + city);}// 获取今日天气Boolean isEx1 = m_info_today.find();if (isEx1) {String info_today = m_info_today.group();// 清洗得到的数据info_today = info_today.replace("<div class=\"weather2__info-txt\" data-hook=\"weather\" slider-type=\"item\">","").replace("</div>", "");System.out.println("今日天气:" + info_today);}// 获取今日温度Boolean isEx2 = m_info_temperature_today.find();if (isEx2) {String temperature_today = m_info_temperature_today.group();// 清洗得到的数据temperature_today = temperature_today.replace("<div class=\"weather2__temperature\" data-hook=\"tempera\">","").replace("</div>", "");System.out.println("今日温度:" + temperature_today);}// 获取明天天气Boolean isEx3 = m_info_tomorrow.find();if (isEx3) {String info_tomorrow = m_info_tomorrow.group();// 清洗得到的数据info_tomorrow = info_tomorrow.replace("<div class=\"weather2__info-txt\" data-hook=\"weather-tomorrow\">","").replace("</div>", "");System.out.println("明日天气:" + info_tomorrow);}// 获取明天温度Boolean isEx4 = m_info_temperature_tomorrow.find();if (isEx4) {String temperature_tomorrow = m_info_temperature_tomorrow.group();// 清洗得到的数据temperature_tomorrow = temperature_tomorrow.replace("<div class=\"weather2__temperature\" data-hook=\"tempera-tomorrow\">","").replace("</div>", "");System.out.println("明日温度:" + temperature_tomorrow);}}reader.close();} catch (Exception e) {System.out.println("下载失败");e.printStackTrace();}System.out.println("-----------------完成天气信息获取--------------------");}}
运行结果示例:
超全面的测试IT技术课程,0元立即加入学习!有需要的朋友戳:
腾讯课堂测试技术学习地址
欢迎转载,但未经作者同意请保留此段声明,并在文章页面明显位置给出原文链接。