浏览器不支持帧
我正在尝试创建一个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框架?
这种刮擦有点复杂,有几个因素需要考虑。
- Achieve app是否设置了Cookie?如果是这样,您需要接受他们并发送下一个请求。我认为
- 通过外观的东西,你需要解析HTML页面并提取你想要加载的框架。我怀疑你会收到会话过期的消息,因为你没有发送cookie或类似的东西。您需要确保使用FRAMESET中提供的确切URL。
我建议使用Apache HttpClient module,它比标准的Java URL提供程序功能更全面一些,并且可以为你管理cookies等东西。
您必须提取主框架的网址(dispatch.php?atknodetype=pim.pim&atkaction=pim&atklevel=-1&atkprevlevel=0&achievo=37b552462afdfd248a21fedbf0eebe43
)并向该网址发出第二个请求。如果使用cookie来跟踪会话,您还必须将响应中包含的cookie发送到您的登录请求。
我会使用更高级别的API来执行此操作(例如Apache HttpClient),或者使用编程式浏览器(例如HtmlUnit)。
这将是很好的一些代码。我如何向此网址发出第二次请求?我已经下载了Apache HttpClient和HtmlUnit。我试过HtmlUnit,但我得到了同样的行为,尽管我基本上做了同样的事情。 –
我不会为你做。向我们展示您的HtmlUnit代码,我们将尝试告诉您为什么它不起作用以及应如何更改。 –
看看更新。我添加了我使用的htmlunit代码 –
当您尝试访问displatch.php时,您需要传递会话中的cookie以及[引荐标题](http://en.wikipedia.org/wiki/HTTP_referrer)。我强烈建议您使用Apache HTTPClient,因为它会自动执行cookie管理,并且使访问任何http资源变得更简单。 – Augusto