/*
* import java.io.BufferedReader;
* import java.io.File;
* import java.io.FileReader;
* import java.io.IOException;
* import java.util.regex.Matcher;
* import java.util.regex.Pattern;
*/
import java.net.*;
import java.io.*;
import java.util.regex.*;
public class DataSpider {
public static void main(String[] args) throws IOException {
String s;
int i = 0;
//网页地址
URL url = new URL(
"http://money.finance.sina.com.cn/corp/go.php/vMS_MarketHistory/stockid/600006.phtml");
//创建输入流
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
//创建输出文档
FileOutputStream fos = new FileOutputStream("D:\\XiAoOMAn\\GuPiao.txt");
//创建输出流
OutputStreamWriter ows = new OutputStreamWriter(fos);
//匹配需要获取的数据的列名
Pattern q = Pattern.compile("((?<=g>)(\\w*?)).*?(?=(</strong>))");
//匹配数据
Pattern p = Pattern.compile("((?<=date=)(\\w*?)).*?(?=('>))|((?<=center\">)(\\d{1,7}?)).*?(?=(</div>))");
String str = null;
//获取源文件内容 匹配 输出
while ((str = br.readLine()) != null) {
// Matcher b = q.matcher(str);
//列名
Matcher n = q.matcher(str);
//数据
Matcher m = p.matcher(str);
//提取 列名
while (n.find()) {
//形式转换 成 String
s = String.valueOf(n.group());
//输出到文本文档
ows.write(s + " ");
System.out.printf("%-44s", n.group());
}
//提取 数据
while (m.find()) {
if (i == 0){
System.out.println();
ows.write("\r\n");
}
i++;
s = String.valueOf(m.group());
ows.write(s + " ");
System.out.printf("%-22s", m.group());
//每输出完一行 回车
if (i % 7 == 0)
{
System.out.println();
ows.write("\r\n");
}
}
}
ows.close();
fos.close();
br.close();
br.close();
}
}
网页数据抓取 获取股票数据
最新推荐文章于 2023-07-22 09:44:39 发布