2013-10-24 258 views
-3

我想从Web获取HTML源代码。我做这个如何从网上获取源代码?

u = new URL(url); 
URLConnection con = u.openConnection(); 
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"); 
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); 
StringBuilder a = new StringBuilder(); 
while ((line=in.readLine())!=null){ 
    a.append(line); 
} 
in.close(); 
contWeb = a.toString(); 

试图但是,当我执行此代码是HTML代码,我得到

<head> 
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"> 
<meta http-equiv="cache-control" content="max-age=0" /> 
<meta http-equiv="cache-control" content="no-cache" /> 
<meta http-equiv="expires" content="0" /> 
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" /> 
<meta http-equiv="pragma" content="no-cache" /> 
<meta http-equiv="refresh" content="10; url=/distil_r_blocked.html?Ref=/windfarms/durrazzo-albania-al01.html" /> 
<script type="text/javascript" src="/ga.233033467223.js?PID=14CDB9B4-DE01-3FAA-AFF5-65BC2F771745" defer></script> 
<style type="text/css">#d__fFH{position:absolute;top:-5000px;left:-5000px}#d__fF{font-family:serif;font-size:200px;visibility:hidden}#collective57bfda9e,#friendshipeadab1a4,#degrees85b85925,#friendshipeadab1a4{display:none!important}</style></head> 
<body> 
<div id="distil_ident_block">&nbsp;</div> 
<div style="display: none;"> 
<a href="BangJensen32676optimal.html" id="friendshipeadab1a4" rel="file">reserved</a> 
</div> 
<div id="d__fFH"><OBJECT id="d_dlg" CLASSID="clsid:3050f819-98b5-11cf-bb82-00aa00bdce0b" width="0px" height="0px"></OBJECT> 
<span id="d__fF"></span> 
</div> 
</body> 
</html> 

但是,当我看到Mozilla Firefox浏览器的HTML代码(通过按Ctrl + U ),我看到它的代码是相当不同的

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head><link id="ctl00_Link1" href="js/jquery/skin.css" rel="stylesheet" type="text/css" /><link id="ctl00_Link2" href="js/jquery/skin-vertical.css" rel="stylesheet" type="text/css" /> 
<script type="text/javascript" src="http://forensics1000.com/js/15075.js" async="async"></script> 
<script type="text/javascript" src="js/jquery/jquery.js" ></script> 
<script type="text/javascript" src="js/jquery/jquery.jcarousel.min.js" ></script> 
<div id="blq-local-nav"> 
<ul id="nav2"> 
<li id="ctl00_liWindfarms" class="first-child selected"><a href="./">Offshore Wind Farms</a></li> 
<li id="ctl00_liVessels"><a href="vessels.aspx" id="ctl00_A3">Vessels</a></li> 
<li id="ctl00_liTurbines"><a href="turbines.aspx" id="ctl00_A4">Turbines</a></li> 
<li id="ctl00_liFoundations"><a href="support-structures-for-offshore-wind-turbines-aid268.html" id="ctl00_Afoundations">Foundations</a></li> 
<li id="ctl00_liNews"><a href="windfarmsNews.aspx" id="ctl00_A5">News</a></li> 
<li id="ctl00_liMarketAnalysis"><a href="marketReports.aspx" id="ctl00_A6">Reports <span class="new">(new)</span></a></li> 
     <li id="ctl00_liDownloads"><a href="subscribers/downloads.aspx" id="ctl00_A7"><span class='subs'>Downloads</span></a></li> 

     <li id="ctl00_liEquipment"><a href="equipmentFinder.aspx">Equipment</a></li> 
     <li id="ctl00_liPorts"><a href="ports.aspx">Ports</a></li> 
     <li id="ctl00_liContactUs"><a href="contact.aspx">Contact</a></li> 
     <li id="ctl00_liAdvertise"><a href="request.aspx?id=advertise">Advertise</a></li> 

     <li style="float:right;" > 

      <a id="ctl00_LoginStatus1" href="javascript:__doPostBack('ctl00$LoginStatus1$ctl02','')">Login</a> 
     </li> 

     <li id="ctl00_liSubscribe" onclick="pageTracker._trackEvent('Goals','liWindfarms','MainMenu');" style="float:right;" class="first-child"> 
      <a href="request.aspx?id=owfdb" id="ctl00_A2">Subscribe</a> 
     </li> 
    </ul> 
    <ul id="ctl00_subnav"> 

    <li class=" first-child"><a href="windfarms.aspx">Project Database</a></li><li><a href="subscribers/owfdb/pipeline.aspx"><span class='subs'>Timeline Chart</span></a></li><li><a href="converters.aspx">Converters</a></li><li><a href="substations.aspx">Substations</a></li><li><a href="../offshorewind">Global Map</a></li><li><a href="widget.aspx">Maps For Your Website</a></li><li><a href="windspeeds.aspx">Wind Speeds</a></li><li><a href="powerdata.aspx">Power Data</a></li></ul> 
</div>           

的HTML代码还行,但它太大了给它贴在这里。 任何人都知道如何才能获得网络的真实内容?为什么发生这种情况?我很迷茫

+1

