我想解析一个网站,并从中获取一些信息。问题是,当我在java中加载页面并将其保存到文件中时,它不包含我需要的信息。当我点击页面上的查看源时,也没有任何信息。但是,当我下载页面(另存为),并用记事本打开时,我可以找到我需要的内容。解析网页
简而言之,该网页,该Java加载不同于我下载和打开记事本。
如何将页面加载到字符串中,使其看起来与我在计算机上下载的页面相同?
public static void main(String[] args) {
try {
String webPage = "http://www.integral-calculator.com/#";
URL url = new URL(webPage);
URLConnection urlConnection = url.openConnection();
InputStream is = urlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int numCharsRead;
char[] charArray = new char[1024];
StringBuffer sb = new StringBuffer();
while ((numCharsRead = isr.read(charArray)) > 0) {
sb.append(charArray, 0, numCharsRead);
}
String result = sb.toString();
PrintWriter out = new PrintWriter("C:\\Users\\Patryk\\Desktop\\filename.txt");
out.println(result);
out.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}