[英]How to setup HtmlUnit in an Eclipse project?
我的項目包括htmlunit jar並下載一些頁面內容。 然而,可執行jar(包括libs,eclipse導出功能)只能在我創建它的機器上運行(在不同的情況下它不會執行)。
編輯:它沒有執行,因為它啟動時不顯示“啟動無頭瀏覽器”MessageBox。 我使用了Eclipse Indigo:File> Export> Runnable jar> package所需的libratries到生成的jar中
幫助,眾神:
import java.io.*;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.RefreshHandler;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
編輯:根據要求提供進一步的代碼
public class MyTest
{
public static void main(String[] arguments) {
try{
JOptionPane.showMessageDialog(null, "Starting Headless Browser");
JFileChooser fr = new JFileChooser();
FileSystemView fw = fr.getFileSystemView();
String MyDocuments = fw.getDefaultDirectory().toString();
FileInputStream fstream = new FileInputStream(MyDocuments+"\\Links.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
String strLineID;
FileWriter xfstream = new FileWriter(MyDocuments+"\\NewPageContentList.txt");
BufferedWriter out = new BufferedWriter(xfstream);
while ((strLineID = br.readLine()) != null) {
strLine = br.readLine();
out.write(strLineID);
out.write("\r\n");
out.write(DownloadPage(strLine));
out.write("\r\n");
}
out.close();
in.close();
JOptionPane.showMessageDialog(null, "HeadLess Browser Process Has Finished");
}
catch (Exception e){
JOptionPane.showMessageDialog(null, "error");
}
}
public static String DownloadPage(String str){
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.setThrowExceptionOnScriptError(false);
try{
final HtmlPage page = webClient.getPage(str);
final String pageAsText = str_replace("\n","",str_replace("\r","",page.asText()));
return pageAsText;
}
catch(IOException e){
JOptionPane.showMessageDialog(null, "error");
}
webClient.closeAllWindows();
return "";
}
public static String str_replace (String search, String replace, String subject)
{
StringBuffer result = new StringBuffer (subject);
int pos = 0;
while (true)
{
pos = result.indexOf (search, pos);
if (pos != -1)
result.replace (pos, pos + search.length (), replace);
else
break;
}
return result.toString ();
}
}
這是如何設置HtmlUnit以及如何將其導出到eclipse中的可運行jar文件:
如果這適用於新項目,則更新您自己的項目以反映列表中采取的步驟。 希望這可以幫助
帶有默認設置的新java項目下載庫HTMUnit的最新版本來自下載最新HTMLUnit jar選擇新項目屬性 - > Java Build Path - >轉到庫選項卡並添加提取的所有jar文件。 在新項目中使用main方法創建一個新類,並運行一個簡單的appliation並在類中添加此方法並在main方法中調用它。
`@Test
public void getElements() throws Exception {
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://some_url");
final HtmlDivision div = page.getHtmlElementById("some_div_id");
final HtmlAnchor anchor = page.getAnchorByName("anchor_name");
webClient.closeAllWindows();
}`
我嘗試了上面的答案,但它們對我不起作用。
他們是需要的,但我有一個動態的Web項目,所以我還需要將所有.jar文件添加到WEB-INF目錄中的lib目錄。
例如ProjectName \\ WebContent \\ WEB-INF \\ lib(您正在使用的所有.jar文件)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.