无论使用Python还是Java,阅读网页都会丢失一些信息

时间:2013-06-17 17:23:51

标签: python urllib2 webpage

当我尝试使用阅读某些网页时,页面源如下所示:

<p/><table border="1" align="center" cellpadding="10"><tbody><tr><td><a href="/cgi-bin/query/C?c101:./temp/~c1011jI5AQ" title="Displays without navigation or highlighting">Printer Friendly</a>[<a href="/home/billdwnloadhelp.html">Help</a>]</td>

但是当我使用Python的urllib2,urllib或请求阅读此网页时,结果与以下内容相同:

<p/><a href="/[<a href="%s">Help</a>]</td>`/C?query:c101" Printer Friendly</a><p/>

那么,为什么我无法阅读所有信息并丢失了非常重要的部分C?c101:./temp/~c1011jI5AQ ???

我尝试用Java来阅读,情况是一样的。我尝试使用不同的操作系统,如Mac,Linux或Windows,它也是相同的结果。那我怎么解决这个问题呢?

1 个答案:

答案 0 :(得分:0)

我不确定是否正确理解:第二个示例是您使用Python或Java获得的示例。而首先?是通过浏览器查看“源代码”获得的吗?在这种情况下,有三种可能的情况:

  • 首先(不太可能),您的浏览器的“查看源代码”显示源由JavaScript修改/更改/生成
  • 其次,服务器根据“客户端签名”生成不同的内容(正式地,user-agent identification
  • 第三,服务器根据浏览器中存储的cookie提供不同的内容

作为测试,您可以使用curl下载页面并进行一些比较 - 它将非常适合该任务,因为在许多选项中,您有机会更改提供的用户代理标识到服务器 - 所以,假装是IE或Firefox或任何你喜欢的:

curl(1)                           Curl Manual                          curl(1)

NAME
       curl - transfer a URL

SYNOPSIS
       curl [options] [URL...]

DESCRIPTION
       curl  is  a tool to transfer data from or to a server, using one of the
       supported protocols (HTTP, HTTPS, FTP, FTPS,  SCP,  SFTP,  TFTP,  DICT,
       TELNET,  LDAP  or  FILE).  The command is designed to work without user
       interaction.

       curl offers a busload of useful tricks like proxy support, user authen‐
       tication,  FTP upload, HTTP post, SSL connections, cookies, file trans‐
       fer resume and more. As you will see below, the number of features will
       make your head spin!

[...]

      -A/--user-agent 
              (HTTP) Specify the User-Agent string to send to the HTTP server.
              Some   badly   done  CGIs  fail  if  this  field  isn't  set  to
              "Mozilla/4.0". To encode blanks  in  the  string,  surround  the
              string  with  single  quote marks. This can also be set with the
              -H/--header option of course.

              If this option is set more than once, the last one will  be  the
              one that's used.