【发布时间】:2011-04-23 19:41:05
【问题描述】:
我正在尝试编写一个程序来读取网站http://judgephilosophies.wikispaces.com 的html 源代码。我编写了一些简单的 java 代码来读取和输出源代码,但它只是打印出“null”。不过,这很奇怪——如果我将代码中的“http://judgephilosophies.wikispaces.com”替换为任何其他网站,它就可以正常工作。该程序似乎仅适用于 wikispaces.com 域中的网站,而我完全不知道为什么。代码如下。非常感谢您的帮助。
import java.io.*;
import java.net.*;
public class AccessWebExample
{
public static void main (String[] args) throws Exception
{
//Create reader to access html source code
URL url = new URL ("http://judgephilosophies.wikispaces.com/");
InputStreamReader isr = new InputStreamReader (url.openStream());
BufferedReader reader = new BufferedReader (isr);
//Read and print the text
do
{
System.out.println(reader.readLine());
}
while(reader.readLine() != null);
}
}
【问题讨论】:
-
怎么不行? - 如果该站点是 Ajax 站点,那么它将无法工作。您拥有的程序只会从站点获取 HTML。
-
@Romain - 不,服务器重定向。请参阅下面的答案。
标签: java html-parsing