import java.io.*;
import java.net.*;
import java.util.regex.*;
public class HexunData {
private static String getHexunIn(String urlString) {
StringBuffer hexun = new StringBuffer();
try {
// 获取所要访问的URL
URL url = new URL(urlString);
URLConnection uc = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(uc
.getInputStream()));// 创建输入流
String line = null;
while ((line = reader.readLine()) != null) {
hexun.append(line + " ");
}
reader.close();
} catch (MalformedURLException e) {
System.out.print("invalid url:" + urlString);
} catch (IOException e) {
e.printStackTrace();
}
return hexun.toString();
}
public void extractor(String urlString) throws IOException {
FileOutputStream fos = new FileOutputStream("E:\\HexunData_股东人数.txt");
OutputStreamWriter ows = new OutputStreamWriter(fos);
try {
// 获得网页文本内容
String str = HexunData.getHexunIn(urlString);
// 创建提取股票数据来源的正则表达式
/*
* Pattern gp_source = Pattern .compile("(?<=<th colspan=\"7\">|\"blue\">|</FONT>).*?(?=<|FONT|</th>)");
* Matcher mc = gp_source.matcher(str); String s1; while (mc.find()) { //
* 提取股票数据来源 s1 = String.valueOf(mc.group()); ows.write(s1);
* System.out.printf("%s", mc.group()); } System.out.println();
* ows.write("\r\n");
*/
String s2;
Pattern gp_item = Pattern
.compile("(?<=<strong>).*?(?=(</strong>))");
Matcher n = gp_item.matcher(str);
while (n.find()) {
s2 = String.valueOf(n.group());
ows.write(s2 + " ");
System.out.printf("%-40s", n.group());
}
Pattern gp_data = Pattern
.compile("(?<=<span class=\"font10\">).*?(?=(</span>))");
Matcher m = gp_data.matcher(str);
String s3;
int i = 0;
while (m.find()) {
if (i == 0)
System.out.println();
i++;
s3 = String.valueOf(m.group());
ows.write(s3 + " ");
System.out.printf("%-20s", m.group());
if (i % 6 == 0) {
System.out.println();
ows.write("\r\n");
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (PatternSyntaxException e) {
System.out.println("Regular expression syntax error");
} catch (IllegalStateException e) {
System.out.println("Do not find the pattern");
} finally {
if (ows != null) {
ows.close();
fos.close();
}
}
}
public static void main(String[] args) {
HexunData test = new HexunData();
try {
test
.extractor("http://stockdata.stock.hexun.com/2009_cgjzd_601398.shtml");
} catch (IOException e) {
e.printStackTrace();
}
}
}
分享到:
相关推荐
网络分析仪数据读取.rar网络分析仪数据读取.rar网络分析仪数据读取.rar网络分析仪数据读取.rar网络分析仪数据读取.rar网络分析仪数据读取.rar
C++读取Excel数据 ole 模式 VS2003 Excel 2003
C#通过串口读取和发送数据,简单的串口读取发送数据例子,简单易懂,适合初学者,欢迎下载
利用matlab读取ALSO SAR数据并进行成像。
“读取数据记录文件”位于函数选板的“编程→文件I/O→文件函数→数据文件→读取数据记录文件”,图标和接线端如图8-31所示。 读取数据记录文件从指定的文件中读取记录数据,从当前的日志位置开始读取。输入和...
读取环境为Win10+VS2015,个人参考资料封装的excel读取模块用来读取Excel2007中的数据,文件为压缩文件,亲测可用。
曲线拟合csv数据读取曲线拟合csv数据读取曲线拟合csv数据读取曲线拟合csv数据读取曲线拟合csv数据读取曲线拟合csv数据读取曲线拟合csv数据读取曲线拟合csv数据读取曲线拟合csv数据读取曲线拟合csv数据读取曲线拟合...
excle大量数据读取,10万条数据没有问题,一般的方法很可能出现内存溢出
地震数据读取程序,matlab编写,可以实现segd格式的读取
C#访问OPC读取数据 一般用于自动化编程
读取通达信日线数据
GPIB数据读取工具 通过GPIB卡读取数据
以前用VC读取txt里数据的时候,非常方便,例如: 2 34 56 3 23 67 之类的, 但是在java里不是这么简单,不注意的话读出来的是乱码,我在网上查了资料,然后自己结合写了一个小程序,实现了方便读取数据
通过串口读取IMU输出的原始数据,并解析
一个提取Excel数据的小工具,并且实时读取串口数据
用于读取大智慧等软件的行情数据,便于分析用或进行二次开发
matlab读取rf数据,rf数据是超声信号的标准数据。用此程序可以读取该类数据
C# 使用 StreamWriter 读取数据 C# 使用 StreamWriter 读取数据 C# 使用 StreamWriter 读取数据 C# 使用 StreamWriter 读取数据
DELPHI7下用superobject读取中国天气网的JSON数据
读取并输出各型雷达基数据,包括SA、SC、CC、CD等。