-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(通过 Ctrl+U)看到 HTML 代码时,我看到的代码完全不同

<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 代码仍然存在,但它太大了,无法将其粘贴到此处。任何人都知道我怎样才能获得网络的真实内容?为什么会这样?我很迷茫

4

4 回答 4

3

网站上有内容保护机制。您应该完全复制浏览器行为(包括 cookie、引用者等)来获取页面。

于 2014-01-25T13:11:48.653 回答
0

我们可以从 url 的输入流中获取内容(就像您在程序中所做的一样)。我试图通过使用您的代码来获取内容内容然后你必须分析没有给出内容的url。你的调试场景应该是这样的

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

于 2013-10-24T11:06:53.233 回答
0

输入流光标位于正文的URLConnection开头,我不知道这是否可以与这个人一起获得头部,但你可能需要手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。

google ip没问题,74.125.228.41
现在试试下面的代码。

import java.io.BufferedReader;

导入 java.io.InputStreamReader;导入 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以获取标头数据,或者使用第三种方法。一个我都不认识的派对图书馆,你可以搜索一下。

于 2013-10-24T10:54:10.857 回答
0

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

于 2013-10-24T10:56:10.433 回答