A. java如何获取带有css js 执行完后的网页源代码
只能抓取静态的页面源代码,因为很多事件和样式是动态绑定和执行的,所以不可能获取到执行完后的代码的。
public
String
getHtmlContent(String
htmlurl)
{
URL
url;
String
temp;
StringBuffer
sb
=
new
StringBuffer();
try
{
url
=
new
URL(htmlurl);
BufferedReader
in
=
new
BufferedReader(new
InputStreamReader(url.openStream(),
"gbk"));
while
((temp
=
in.readLine())
!=
null)
{
sb.append(temp);
}
in.close();
}
catch
(final
MalformedURLException
me)
{
me.getMessage();
}
catch
(final
IOException
e)
{
e.printStackTrace();
}
return
sb.toString();
}
B. java中如何根据一个网址获得该网页的源代码
package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpTest {
private String u;
private String encoding;
public static void main(String[] args) throws Exception {
HttpTest client = new HttpTest("http://www..com/", "UTF-8");
client.run();
}
public HttpTest(String u, String encoding) {
this.u = u;
this.encoding = encoding;
}
public void run() throws Exception {
URL url = new URL(u);// 根据链接(字符串格式),生成一个URL对象
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();// 打开URL
BufferedReader reader = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream(), encoding));// 得到输入流,即获得了网页的内容
String line; // 读取输入流的数据,并缺兄显示
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
C. Java访问指定URL并获取网页源代码
1.编写useSourceViewer 类的基本框架,该类仅包括无返回值的main ()方法,该方法从参数中获取URL,通过输入缓冲和输出缓冲将该URL 原码输出。
2.编写useSourceViewer 类,代码如下:
import java.net.*;
import java.io.*;
public class useSourceViewer
{
public static void main (String[] args)
{
if (args.length > 0)
{
try
{
//读入URL
URL u = new URL(args[0]);
InputStream in = u.openStream( );
// 为增加性能存储输入流
in = new BufferedInputStream(in);
// 将输入流连接到阅读器
Reader r = new InputStreamReader(in);
int c;
while ((c = r.read( )) != -1)
{
System.out.print((char) c);
}
Object o = u.getContent( );
System.out.println("I got a " + o.getClass().getName( ));
}
catch (MalformedURLException e)
{
System.err.println(args[0] + " is not a parseable URL");
}
catch (IOException e)
{
System.err.println(e);
}
} // end if
} // end main
} // end SourceViewer}
D. java 获取网页源代码---有效防止乱码
前段时间做过这类功能,如何有效防止乱码,我们必须先知道一个网页的编码方式,是utf-8,还是gbk。
1.HttpURLConnection.getContentType();直接读取,效率高,但有很多时候读不到。只是text/html就完事了,没有charset.
2.使用第三方的HttpClient,执行效率较高。但读取网页头header也只适用部分站,很多网站服务段不设置,结果就读成了null.
3.最没有效率的判断方法就是使用inputStreamReader先把正页的html源码读取出来,之后截取charset后面编码。得到编码之后重新再读取一遍。但是效率很低。
做个总结:
/**
* 取得页面编码
*
* @param url
* @return
*/
public String getCharset(String url) throws Exception {
// log.info("进入读页面的关键词:" + keyword);
String charset = "";
int c;
HttpURLConnection httpurlcon = null;
// log.info("url:"+url);
// log.info("charset:"+charset);
log.info("url:" + url);
URL httpurl = new URL(url);
// System.out.println(url+str);
httpurlcon = (HttpURLConnection) httpurl.openConnection();
// google需要身份
httpurlcon.setRequestProperty("User-agent", "Mozilla/4.0");
charset = httpurlcon.getContentType();
log.info("charset1:" + charset);
// 如果可以找到
if (charset.indexOf("charset=") != -1)
charset = charset.substring(charset.indexOf("charset=")
+ "charset=".length(), charset.length());
// 否则读取response.Header头
else {
charset = this.getContentCharset();
log.info("charset2:" + charset);
}
// 如果charset还是为空,那么直接读网页来截取
if (charset == null) {
charset = this.readPageCharset(url);
log.info("charset31:" + charset);
}
return charset;
}
E. java 得到网页源码为啥跟浏览器查看到的网页源码不一样呢
从浏览器查看到的源码是页面运行过之后的html静态文件(这个就是最终源码了),并不是开发时候的页面源码。