2017-08-06 36 views
0

我目前正在嘗試項目發送一個URL到多個網站掃描他們的分類和任何安全風險使用Java和HtmlUnit. www.virustotal.com是我必須的最後一個網站配置和我正在通過該網站進行的問題,因爲href是空的。Java HtmlUnit - 當刮網站時收到空href

該網站的工作原理是在第一頁輸入網址,然後點擊提交。從這裏顯示一個彈出窗口,用戶必須選擇是重新分析還是使用最後的掃描結果(在這種情況下,我們希望始終重新分析)。它是提供空href的重新分析錨。我的想法是,這是一個JavaScript問題,它不會生成結果頁面的URL。不幸的是,我不確定哪裏去旁邊:/

項目代碼(道歉是多麼邋遢!): -

//turn off htmlunit logging// 
    java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF); 
    java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF); 
    java.util.logging.Logger.getLogger("org.apache.http.client.protocol.ResponseProcessCookies").setLevel(java.util.logging.Level.OFF); 

    //initialise url and obtain users selection// 
    System.out.println("Please select the url you would like to review:"); 
    Scanner sc = new Scanner(System.in); 
    String startPath = sc.nextLine(); 

    //enable javascript and use engine to initialise and parse websites code// 
    String url = "https://www.virustotal.com/#url"; 
    System.out.println("Connecting to Virus Total..."); 
    WebClient webClient = new WebClient(BrowserVersion.CHROME); 
    webClient.getOptions().setThrowExceptionOnScriptError(false); 
    webClient.getOptions().setJavaScriptEnabled(true); 
    webClient.waitForBackgroundJavaScript(8000); 
    page = webClient.getPage(url); 

    //fill in form 
    HtmlForm form = page.getFirstByXPath("//form[@action='/en/url/submission/']"); 
    HtmlTextInput textField = form.getInputByName("url"); 
    textField.setValueAttribute(startPath); 
    HtmlButton button1 = page.getFirstByXPath("//button[@id='btn-scan-url']"); 
    HtmlPage page1 = button1.click(); 

    //waiting and dealing with popup 
    webClient.waitForBackgroundJavaScript(8000); 
    String page1String = page1.getWebResponse().getContentAsString(); 
    System.out.println(page1String); 
    HtmlAnchor htmlAnchor = page1.getFirstByXPath("//button[@id='btn-url-reanalyse']"); 
    System.out.println(htmlAnchor); //testing what I can see in the anchor 
    HtmlPage page2 = htmlAnchor.click(); 

    //progressing to next screen 
    String output = page2.asText(); 
    System.out.println(output); 

HTML後我會收到打印出字符串page1String:

<div class="modal-footer"> 
 
    <a id="btn-url-reanalyse" class="btn" href=""> 
 
    Reanalyse 
 
    </a>

01:

HTML當通過手動現場進展

<a id="btn-url-reanalyse" class="btn" href="/en/url/submission/?force=1&amp;url=http%3A//www.facebook.com/&amp;token=415eda59daae48938b1dcc64f3152ed5ee9ac27d485348d55c87e9da7e714605"> 
 
    Reanalyse 
 
    </a>

任何幫助或建議將不勝感激!我也很樂意與任何提供的模塊建議一起工作,只需使用HtmlUnit,因爲它是我發現與其他站點實際合作的第一個。

在此先感謝。

+0

我希望Facebook令牌不是一個重要的。 – Oleg

+0

附加在網址中。它可能是一個公開的@Oleg –

+0

@SagarV也許,只是指出了OP的情況下,它不是... – Oleg

回答

0
java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF); 

我認爲在尋找問題時禁用日誌記錄是一個壞主意。 如果您啓用日誌記錄,您將看到存在js錯誤。

webClient.getOptions().setThrowExceptionOnScriptError(false); 

由於這個程序繼續,但部分JavaScript不執行。 我想這就是爲什麼你的鏈接沒有得到更新的原因。

Javascript錯誤看起來像一個HtmlUnit錯誤。請打開問題並隔離最小測試用例as described here