Java get Web page specified content-2 (practice + Modify)

Source: Internet
Author: User

ImportJava.io.BufferedReader;ImportJava.io.InputStreamReader;Importjava.net.HttpURLConnection;ImportJava.net.URL;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;Importjava.util.Arrays; Public classWeather {String urlstring; String Array; StringBuffer SB=NewStringBuffer ("");  Public Static voidMain (string[] args)throwsException {Weather client=NewWeather ("http://www.weather.com.cn/weather/101181201.shtml"); Client.run (); }  PublicWeather (String urlstring) { This. urlstring =urlstring;}  Public voidRun ()throwsException {URL url=NewURL (urlstring); HttpURLConnection URLConnection=(HttpURLConnection) url.openconnection (); BufferedReader Reader=NewBufferedReader (NewInputStreamReader (URLConnection. getInputStream (),"UTF8"));  String Line;  while(line = Reader.readline ())! =NULL) {Pattern P= Pattern.compile ("<p class=\" Wea\ "> (. +?) </p> "); Matcher m=P.matcher (line);  while(M.find ()) {array= M.group (1); Sb.append (Array+","); }} String arr=sb.tostring (); String[] s= Arr.split ("\ \,"); System.out.println (S[s.length-7]); } }

UTF8 encoding format

ImportJava.io.BufferedReader;ImportJava.io.InputStreamReader;Importjava.net.HttpURLConnection;ImportJava.net.URL;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;Importjava.util.Arrays; Public classWeather {String urlstring; String Array; StringBuffer SB=NewStringBuffer ("");  Public Static voidMain (string[] args)throwsException {Weather client=NewWeather ("http://www.weather.com.cn/weather/101181201.shtml"); Client.run (); }  PublicWeather (String urlstring) { This. urlstring =urlstring;}  Public voidRun ()throwsException {URL url=NewURL (urlstring); HttpURLConnection URLConnection=(HttpURLConnection) url.openconnection (); BufferedReader Reader=NewBufferedReader (NewInputStreamReader (URLConnection. getInputStream (),"UTF8"));  String Line;  while(line = Reader.readline ())! =NULL) {Pattern P= Pattern.compile ("<p class=\" Wea\ "> (. +?) </p> "); Matcher m=P.matcher (line);  while(M.find ()) {array= M.group (1); Sb.append (Array+",");//The regular data is appended to SB and separated by commas}} String arr= Sb.tostring ();//SB to StringString[] s = arr.split ("\ \,");//string to array, comma as tagSystem.out.println (s[s.length-7]);//take the 7th count in the array     } }

Java get Web page specified content-2 (practice + Modify)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.