stringtoken

rgh914 2008-06-04 04:28:38
我最近在做一个抓取网页数据的程序,抓取数据时我用的是dom来实现抓取的,数据抓取下来后在一个字符串了,我写个方法把他们写入文本文件里了,但是里面有很多空格之类的东西,数据显得很乱,我要实现分析写库那样的数据是没有办法实现的,于是我用stringtoken来对这个字符串进行分割后存入一个数组内,以方便我提取我想要的数据写入数据库,可是我那样做了之后发现数据还没有达到我想要的那种要求,不知道该怎么做才更好了,那位大侠知道给我指点一下。我的代码如下:


public class Ahold {
private static final ResourceBundle config = ResourceBundle.getBundle("com.finet.hkfuture.configure");
public static void parse() throws Exception {
DOMParser parser = new DOMParser();
// parser.parse(config.getString("URL"));
String urls = config.getString("URL");
StringTokenizer tokenzer = new StringTokenizer(urls, ";");
while (tokenzer.hasMoreTokens()) {
String url = tokenzer.nextToken();
parser.parse(url);
print(parser.getDocument(), "");
}
}

public static void main(String[] args) throws Exception {
Ahold.parse();
}

public static void Filewrite (String s) throws IOException {
FileWriter fw = null;
try {
fw = new FileWriter("f:/jackie.txt",true);//创建FileWriter对象,用来写入字符流
String newline = "\r\n";
fw.write(s);
fw.write(newline);
fw.flush();
}
catch (IOException e) {
e.printStackTrace();
}
finally{
fw.close();
}
}


public static String[] StrSplit(String Record,String delimiter){
StringTokenizer st = new StringTokenizer(Record,delimiter);
String[] RecordArray = new String[st.countTokens()];
//System.out.println(st.countTokens());
int i = 0;
//将分割的元素放到数组 RecordArray 元素中。
while (st.hasMoreTokens()) {
try {
RecordArray[i] = st.nextToken();
i++;
}
catch(Exception e) {
e.printStackTrace();
}
}
return RecordArray;
}


public static void print(Node node, String indent) {
if (node.getNodeName().equals("HEAD"))
return;

if (node.getNodeName().equals("#comment"))
return;
if (node.getNodeName().equals("SCRIPT"))
return;

if (node.getNodeName().equals("STYLE"))
return;

//if (node.getNodeName().equals("HEAD"))
//return;
if (node.getNodeName().equals("#text")) {
// Here only print the text content for testing, and you can write
// the text into database
// System.out.println(node.getTextContent());
EncodingConvertorUtils encodingUtils = EncodingConvertorUtils.getInstance();
try {
String text = encodingUtils.big52gb(node.getTextContent());
// System.out.println(text);
// Filewrite(text);
String[] str2 = StrSplit(text," ");
for(int j=0;j<str2.length;j++)
{
System.out.println(str2[j]); //这里在我分割后在数组里打出数据
}
// xmlElements(text);
/****
String[] ss=text.split("");
for(int i=0;i<ss.length;i++)
System.out.println(ss[i]);
*********/
} catch (DOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Conndb db = new Conndb();
}
Node child = node.getFirstChild();
while (child != null) {
print(child, indent + " ");
child = child.getNextSibling();
}
}
}
...全文
279 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
rgh914 2008-06-05
  • 打赏
  • 举报
回复

?交易资料


?













衍生产品市场






合约概要






更新日期: 2008年5月26日










新华富时中国25指数期货












项目




合约细则






相关指数


?




新华富时中国25指数
(由新华富时指数有限公司编纂、计算 及发放)






HKATS代码


?




FXC






合约乘数


?




每指数点港币$50






最低价格波幅


?




一个指数点






合约月份


?




现月,下月,及之後的两个季月






开市前时段


?




上午9:15 -上午9:45 & 下午2:00 -下午2:30






交易时间


上面这些是我取出来的部分数据,其他的也都是这个样子的,我通过什么方式才能取得这些数据里对应字段的值来把他们写入数据库里去?

rgh914 2008-06-05
  • 打赏
  • 举报
回复
我就是想把里面纯文本的字符取出来,以便于我来把相应数据写入数据库,
这么说吧,就是我已经把数据取下来了,但是不知道该怎么来进一步来分析这些数据来把他写入数据库里去,因为我取下来的数据已经经过过滤都是一些文本类型的数据。
楼上的,你说可以用正则表达式,如果那样的的话该怎么用,能给说点具体点么?
tavor 2008-06-05
  • 打赏
  • 举报
回复
你想要啥样的结果,你的数据又是啥样子的呀,说不清楚没办法帮忙,不过一般字符串处理,正则表达式应该可以做
jahcy 2008-06-04
  • 打赏
  • 举报
回复
为什么不用正则来匹配删除

67,538

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