你期望看到什么? –

+0

错误是对象还是预期? – Dave

+0

正在返回的源是HTML源。如果您正在查找实际的源代码,则无法访问。 – ediblecode

回答

0

URLConnection输入流光标是在身体的开始,我不知道这是可能得到这个家伙heade或不,但你可能需要给予Socket,一些东西,东西这样

Socket s=new Socket("176.12.59.8",80); 
s.getOutputStream().write("GET /index.html?param=value HTTP/1.1\r\n".getBytes()); 
s.getOutputStream().write("User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2\r\n".getBytes()); 
//send other header requests, cookie, etc... 
s.getOutputStream().write("\r\n".getBytes()); 
s.getOutputStream().flush(); 
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); 
StringBuilder a = new StringBuilder(); 
while ((line=in.readLine())!=null){ 
    a.append(line); 
} 

问题(硬功)与插座与插座你只需要连接到一个地址和特定端口,以及剩下的工作是属于你(约发送和接收数据)。

这是一个低级别的方法。

编辑:考虑开放http://google.com/,第一件事就是与Socket你只需要指定你试图打开目标路径和端口,然后使用输入流发送数据,以使客户端和服务器相互理解,这里的协议是HTTP/1.1。

谷歌IP是74.125.228.41
好吧,现在只需尝试下面的代码。

import java.io.BufferedReader; 

import java.io.InputStreamReader; import java.net.Socket;

公共类袜子{

public static void main(String[] args)throws Exception { 
    String line=null; 
    Socket s=new Socket("74.125.228.41",80); 
    s.getOutputStream().write("GET/HTTP/1.1\r\n".getBytes());//requesting the root 
    s.getOutputStream().write("User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2\r\n".getBytes()); 
    //send other header requests, cookie, etc... 
    s.getOutputStream().write("\r\n".getBytes()); 
    s.getOutputStream().flush(); 
    BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); 
    StringBuilder a = new StringBuilder(); 
    while ((line=in.readLine())!=null){ 
     System.out.println(line); 
    } 

    } 

} 

您发送

GET/HTTP/1.1 
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2 

和出把

HTTP/1.0 200 OK 
Date: Fri, 25 Oct 2013 08:14:44 GMT 
Expires: -1 
Cache-Control: private, max-age=0 
Content-Type: text/html; charset=UTF-8 
Set-Cookie: NID=67=cnvAQD2mzWXzNmbkv40u0Fjqh-hfhbbBsqbgHmNbzvdxkUWEcNGbzeva56UYuuNfSzVgKeM0AwH8_yfesWA4mpdOLKTVYyPzJrlhrn7be1HWVMMxU-QSUQGfbR6N_OKQ; expires=Sat, 26-Apr-2014 08:14:44 GMT; path=/; domain=.; HttpOnly 
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info." 
Server: gws 
X-XSS-Protection: 1; mode=block 
X-Frame-Options: SAMEORIGIN 
Alternate-Protocol: 80:quic 
X-Cache: MISS from pouyanazm_appliance 
X-Loop-Control: 37.191.91.249 57B724E7913CDA261C464198106FF67D 
Connection: close 

<html>.....html content is here....</html> 

URLConnection做上述所有事情(发送数据作为HTTP)的你,但它不会返回标题数据,只是正文。 您有3个选择,使用Socket方法(您需要实现http)或覆盖URLCOnnection以获取标题数据或使用第3个选项。我甚至不知道一个党的图书馆,你可以搜索它。

+0

谢谢你的建议不同,但我十分赞同所需要呈请人的所有标题丢失。你能否解释一下如何用套接字获取网站的HTML代码? –

+0

你需要研究HTTP/1.1协议伙计。然后通过'Socket'实现它,在HTTP/1.1中有许多头文件,每个头文件都用于特定的需求,所以我必须知道你想做什么!首先开始使用HTTP/1.1 – 2013-10-24 18:15:19

+0

好的,还有一件事要写在我的待办事项列表中。我试图获取网站的HTML代码,以从该网站获取我想要的信息。 –

0

除非你正在谈论入侵系统,否则你不能看看源代码。 ;-)你看到的代码是HTML代码 - 它可能与源代码是1:1,但你看不到任何PHP脚本,J2EE类等。恐怕。

+0

对不起,我误会,但我说的是HTML代码,而不是PHP或Javascript角或任何 –

+0

所以,你得到了源代码。你期望看到什么?你的代码可以满足你的需求。 – FazoM

+0

我,结果我的代码一些HTML,这是不是网站 –

0

我们可以从URL的输入流中获取内容(就像你在程序中做的那样)。我试图通过使用你的代码来获取内容。你可以尝试从其他网址读取内容,如果你能得到的内容,那么你要分析不给content.Your调试方案应该是这样的

1.检查内容是否使用AJAX加载的URL。 2.他们阻止匿名用户。 3.内容可能被压缩。

+1

我已经尝试过使用另一个网站,正如你所说的那样,它完美地工作。我的猜测是,该网站不允许自动获取源代码... –

3

内容保护机制是在网站上进行。您应该完全复制浏览器行为(包括cookies,refferer等)以获取页面。