我想从以下网址获取一些数据,例如sunriseTime,outTemp,outsideHumidity等。如果你知道我怎么做,请帮助我。
答案 0 :(得分:1)
您需要执行以下操作:
我编写了一个Java程序来执行上述操作。希望它有所帮助:)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class URLReader {
public static List<String> getAttributesFromURL(String urlString,
String [] keys) {
StringBuilder sb = new StringBuilder();
try {
//Opening the URL
URL url = new URL(urlString);
URLConnection urlConnection = url.openConnection();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream()));
// Reading the URL contents into a StringBuilder object
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line + "\n");
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
// Copying the StringBuilder contents into a string
String contents = sb.toString();
List<String> keyList = Arrays.asList(keys);
List<String> result = new ArrayList<String>();
for (String key : keyList) {
// Defining the pattern to be found in contents
String patternToFind = key + "=\"(.*)\"";
Pattern p = Pattern.compile(patternToFind);
Matcher m = p.matcher(contents);
if (m.find()) {
// If pattern found, store them in the result list
result.add(key + ": " + m.group(1).trim());
}
}
// Returning the result
return result;
}
public static void main(String[] args) throws java.lang.Exception {
String urlString ="http://www.metar.gr/ws/alexan/ZWNTANA.htm";
// List all the keys whose values need to be extracted
String[] keys = {"sunriseTime", "outsideTemp", "outsideHumidity"};
// Getting the result
List<String> attributeValuePairs = getAttributesFromURL(urlString, keys);
System.out.println(attributeValuePairs);
}
}
上述程序的输出将是:
[sunriseTime: 6:14, outsideTemp: 21.7, outsideHumidity: 57]
您可以通过在程序的keys
数组中指定属性名称来获取所需的其他属性的值。