2011-07-12 58 views
0

我正在嘗試創建一個java程序,該程序對achievo實例執行登錄。我正在嘗試使用Screen Scraping瀏覽器不支持幀

我管理使用下面的代碼登錄:

@Test 
public void testLogin() throws Exception { 
    HashMap<String, String> data = new HashMap<String, String>(); 
    data.put("auth_user", "user"); 
    data.put("auth_pw", "password"); 
    doSubmit("https://someurl.com/achievo/index.php", data); 
} 

private void doSubmit(String url, HashMap<String, String> data) throws Exception { 
    URL siteUrl = new URL(url); 
    HttpsURLConnection conn = (HttpsURLConnection) siteUrl.openConnection(); 
    conn.setRequestMethod("POST"); 
    conn.setDoOutput(true); 
    conn.setDoInput(true); 
    //conn.setRequestProperty("User-agent", "spider"); 
    //conn.setRequestProperty("User-agent", "Opera/9.80 (X11; Linux i686; U; en) Presto/2.7.62 Version/11.01"); 

    conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; .NET CLR 1.2.30703)"); 

    DataOutputStream out = new DataOutputStream(conn.getOutputStream()); 

    Set<String> keys = data.keySet(); 
    Iterator<String> keyIter = keys.iterator(); 
    StringBuilder content = new StringBuilder(""); 
    for(int i=0; keyIter.hasNext(); i++) { 
     Object key = keyIter.next(); 
     if(i!=0) { 
      content.append("&"); 
     } 
     content.append(key + "=" + URLEncoder.encode(data.get(key), "UTF-8")); 
    } 
    System.out.println(content.toString()); 

    out.writeBytes(content.toString()); 
    out.flush(); 
    out.close(); 
    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
    String line = ""; 
    while((line=in.readLine())!=null) { 
     System.out.println(line); 
    } 
    in.close(); 
} 

然而,當大展成功登錄項,我重定向到主頁,它說:

<head> 
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> 
    <title>Achievo</title> 
    </head> 
    <frameset rows="113,*" frameborder="0" border="0"> 
    <frame name="top" scrolling="no" noresize src="top.php?atklevel=-1&atkprevlevel=0&achievo=37b552462afdfd248a21fedbf0eebe43" marginwidth="0" marginheight="0"> 
    <frameset cols="210,*" frameborder="0" border="0"> 
     <frame name="menu" scrolling="no" noresize src="menu.php?atklevel=-1&atkprevlevel=0&achievo=37b552462afdfd248a21fedbf0eebe43" marginwidth="0" marginheight="0"> 
     <frame name="main" scrolling="auto" noresize src="dispatch.php?atknodetype=pim.pim&atkaction=pim&atklevel=-1&atkprevlevel=0&achievo=37b552462afdfd248a21fedbf0eebe43" marginwidth="0" marginheight="0"> 
    </frameset> 
    <noframes> 
     <body bgcolor="#CCCCCC" text="#000000"> 
     <p>Your browser doesnt support frames, but this is required to run Achievo</p> 
     </body> 
    </noframes> 
    </frameset> 

很顯然,我得到您的瀏覽器不支持框架,但這是運行Achievo所必需的。

我試圖直接訪問dispatch.php框架,因爲這是我可能想要的,但是,它報告我的會話已過期,而且我需要重新登錄。

有沒有辦法僞造一個框架?或者以某種方式保持連接,更改網址,並嘗試獲取dispatch.php框架?


使用的HtmlUnit,我也做了以下內容:

WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3); 
HtmlPage page = webClient.getPage("https://someurl.com/index.php"); 
System.out.println(page.asXml()); 

List<HtmlForm> forms = page.getForms(); 
assertTrue(forms != null && !forms.isEmpty()); 

HtmlForm form = forms.get(0); 
HtmlSubmitInput submit = form.getInputByName("login"); 
HtmlInput inputUsername = form.getInputByName("auth_user"); 
HtmlInput inputPw = form.getInputByName("auth_pw"); 

inputUsername.setValueAttribute("foo"); 
inputPw.setValueAttribute("bar"); 

HtmlPage page2 = submit.click(); 

CookieManager cookieManager = webClient.getCookieManager(); 
Set<Cookie> cookies = cookieManager.getCookies(); 
System.out.println("Is cookie " + cookieManager.isCookiesEnabled()); 

for(Cookie cookie : cookies) { 
    System.out.println(cookie.toString()); 
} 

System.out.println(page2.asXml()); 
webClient.closeAllWindows(); 

這裏我得到的形式,我提交它,我找回了同樣的信息。當我也打印出來時,我可以看到我有一個cookie。現在的問題是,我如何繼續使用登錄的Cookie獲取dispatch.php框架?

+1

當您嘗試訪問displatch.php時,您需要傳遞會話中的cookie以及[引薦標題](http://en.wikipedia.org/wiki/HTTP_referrer)。我強烈建議您使用Apache HTTPClient,因爲它會自動執行cookie管理,並且使訪問任何http資源變得更簡單。 – Augusto

回答

1

這種刮擦有點複雜,有幾個因素需要考慮。

  1. Achieve app是否設置了Cookie?如果是這樣,您需要接受他們併發送下一個請求。我認爲
  2. 通過外觀的東西,你需要解析HTML頁面並提取你想要加載的框架。我懷疑你會收到會話過期的消息,因爲你沒有發送cookie或類似的東西。您需要確保使用FRAMESET中提供的確切URL。

我建議使用Apache HttpClient module,它比標準的Java URL提供程序功能更全面一些,並且可以爲你管理cookies等東西。

1

您必須提取主框架的網址(dispatch.php?atknodetype=pim.pim&atkaction=pim&atklevel=-1&atkprevlevel=0&achievo=37b552462afdfd248a21fedbf0eebe43)並向該網址發出第二個請求。如果使用cookie來跟蹤會話,您還必須將響應中包含的cookie發送到您的登錄請求。

我會使用更高級別的API來執行此操作(例如Apache HttpClient),或者使用編程式瀏覽器(例如HtmlUnit)。

+0

這將是很好的一些代碼。我如何向此網址發出第二次請求?我已經下載了Apache HttpClient和HtmlUnit。我試過HtmlUnit,但我得到了同樣的行爲,儘管我基本上做了同樣的事情。 –

+0

我不會爲你做。向我們展示您的HtmlUnit代碼,我們將嘗試告訴您爲什麼它不起作用以及應如何更改。 –

+0

看看更新。我添加了我使用的htmlunit代碼 –