Python urllib库使用

urlopen()方法

In [2]: import urllib

In [3]: instance_s = urllib.urlopen("http://www.imekaku.com")

In [4]: type(instance_s)
Out[4]: instance

In [5]: instance_s.getcode()
Out[5]: 200

In [6]: instance_s.read(10)
Out[6]: '<!DOCTYPE '

# 另外还有

instance_s.readline()# 全部读取
instance_s.readlines()# 读取成列表

HTTPMessage

In [2]: import urllib

In [3]: s = urllib.urlopen("http://www.imekaku.com")

In [4]: help(s)
Help on instance of addinfourl in module urllib:

class addinfourl(addbase)
 |  class to add info() and geturl() methods to an open file.
 |  
 |  Methods defined here:
 |  
 |  __init__(self, fp, headers, url, code=None)
 |  
 |  getcode(self)
 |  
 |  geturl(self)
 |  
 |  info(self)
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from addbase:
 |  
 |  __repr__(self)
 |  
 |  close(self)

In [5]: msg = s.info()
# msg是一个HTTPMessage对象
In [6]: help(msg)
Help on instance of HTTPMessage in module httplib:

class HTTPMessage(mimetools.Message)
 |  Method resolution order:
 |      HTTPMessage
 |      mimetools.Message
 |      rfc822.Message
 |  
 |  Methods defined here:
 |  
 |  addcontinue(self, key, more)
 |      Add more field data from a continuation line.
 |  
 |  addheader(self, key, value)
 |      Add header for field key handling repeats.
 |  
 |  readheaders(self)
 |      Read header lines.
 |      
 |      Read header lines up to the entirely blank line that terminates them.
 |      The (normally blank) line that ends the headers is skipped, but not
 |      included in the returned list.  If a non-header line ends the headers,
 |      (which is an error), an attempt is made to backspace over it; it is
 |      never included in the returned list.
 |      
 |      The variable self.status is set to the empty string if all went well,
 |      otherwise it is an error message.  The variable self.headers is a
 |      completely uninterpreted list of lines contained in the header (so
 |      printing them will reproduce the header exactly as it appears in the
 |      file).
:

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部