java爬虫获取天气信息并发送短信。

java爬虫获取天气信息并发送短信:

   自己谷脑java获取网页信息,想着顺便发送短信给自己实现一个天气短信提醒的小玩意。可加入自己项目中,做个定时任务。完善这个小玩意。:

   需要SMS平台的注册。获取到key;可自己查看SMS短信发送平台,恶心的就是  免费的只有5条短信。仅供自己娱乐。

    所需jar

  <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.10.2</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
<dependency>
    <groupId>commons-httpclient</groupId>
    <artifactId>commons-httpclient</artifactId>
    <version>3.1</version>
</dependency>

public class Test {
	public static void main(String[] args) throws Exception {
		//定义map封装获取的数据;
		 HashMap<String, Object> map=new HashMap<String, Object>();
		 
		CloseableHttpClient httpClient = HttpClients.createDefault();
		//发送get请求
		HttpGet get = new HttpGet("http://m.weather.com.cn/d/town/index?lat=39.9219&lon=116.44355");
		//模拟浏览器添加请求头
		get.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36");
		CloseableHttpResponse response = httpClient.execute(get);
		
		//获取请求网页的返回状态码 如:200
		int code = response.getStatusLine().getStatusCode();
		System.out.println("状态码:"+code);
		//将请求数据转为entity;
		HttpEntity entity = response.getEntity();
		//网页内容
		 String content = EntityUtils.toString(entity,"UTF-8");//转化为字符串并设置编码类型;
		 
		//运用Jsoup解析html内容得到document对象。
		 Document document = Jsoup.parse(content);
		//根据网页标签获取内容 如:title     .first 取第一个值
		Element element = document.getElementsByTag("title").first();
		map.put("标题:", element.text());
		System.out.println("title"+element.text());
		//jsoup的选择器。类似css 获取页面上的你需要的信息的所在标签。一级一级从上往下查找 如: .cityName h2        是class="cityName" 下一级h2标签下的文字信息;
		Elements select = document.select(".wrap");
		//遍历。
	    for(Element s: select){
	    	map.put("城市:", s.text());
	    }
		Elements elements = document.select(".n_wd");
	    for(Element o: elements){
	    	map.put("气温:", o.text());
	    }
	    System.out.println(map.toString());
	   sendMsg(map.toString(), "手机号"); 
	}
	public static boolean sendMsg(String msg, String phone) throws HttpException, IOException {
		String url = "http://utf8.api.smschinese.cn/";
		// Uid=本站用户名&Key=接口安全秘钥&smsMob=手机号码&smsText=验证码:8888
		HttpClient client = new HttpClient();
		HttpConnectionManagerParams params = client.getHttpConnectionManager().getParams();
		params.setConnectionTimeout(6000);
		params.setSoTimeout(20000);


		PostMethod method = new PostMethod(url);
		method.getParams().setHttpElementCharset("UTF-8");
		method.getParams().setContentCharset("UTF-8");
		method.getParams().setCredentialCharset("UTF-8");


		method.addParameter("Uid", "*****");//你申请的SMS的名字
		method.addParameter("Key", "*****");
		method.addParameter("smsMob", phone);
		method.addParameter("smsText", msg);


		client.executeMethod(method);
		String string = method.getResponseBodyAsString();
		System.err.println(string);


		return true;
	}
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值