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

2024-04-18 00:18:41 发布

您现在位置:Python中文网/ 问答频道 /正文

当我尝试使用read some web page时,页面源代码如下所示:

<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/>

那么,为什么我不能阅读所有的信息而丢失了非常重要的部分呢???你知道吗

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


Tags: webread源代码pagetablehelpsome页面
1条回答
网友
1楼 · 发布于 2024-04-18 00:18:41

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

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

作为一个测试,您可以使用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.

相关问题 更多 >

    热门问题