我是Java的新手,遇到了一些问题。
主要想法是连接到一个网站,收集信息并将其存储在数组中。
我想让程序搜索网站找到关键词,并存储关键词后面的内容。
daniweb网站首页底部有一个名为“标签云”的部分,其中包含标签/简短单词
标签云:“我想要存储这里写的内容”
我的想法是首先读取网站的HTML,然后使用Scanner和StringTokenizer搜索该文件以查找关键词后跟文本,然后存储为数组。
是否有更好/更容易的方法?
您建议我在哪里寻找一些示例?
以下是我目前的进展。
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL dweb = new URL("http://www.daniweb.com/");
URLConnection dw = dweb.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(hc.getInputStream()));
System.out.println("connected to daniweb");
String inputLine;
PrintStream out = new PrintStream(new FileOutputStream("OutFile.txt"));
try {
while ((inputLine = in.readLine()) != null)
out.println(inputLine);
//System.out.println(inputLine);
//in.close();
out.close();
System.out.println("printed text to outfile");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
Scanner scan = new Scanner(OutFile.txt);
String search = txtSearch.getText();
while (scan.hasNextLine()) {
line = scan.nextLine();
//still working
while (st.hasMoreTokens()) {
word = st.nextToken();
if (word == search) {
} else {
}
}
}
scan.close();
SearchWin.dispose();
} catch (IOException iox) {
}
}
任何帮助都将不胜感激!