我编写了一个程序,它两次抓取源代码,并从检索到的数据中创建了一个包含特定信息的 CSV。我的问题是,当我去保存第二位数据时,它没有添加到创建的 CSV,而是用新信息覆盖它。我已经提到了这个链接,但它使用的是不同的类。我的代码目前是:
public static void scrapeWebsite() throws IOException {
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage(s);
originalHtml = page.getWebResponse().getContentAsString();
obtainInformation();
originalHtml = "";
final HtmlForm form = page.getForms().get(0);
final HtmlSubmitInput button = form.getInputByValue(">");
final HtmlPage page2 = button.click();
try {
synchronized (page2) {
page2.wait(1000);
}
}
catch(InterruptedException e)
{
System.out.println("error");
}
originalHtml = originalHtml + page2.refresh().getWebResponse().getContentAsString();
obtainInformation();
}
public static void obtainInformation() throws IOException {
PrintWriter docketFile = new PrintWriter(new FileWriter("tester3.csv", true));
// 创建 csv 文件。(名称必须更改,覆盖删除文件) originalHtml = originalHtml.replace('"','*'); int i = 0;
//While loop runs through all the data in the source code. There is (14) entries per page.
while(i<14) {
String plaintiffAtty = "PlaintiffAtty_"+i+"*>"; //creates the search string for the plaintiffatty
Pattern plaintiffPattern = Pattern.compile("(?<="+Pattern.quote(plaintiffAtty)+").*?(?=</span>)");//creates the pattern for the atty
Matcher plaintiffMatcher = plaintiffPattern.matcher(originalHtml); // looks for a match for the atty
while (plaintiffMatcher.find()) {
docketFile.write(plaintiffMatcher.group().toString()+", "); //writes the found atty to the file
}
i++;
}
docketFile.close(); //closes the file
}
}
我相信必须在第二种方法中进行更改。