我想要做的是在我的浏览器中打开一个网页(chrome)并获取刚刚用我的java应用程序打开的页面的html源代码。
我不想获取网址的源代码,我想要一个连接到浏览器的程序,并获取打开的页面的html代码。
例如,如果我在浏览器中打开youtube,我希望我的应用程序获取当前页面的html代码(在这种情况下是youtube代码)。对不起,如果我的英语不是很好。
答案 0 :(得分:1)
你可以这样做:
import java.util.*;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
URL url;
InputStream is = null;
BufferedReader br;
String line;
try {
String urlInput = input.nextLine();
url = new URL(urlInput);
is = url.openStream(); // throws an IOException
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (is != null) is.close();
} catch (IOException ioe) {
// nothing to see here
}
}
}
我从这里得到了这个:How do you Programmatically Download a Webpage in Java
答案 1 :(得分:0)
试试这个:
您必须传递URL作为参数,并且您将拥有HTML代码
public static void main(String[] args) throws IOException {
URL u = null;
try {
u = new URL(args[0]);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader in = new BufferedReader(new InputStreamReader(u.openStream()));
String line = null;
while((line = in.readLine()) != null){
System.out.print(line);
}
}