我正在尝试使用以下代码获取 HTML 页面的内容:
String malSearch = "http://myanimelist.net/anime.php?letter=" + firstLetter;
URL url = new URL(malSearch);
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[8192];
int len = 0;
while ((len = in.read(buf)) != -1) {
baos.write(buf, 0, len);
}
String body = new String(baos.toByteArray(), encoding);
它工作正常,但它没有给我真正想要的东西。它给了我这个:
<html>
<head>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<meta name="format-detection" content="telephone=no">
<meta name="viewport" content="initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
</head>
<body style="margin:0px">
<iframe src="/_Incapsula_Resource?CWUDNSAI=9&xinfo=6-122029399-0 0NNN RT(1404149034204 2) q(0 -1 -1 -1) r(0 -1) B12(4,315,0) U1&incident_id=124001330081285077-564449081699338326&edet=12&cinfo=4ee46646c753833e04000000" frameborder=0 width="100%" height="100%" marginheight="0px" marginwidth="0px">Request unsuccessful. Incapsula incident ID: 124001330081285077-564449081699338326</iframe>
</body>
</html>
什么时候应该给我整个页面(大约 800 行)。
我认为这是因为这是一个使用 PHP 的网站,但我不太确定。谁能告诉我如何获取整个 HTML 内容?
这是我试图从中获取内容的页面:http://myanimelist.net/anime.php?letter=A
最佳答案
本网站使用名为 Incapsula 的服务。 网站管理员配置了 Incapsula 以防止机器人访问其内容。
我建议你联系管理员并要求加入白名单, 试图绕过系统可能会让您被禁止并列入黑名单。
关于java - 如何从 PHP 生成的 HTML 页面中获取正文内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24496114/