首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从html页面提取源代码

从html页面提取源代码
EN

Stack Overflow用户
提问于 2013-02-14 23:17:18
回答 1查看 633关注 0票数 1

我正在尝试使用以下代码从网站读取源代码

代码语言:javascript
复制
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class GrabHTML {

public static void Connect() throws Exception{

 //Set URL
 URL url = new URL("http://www.google.ca/");
 URLConnection spoof = url.openConnection();

 //Spoof the connection so we look like a web browser
 spoof.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)" );
 BufferedReader in = new BufferedReader(new InputStreamReader(spoof.getInputStream()));
 String strLine = "";

 //Loop through every line in the source
   while ((strLine = in.readLine()) != null){

 //Prints each line to the console
   System.out.println(strLine);
  }

 System.out.println("End of page.");
}

public static void main(String[] args){

 try{
  //Calling the Connect method
  Connect();
 }catch(Exception e){

 }
 }
 }

但它只读取源代码的一部分。当我在浏览器中看到“View Source”时,Google.com有更多的数据。

EN

回答 1

Stack Overflow用户

发布于 2013-02-14 23:57:20

Remove below语句

spoof.setRequestProperty(“用户代理”,"Mozilla/4.0 (兼容;MSIE5.5;Windows NT 5.0;H010818)“);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14878021

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档