我想在该网页http://finance.yahoo.com/q/cp?s=^GSPC&alpha=Z上提取Zions Bancorporation和ZIMMER HOLDINGS INC数据。。怎么做呢?
我写的代码(总得不到想要的数据,正则有问题):import java.io.*;
import java.net.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class GetIp{ public static void main(String args[]) throws MalformedURLException,IOException {
getContent("http://finance.yahoo.com/q/cp?s=^GSPC&alpha=Z");
}
public static void getContent(String URLSTR) {
try {
URL url = new URL(URLSTR);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String line = null;
Pattern namePat = Pattern.compile("<small>([a-zA-Z ]+)</small>"); while ((line = br.readLine()) != null) {
// System.out.println(line);
Matcher m = namePat.matcher(line);
if (m.find()) {
System.out.println(m.group(1));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
我写的代码(总得不到想要的数据,正则有问题):import java.io.*;
import java.net.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class GetIp{ public static void main(String args[]) throws MalformedURLException,IOException {
getContent("http://finance.yahoo.com/q/cp?s=^GSPC&alpha=Z");
}
public static void getContent(String URLSTR) {
try {
URL url = new URL(URLSTR);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String line = null;
Pattern namePat = Pattern.compile("<small>([a-zA-Z ]+)</small>"); while ((line = br.readLine()) != null) {
// System.out.println(line);
Matcher m = namePat.matcher(line);
if (m.find()) {
System.out.println(m.group(1));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
两个匹配的 字符串 在同一行里面,所以只打印了第一个匹配的字符串。
稍微改一下,效率太低。
while ((line = br.readLine()) != null) {
//System.out.println(line);
Matcher m = namePat.matcher(line);
int iSt = 0;
while(iSt < line.length()){
if (m.find()) {
System.out.println(i+">>>>>>>>>>>>>"+m.group(1));
iSt = m.end();
}else{
break;
}
}
}
import java.io.*;
import java.net.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class GetIp{ public static void main(String args[]) throws MalformedURLException,IOException {
getContent("http://finance.yahoo.com/q/cp?s=^GSPC&alpha=Z");
}
public static void getContent(String URLSTR) {
try {
URL url = new URL(URLSTR);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String line = null;
Pattern namePat = Pattern.compile("<small>([a-zA-Z ]+)</small>"); while ((line = br.readLine()) != null) {
// System.out.println(line);
Matcher m = namePat.matcher(line);
//----Modified by bruce 09/16 20:11 start
int i=0;
while(m.find()) //loop to get data
{
System.out.println(m.group(1));
i++;
}
// if (m.find()) {
//
// System.out.println(m.group(1));
// }
//----Modified by bruce 09/16 20:11 end
}
} catch (IOException e) {
e.printStackTrace();
}
}
}自己看注释吧...我抽烟去了...