【问题标题】:How to download the exact source code of the webpage如何下载网页的确切源代码
【发布时间】:2016-05-12 03:41:48
【问题描述】:

我想下载网页的源代码。我使用了 URL 方法,即 URL url=new URL("http://a.html");

和 Jsoup 方法,但没有得到实际源代码中提到的确切数据。例如-

<input type="image"
       name="ctl00$dtlAlbums$ctl00$imbAlbumImage"    
       id="ctl00_dtlAlbums_ctl00_imbAlbumImage"
       title="Independence Day Celebr..."
       border="0"         
       onmouseover="AlbumImageSlideShow('ctl00_dtlAlbums_ctl00_imbAlbumImage','ctl00_dtlAlbums_ctl00_hdThumbnails','0','Uploads/imagegallary/135/Thumbnails/IMG_3206.JPG','Uploads/imagegallary/135/Thumbnails/');"
       onmouseout="AlbumImageSlideShow('ctl00_dtlAlbums_ctl00_imbAlbumImage','ctl00_dtlAlbums_ctl00_hdThumbnails','1','Uploads/imagegallary/135/Thumbnails/IMG_3206.JPG','Uploads/imagegallary/135/Thumbnails/');" 
       src="Uploads/imagegallary/135/Thumbnails/IMG_3206.JPG"     
       alt="Independence Day Celebr..." 
       style="height:79px;width:148px;border-width:0px;"
/>

在此标记中,jsoup 的代码未检测到最后一个属性“样式”。如果我从 URL 方法下载它,它会将样式标签更改为 border=""/> 属性。

谁能告诉我如何下载网页的确切源代码? 我的代码是-

URL url=new URL("http://www.apcob.org/");
InputStream is = url.openStream();  // throws an IOException
BufferedReader  br = new BufferedReader(new InputStreamReader(is));
String line;
File fileDir = new File(contextpath+"\\extractedtxt.txt");
Writer fw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileDir), "UTF8"));
while ((line = br.readLine()) != null)
{
 // System.out.println("line\n "+line);
  fw.write("\n"+line);
}
 InputStream in = new FileInputStream(new File(contextpath+"extractedtxt.txt";));
String baseUrl="http://www.apcob.org/";
Document doc=Jsoup.parse(in,"UTF-8",baseUrl);
System.out.println(doc);

我遵循的第二种方法是-

Document doc = Jsoup.connect(url_of_currentpage).get();

我想在 java 中执行此操作,并且发生此问题的网站名称是“http://www.apcob.org/”。

【问题讨论】:

    标签: javascript java html css jsoup


    【解决方案1】:

    这可能是由于 user agent 字符串不同 - 当您从浏览器浏览页面时,它会发送带有浏览器类型的 user agent 字符串。有些网站会针对不同的浏览器(例如移动设备)使用不同的页面做出响应。
    尝试添加与浏览器相同的 user agent 字符串。

    【讨论】:

      【解决方案2】:

      您尝试下载的页面已被 JavaScript 代码以某种方式修改。 Jsoup 是一个 html 解析器。它不运行 javascript。

      如果您想像在 Chrome 中看到的那样获取源代码,请使用以下工具之一:

      这三个都可以解析和运行页面内的Javascript代码。

      【讨论】:

      • 不,我使用过 jsoup(在我的问题中提到)但没有得到相同的数据。我在问题中提到的示例
      • @anny 问题中缺少一个重要部分。我突出显示它并因此改变了我的答案。请查看我的更新。
      • ohk ,但是我的完整代码依赖于 jsoup,所以我现在无法更改我的解析器。根据我的研究,jsoup 比 htmlunit 解析器更好,因为我无法更新其中的数据
      【解决方案3】:

      我想这会很好,

      public static void main(String[] args) throws Exception {
          //Only If you're using a proxy
          //System.setProperty("java.net.useSystemProxies", "true");
      
          URL url = new URL("http://www.apcob.org/");
      
          HttpURLConnection yc = (HttpURLConnection) url.openConnection();
          yc.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36");
          BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
      
          String inputLine;
          while ((inputLine = in.readLine()) != null)
              System.out.println(inputLine);
          in.close();
      }
      

      【讨论】:

      • 我已经尝试过了,但它没有按我的意愿工作(有问题的解释)
      • @anny 我已经更新了我的答案。现在我正在使用 HttpUrlConnection(而不是 UrlConnection)并添加了用户代理。
      【解决方案4】:

      这是一个获取网页的便捷功能。使用它获取 HTML 字符串。然后使用JSOUPString 解析为Document

      public static String fetchPage(String urlFullAddress) throws IOException {
      //      String proxy = "10.3.100.207";
      //      int port = 8080;
              URL url = new URL(urlFullAddress);
              HttpURLConnection connection = null;
      //      Proxy proxyConnect = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy, port));
              connection = (HttpURLConnection) url.openConnection();//proxyConnect);
              connection.setDoOutput(true);
              connection.setDoInput(true);
      
              connection.addRequestProperty("User-Agent",
                      "Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10')");
              connection.setReadTimeout(5000); // set timeout
      
              connection.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
              connection.addRequestProperty("Accept-Language", "en-US,en;q=0.5");
              connection.addRequestProperty("Accept-Encoding", "gzip, deflate");
              connection.addRequestProperty("connection", "keep-alive");
              System.setProperty("http.keepAlive", "true");
      
              BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
      
              String urlString = "";
              String current;
              while ((current = in.readLine()) != null) {
                  urlString += current;
              }
      
              return urlString;   
      }
      

      如果问题出在 JSOUP 解析器上,请尝试使用 http://jericho.htmlparser.net/docs/index.html。它按原样解析 HTML,而不纠正错误。

      我注意到的其他几件事: 你没有关闭fw。将 UTF8 替换为 UTF-8`。 如果您需要解析大量 CSS,请尝试使用 CSS-Parser

      【讨论】:

      • 如果我包含用户代理,那么对于这个网站apcob.org,内容没有完全下载。但如果我不包括用户代理,那么cumhuriyet.com.tr 这个网站没有下载。我正在使用 chrome 浏览器。我添加了 connection.addRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11");
      • 我的 chrome 版本是 49.0.2623.28 beta-m。
      • 哦对不起..我弄错了..我没有写 fw.close();这就是为什么它没有完全阅读。非常感谢你的好回答
      【解决方案5】:

      当通过http 获取网页时,Web 服务器通常会以某种方式格式化源文件;您无法使用http 获得php 文件的确切来源。 据我所知,完成您所要求的唯一方法是使用ftp

      【讨论】:

      • 你为什么在你的答案中谈论一个 php 文件..?我不确定 OP 是否要下载网站的未编译源代码
      • @ibiza OP 说他们正在寻找“下载网页确切源代码的方法”,我很确定“确切源代码”与“未编译源代码”相同"。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-23
      相关资源
      最近更新 更多