我正在尝试获取网页源代码,但我发现了一些问题。我想获取源代码上的Url,但是当我向下获取时,Url变成了一个Javascript方法。
在浏览器源码查看器中:
<a class="title" href="/hkstp_web/en/Directory/Acquest%20Stem%20Cell%20Research%20Company%20Limited/">aaa Company Limited</a>但是当我把它弄下来的时候,它变成了这个样子:
<a href="javascript:void(0)"><span>...</span></a>下面是我的代码:
public class DownloadPage {
public static void main(String[] args) {
URL url;
try {
// get URL content
url = new URL("https://www.hkstp.org/hkstp_web/en/directory/");
URLConnection conn = url.openConnection();
// open the stream and put it into BufferedReader
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
//save to this filename
String fileName = "C:\\Users\\USER\\Documents\\server\\test.txt";
File file = new File(fileName);
if (!file.exists()) {
file.createNewFile();
}
//use FileWriter to write file
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
while ((inputLine = br.readLine()) != null) {
bw.write(inputLine + "\n");
}
bw.close();
br.close();
System.out.println("Done");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}如何获得正确链接?谢谢
发布于 2016-08-31 17:31:49
嗯,正如你(希望)所知道的,自从Sir Timothy发明了网页以来,它们已经发生了很大的变化。这意味着你所看到的(一个可以与之交互的)不仅仅是从服务器传递的超文本标记语言(和CSS)代码的结果,而且通常是由浏览器使用JavaScript进行大量的"post“处理。
因此,如果你想得到这个链接,你必须做同样的后处理,也就是使用HtmlUnit框架,或者-如果你不坚持使用Java - PhantomJS。
https://stackoverflow.com/questions/39244991
复制相似问题