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; } }