urllib 模块是一个高级的 web 交流库,其核心功能就是模仿web浏览器等客户端,去请求相应的资源,并返回一个类文件对象。urllib 支持各种 web 协议,例如:HTTP、FTP、Gopher;同时也支持对本地文件进行访问。但一般而言多用来进行爬虫的编写,而下面的内容也是围绕着如何使用 urllib 库去编写简单的爬虫。另外,如果要爬取 js 动态生成的东西, 如 js 动态加载的图片,还需要一些高级的技巧,这里的例子都是针对于静态的 html 网页的。
下面的说明都是针对于 python2.7 版本而言的,版本间存在差距,具体参考官方手册。
首先,如果我需要写一个爬虫,去爬取一个网站的图片的话,可以分为以下几步:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAArkAAACUCAIAAAAYkaYiAAAX10lEQVR4nO2dPa7jNheGsyZ/TYoECFIkC/BKJsIgwAXSBIMsQBuJkSZNmjRppvEmZgO3SBXAX6E//hxKpC2/NO3nrWZ0afIxDyW+OqKpry6R3t/f44MpffnyJb9wUc33KwyzpjDMmsIwawrDrCkMs6ZwKfNX4vYeoTDMmsIwawrDrCkMs6YwzJrCeIVtwawpDLOmMMyawjBrCsOsKYxX2BbMmsIwawrDrCkMs6YwzJrCeIVtwawpDLOmMMyawjBrCsOsKYxX2BbMmsIwawrDrCkMs6YwzJrCeIVtwawpDLOmMMyawjBrCsOsKVzsFd5v05cvX26sQS+YNYJZI5g1glkjmDUqZSavUA0DZg0GzBoMmDUYMGswYA5qxitUw4BZgwGzBgNmDQbMGgyYg5rxCtUwYNZgwKzBgFmDAbMGA+agZrxCNQyYNRgwazBg1mDArMGAOagZr1ANA2YNBswaDJg1GDBrMGAOasYrVMOAWYMBswYDZg0GzBoMmIOa8QrVMGDWYMCswYBZgwGzBgPmoGa8QjUMmDUYMGswYNZgwKzBgDmoGa9QDQNmDQbMGgyYNRgwazBgDmrGK1TDgFmDAbMGA2YNBswaDJiDmvEK1TBg1mDArMGAWYMBswYD5qBmvEI1DJg1GDBrMGDWYMCswYA5qBmvUA0DZg0GzBoMmDUYMGswYA5qxitUw4BZgwGzB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sIwawrjFbYFs6YwzJrCMGsKw6wpDLOmcLFXeL9Nw9sq2xLMGsGsEcwawawRzBqVMpNXqIYBswYDZg0GzBoMmDUYMAc14xWqYcCswYBZgwGzBgNmDQbMQc14hWoYMGswYNZgwKzBgFmDAXNQM16hGgbMGgyYNRgwazBg1mDAHNSMV6iGAbMGA2YNBswaDJg1GDAHNeMVqmHArMGAWYMBswYDZg0GzEHNeIVqGDBrMGDWYMCswYBZgwFzUDNeoRoGzBoMmDUYMGswYNZgwBzUjFeohgGzBgNmDQbMGgyYNRgwBzXjFaphwKzBgFmDAbMGA2YNBsxBzXiFahgwazBg1mDArMGAWYMBc1AzXqEaBswaDJg1GDBrMGDWYMAc1IxXqIYBswYDZg0GzBoMmDUYMAc14xWqYcCswYBZgwGzBgNmDQbMQc28k7oNwawRzBrBrBHMGr0CM3mFahgwazBg1mDArMGAWYMBc1AzXqEaBswaDJg1GDBrMGDWYMAc1IxXqIYBswYDZg0GzBoMmDUYMAc14xWqYcCswYBZgwGzBgNmDQbMQc14hWoYMGswYNZgwKzBgFmDAXNQM16hGgbMGgyYNRgwazBg1mDAHNSMV6iGAbMGA2YNBswaDJg1GDAHNeMVqmHArMGAWYMBswYDZg0GzEHNeIVqGDBrMGDWYMCswYBZgwFzUDNeoRoGzBoMmDUYMGswYNZgwBzUjFeohgGzBgNmDQbMGgyYNRgwBzXjFaphwKzBgFmDAbMGA2YNBsxBzXiFahgwazBg1mDArMGAWYMBc1AzXqEaBswaDJg1GDBrMGDWYMAc1Mw7qdsQzBrBrBHMGsGs0Sswk1eohgGzBgNmDQbMGgyYNRgwBzXjFaphwKzBgFmDAbMGA2YNBsxBzXiFahgwazBg1mDArMGAWYMBc1AzXqEaBswaDJg1GDBrMGDWYMAc1IxXqIYBswYDZg0GzBoMmDUYMAc1/x8pQ8TDcjNMAAAAAABJRU5ErkJggg==" alt="" />
当然,你也可以将爬到的数据进行各种处理分析,例如你可以写一个比价网站,去各种网站获取报价,然后整合到一起。所以,将上面的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hSC3BEEOdSBIAcTgtwpBLkjCHKoA0EOJgS5UwhyR+wT5G8D5/P5rZhF4hAGhIC+yBU7uJ2Y8aCkZGBs2u5QTOvLKWzcuqaUQLPKWdosk6WHFSvyJ4QVOZiwIncKK3JHsLUOdSDIwYQgdwpB7giCHOpAkIMJQe4UgtwRBDnUgSAHE4LcKQS5I95RkIOAih0kyJ+DwmoT5I/G0loR5DsyV6unCvLX11ddlL1jXl9fK3aQIH8OCqtNkD8aS2tFkO/IXK2eKsjrvjKeQzx65gShhCB3CkHuCIJ8Wxt41tggyB8WgtwpBLkjCPJtbeBZY4Mgf1gIcqcQ5I4gyLe1gWeNDYL8YSHInUKQO4Ig39YGnjU2VgQ5KNmx3XmQQzkbNaU8yKGcdXVe10GCfBY8a8QfP37c+4h7X9z8OMOm7Q7FfIBlEZ8+fdqoKSXnDZq1CPMoI8h3EONZI8azRoxnjRjPGjGeEzFBboNnjRjPGjGeNWI8a8R4TsQfztvTf8+aL/CsAc8a8KwBzxrwnMCK3AbPGjGeNWI8a8R41ojxnIgJchs8a8R41ojxrBHjWSPGcyImyG3wrBHjWSPGs0aMZ40Yz4mYILfBs0aMZ40YzxoxnjViPCdigtwGzxoxnjViPGvEeNaI8ZyICXIbPGvEeNaI8awR41kjxnMiJsht8KwR41kjxrNGjGeNGM+JmCC3wbNGjGeNGM8aMZ41YjwnYoLcBs8aMZ41YjxrxHjWiPGciAlyGzxrxHjWiPGsEeNZI8ZzIibIbfCsEeNZI8azRoxnjRjPiZggt8GzRoxnjRjPGjGeNWI8J2KC3AbPGjGeNWI8a8R41ojxnIgJchs8a8R41ojxrBHjWSPGcyLma0xt8KwBzxrwrAHPGvCcwIrcBs8aMZ41YjxrxHjWiPGciAlyGzxrxHjWiPGsEeNZI8ZzIv4fOHlX2k32iZUAAAAASUVORK5CYII=" alt="" />
下面我们按照这个通用的布置,学习每一步都应该怎么做。
1.打开目标网站
urllib.urlopen(url[, data[, proxies[, context]]])
去远程请求响应的 url,并返回一个类文件对象。(注意,此处已经发起了远程请求,也就是进行了联网操作,有数量流量)
url : 一个完整的远程资源路径,一般是一个网站。(注意,要包含协议头,例如:http://www.baidu.com/,此处的 http:// 不能省略)
如果该URL没有指明协议类型,或者其协议标识符为file:,则该函数会打开本地文件。如果无法打开远程地址,将触发 IOError 异常。
data : 如果使用的是 http:// 协议,这是一个可选的参数,用于指定一个 POST 请求(默认使用的是 GET 方法)。这个参数必须使用标准的 application/x-www-form-urlencoded 格式。我们可以使用 urlencode() 方法来快速生成。
proxies : 设置代理,有需要的参照官方文档。下面给出官网的例子:
# Use http://www.someproxy.com:3128 for HTTP proxying
proxies = {'http': 'http://www.someproxy.com:3128'}
filehandle = urllib.urlopen(some_url, proxies=proxies)
# Don't use any proxies
filehandle = urllib.urlopen(some_url, proxies={})
# Use proxies from environment - both versions are equivalent
filehandle = urllib.urlopen(some_url, proxies=None)
filehandle = urllib.urlopen(some_url)
context : 在用 HTTPS 连接时,这个参数要设置为 ssl.SSLContext
的实例,用于配置 SSL 。
一般而言,我们只需要设置 url 参数就可以了。
例如:
f = urllib.urlopen('http://www.baidu.com/')
这样我能就能够得到一个类文件对象了,然后就可以对这个类文件对象进行各种读取操作了。
2.操作类文件对象
下面的方法和文件操作中的一致,下面列举出来,详情请参考我在python文件操作中的解释:
1. read([size]) -> read at most size bytes, returned as a string.
读取整个文件,将读取结果返回一个字符串对象
2. readline([size]) -> next line from the file, as a string.
读取一行,将读取结果返回一个字符串对象
3. readlines([size]) -> list of strings, each a line from the file.
读取整个文件,将每一行封装成列表中的元素,返回一个列表。
4. readinto() -> Undocumented. Don't use this; it may go away.
一个可以无视的将要废弃的方法
5. close() -> None or (perhaps) an integer. Close the file.
关闭文件,返回None或者一个表示关闭状态的整数。
另外:也可以和文件对象一样直接进行迭代操作。
除了以上和文件操作中用法一样的方法之外,还有以下特殊的方法:
1. info()
返回文件信息,对于 http 协议来说,返回的是响应报文中的报文头。
例子:
f = urllib.urlopen('http://www.so.com/')
print f.info()
aaar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Z3MePfFopAqzAxgXsYQ75QFAAAAZ2jOc2RHijE3nWP/uD+in1yY07+1k0eIfi08pdoCXrcCX6dZmkbp5jmVsh7RozoAAADOMq45lETIZ0MeYspAjNnyXzvVAB19U8lnxOaGp5kK+b17agEAAJ7Eac2h93wU55ia49xOEXN+Ynye4zGHUCm9fo4xz/F4zDPzHAAAAOcwNMc6W5HGcvGcX66uTOt2Db2t4t83N02T9z79u6bVqTrrLFumi3Qp92o0Vzu2pPtJdZu3uZlGWUxzAAAAnOeJ3wST0oG3PwAAACDjmd8hTdMBvOMBAAAABXz7HAAAAK4AzQEAAABXgOYAAACAK0BzAAAAwBWgOQAAAOAK0BwAAABwBWgOAAAAuAI0BwAAAFwBmgMAAACuAM0BAAAAV4DmAAAAgCtAcwAAAMAVoDkAAADgCtAcAAAAcAVoDgAAALiC/wOHJ63l3CvDdQAAAABJRU5ErkJggg==" alt="" />
2. geturl()
返回当前页面的真正的 url ,针对于网站服务器在进行重定向以后,我们可以用它来获取重定向后的页面。
3. getcode()
返回当前请求的状态码,如成功请求的状态码就是 200 。如果不是使用 http 协议打开的,就返回 None。
3.操作读取后的网页源码
这个部分就和文件操作一样了,但多数情况下会和 re 模块配合起来进行数据筛选,例如:
f = urllib.urlopen('http://www.baidu.com/')
b = f.read()
p = re.compile(r'<img.*?src="//(.*?\.(?:jpg|gif|png))".*?>', re.I)
result = p.findall(b)
print result
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAe0AAAAdCAIAAAAIM6MBAAAGwUlEQVR4nO2cPa7bOBDH5xR7jV0gBMJbpNsUT611jATQAdRtlTa+warfKrrBFtEBwgcXLlaAgLiYLUhR/BhJtEX7Pb3MD4YhM8PhDB/9NzWmA/gI/i3hE8DnsnnIaAk05SeAzyD/6UzDq4uQ+TWJVmYKZvUC/FVf0+3X4c2/3+GlA2AYhmE2wTrOMAyzb1jHGYZh9g3rOMMwzL7JoOPPDDPD9tXFMMwqrOPMHdm+uhiGWYV1nLkj21cXwz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bej4qRJD5Rw1SWkhsQIxKYU6F+Pnoe0YHA6Je8UXC2PN+Ymfx0TCIrLraqaXzcLsf2M/t2WRMhvx6KTn1SM3KREGY+VapqhrlP5Rk5SWGCth7rEE+52YbQkOLcS9SD9zY835iZ+nRKKv2oLY4l42C6sogZ9H5k66iiPUH731YfqwWR099rMQz3KEcTzXepis9q7jz/pWuugr4d/Ir7XE6HMXrsaZlrYXTvUjOJ5hts+ODdkrHmvcVPZ6LC1GSp0LuFSt0k7c5zmxc+Mhe8XnSWI/ZO5BXguTFni2WSyM7tqQZuRApOe5TMnVdNvZ8KZEKL3vr1JaSD96eKtxpqVD6VQ/gn2c2eg5NmSveCy7hdRjWVHQX75pJ+4zzgiHGw/ZK954xn4emftc2LEB+AWlwCyYseVM7awah11YrZqLbaEx7fDMm9DxtuqP6lRV0135aku8IVXqVFVnXV31bvYBQfRVgQDD168DRNvz0WZ4En7xd2z59oPYRNuT116hYyyXaw9F1Yvx2UgqXYk28ZC9CLPYz1LuU16BsLpuySzIIGObaSq8+6foryMuQozzo8vlgKaIZIvm/ljkarrtbHhXY4NY19e1xBvbujbaMWlcY6qfdYkA+PdYaSVtDtIvyI4t38lTDeNpaK/QYcvlDQJgWaMcn41czlSHTTxkr9gs9nO33Bf+mt5ZkTnnJWFAz9icn8DGMbCjE38damJJt148NLvQce/3nCH05u3NEexer+w4llPa/t274QP8fMzhyHj0hW8y1/2s9Y3HitbL3c+G752bD8lNZyo6fP8eP24+bJeBxlN5N0L7GbN880RC+rmBPH529HvOFRfAD37MPrLw4lnwgx/68Trh/yeLuSPbVxfDMKuwjjN3ZPvqYhhmFdZx5o5sX10Mw6zCOs7cke2ri2GYVV5r3Z5hGIZJg3WcYRhm37COMwzD7BvWcYZhmH3DOs4wDLNv/gfcRGQ6c2RK4gAAAABJRU5ErkJggg==" alt="" />
尝试在百度的首页爬取图片的地址,当然还有很多改进的地方,这里中做演示用。
关于python中的正则 re 模块的使用这里就不再重复了。
4.下载相应资源
在我们通过正则筛选出我们相要的图片的 url 之后,就可以开始下载了,在 urllib 中提供了相应的下载函数。
1. urllib.urlretrieve(url[, filename[, reporthook[, data]]])
将给定的 url 下载为本地文件,如果 url 指向的是本地的文件,或者是一个有效的缓存对象,那么将不会下载(注意,这里的存在是指下载的目录里有相同的文件了)。返回一个元祖(filename, headers),其中filename值的是本地保存的文件名,header指的是上面 info() 方法返回的对象。
url : 目标 url 。
filename : 下载到本地后保存的文件名, 可以是决对路径或相对路径形式。如果没有给,将缓存到一个临时文件夹中。
reporthook:一个回调函数,方法会在连接建立时和下载完成时调用这个函数。同时会向函数传递三个参数:1.目前为止下载了多少数据块;2.数据块的大小(单位是字节);3.文件的总大小;
data:如果使用的是 http:// 协议,这是一个可选的参数,用于指定一个 POST 请求(默认使用的是 GET 方法)。这个参数必须使用标准的 application/x-www-form-urlencoded 格式。我们可以使用 urlencode()来快速生成。
2. urllib.urlcleanup()
Clear the cache that may have been built up by previous calls to urlretrieve()
.
清除先前由 urlretrieve()
建立的缓存
。
其他常用模块内置方法:
1. urllib.quote(string[, safe])
将 string 编码成 url 格式,safe 指定不受影响的字符(默认 safe='/')。
因为 url 的编码和我们常用的 ASCII 并不同,例如我们常用的空格在 ASCII 中: 'scolia good' 直接现实一个空白字符,而在 url 中: 'scolia%20good' ,原本的空白字符变成了 %20 。
其转换规则为:
逗号,下划线,句号,斜线和字母数字这类符号是不需要转化。其他的则均需要转换。另外, 那些不被允许的字符前边会被加上百分号(%)同时转换成 16 进制,例如:“%xx”,“xx”代表这个字母的 ASCII 码的十六进制值。
例子:
f = urllib.quote('scolia good')
print f
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAV4AAAAgCAIAAADVFodIAAAFxElEQVR4nO2dzZXbOAzH0dWyhG3EKEOuILz5lO1A18EtB1ehw6oDN8E98BsAJdljeyZZ/F6en60hQfDrD5DjJBA2Wf3F+VsIIYQF3XXdLxZSYZgphLBeXXwzrHhtfppr3cHNn7JXNEOqvvjsM2VzwsOjf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ho71r6O1o40w4U7gRNW3xOV28X3y3DvuVOfJZv4fqp16un2xNmUQxg7vScBcsJeZoSf6bKR6O0oov9/C53Hth/ru09d14Yt83ks099r4rsEfXi+dKw4il3v9/D9JVdpdlfC8Pjf8nYmUeoVwrbAXme3mPNBiG8Zth0mAYhoJJg2EYCiYNhmEomDQYhqFg0mAYhoJJg2EYCiYNhmEovEQaVu8AAIB9gYiQP/lyoqffzSvD+HpelzVIIVj9aevLWnGbOr+GEMj7NVSNabZvfuSe9b2vbyhYhvH1vFMatlm9QwqEzq+BiEInJYRRHGKh+ORJ4mDSYBgK30caYnlCQO89cQuEgB9h9c5VtXiONpg0GIbCvjQQ5py+bMWS5zf/UoLjWX6/5ZKV/u/yd2bitid0Tp4YsiRwsSDuYmOM2ZBPYi3nnEmDYQh2pUG7Mkibqfwo79w2qCsV2ZPyMb0ptwjOOU/VVGNVlQb5Rvqz+cSkwTAERw4UKbw25/7Rnh9E9dGTGu1rLe99OVlQCIQs9CsHipJ9CAW5641hGJX9rMHz/Vbj7JouBT6XNbRxm4ia+8hf5dIxt6VdQ6p5zeGsIV9wGobRsisNyu8PS7SvOtCf5Js6+VFzHdD8DwUxHTmdHIDz/xapIQSAv378bK8QuG12rcGMH79rQET7ZoNhCP6Ab0Nun2IMw3iEP0AatCTFMIzP8SdIg2EYT8ekwTAMBZMGwzAUTBoMw1D4D4jGmD9arv4IAAAAAElFTkSuQmCC" alt="" />
2. urllib.quote_plus(string[, safe])
和上面的几乎一样,只是不转换空白字符,而将其用 + 号替换。
例子:
f = urllib.quote_plus('scolia good')
print f
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAX0AAAAqCAIAAADQ5xz9AAAGLUlEQVR4nO2dwZXjLAzHKYsStpFQhlPBcsvp2w58He6pwhd3kCb8HQw2EgLjxCbJ7v/39s1MPCCEkIWQ/WbVBAAAbVG7e4x3re/jq+MORnVK9e5VORKfr2EbXK8OsMMhYz2sLhtzMNumrmnzDmrtfLhHHWqQ87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7d8YyB0utmkquGas+34l9zGso3CnrTNP6VMYaU8F7d0vOPUffTGiXg0zokp4OXE8Tk9DSDGsQNQM5qQonybjNn19r6L1KJQxRw7nBTetQ0zlRw+XWopCT9pJID1J9p/wGw9bqSI9XeEVDfB4fjx4dxSP7JL3kZIdUvtwU8gvTG9Up/TtUQPzXqKjB64PEqrxNZhbxCsqzWOpN/UV30YsXnWSNuL5D5Uh2dqZ308O5aCy+poO1gyV+SD0zp7Nc+6NLn/pPkCYsoryCUhsuOeO9OyXvf2+QTLv0mox09mnMomEuIXq7hsdy3lsY7x3r0zhw7oU0eYut11a2aP3OTswrcSfHsD4t+gz8gw+SH32WhuDfJPHMGpaCyyEvx76HM+IOAACUQNwBALQGcQcA0BrEHQBAaxB3AACtQdwBALQGcQcA0BrEHQBAaw6IO6PVSiml2Ft4zvArpzJrgTcBAfgCjsp30igz2kvj1ykbRzoAwJOcF3fO7niqKADAiSDuAABaw+OOM8qjwykp1G9U9JdvNGkx8XveS6F/m4WLWYdaL5NrxoljpVfmXlprxB0AvgEWd6QyjV5CQvgrQlrbcf2e6ciuLB9z18U26VjFK4g7AHwD6TnLJw8hohQCihgy0mZUMM+DSLMlJwrDp2PV/AAA+Gh4vmP5Db9mEaO1B+Q7paxEzLaq8x1n8BwdgK+AxZ0l4VAklaEFH1ZhifqES1GZJvqfleZE6nLRtDQUk3asr+8YY/AGDwDfwEe9r1w+uAEA/hI+Ku5IqRMA4K/js+IOAOBfAHEHANAaxB0AQGsQdwAArUHcAQC0BnEHANAaxB0AQGsQdwAArUHcAQC0BnEHANAaxB0AQGv+B9UF/i/pUdqZAAAAAElFTkSuQmCC" alt="" />
3. urllib.unquote(string)
将 url 重新转码回去,是上面操作的urllib.quote(string[, safe])逆运算。
4. unquote_plus(string)
同上,是urllib.quote_plus(string[, safe])的逆运算。
5. urllib.urlencode(query[, doseq])
将一个python的字典快速转换为一个请求的格式,用于上面的 data 属性。
例子:
aDict = {'name': 'Georgina Garcia', 'hmdir': '~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y0tWr+FKZLf8jWbSCV8Vvln07FnM/VOFvjQghrqWir5mHZJ6GMlkKIuB139VV2XIA253jTjq4cFjGGeRvtTFPLqFhhRsc3d7eJDa9FmqalW/8FQZL8WhDqZ17LpHZi2qclFFKMHVuRKG/0QrespR6S1Qt/jIoC1vAns2PURSnpq0XLweeNOIc2K31HKpsi//E9FEIonWTY2T68cYAw+maHiDHMG9kUTTRZ47qzizmspXxh4MQkkm5+G7U2Nc7ZApQibS+5pBdUSdJDXAv7Q65qdJGIod2cG+3+Qxl8cyLj3HPD6FEZrZxsJ2eXsmLHn3PiyBNjcUDEWDSZ9zIqmv6NUaK6mBPFMWZrg96oRWaaLyy5jV7Qi5DwENfCrcfv0IEgMh1jGLaydqdcZrUbcfYGmn1vorVX7ZDm1+yE37V7xuKQiLFoMu9l80wT3DoNN3n7XlwuAuD3PwBAShle48Z6My3AR1e4FnGmWSSV2XWp0cfwudGLxtFr4WFa68/wVA7tT3qiUAYjb/2nKyOUVgLgcsc+/0bH2X7rvtCNUDR///dilW2dM9nKdvWpPhfXalf2jhj/fM+8kV/94fbGzpQ5lDLydx4L5nvwq4tm+hw2pyCngiPPY8F8B3550WQYhjkTFk2GYZgBWDQZhmEGYNFkGIYZgEWTYRhmABZNhmGYAVg0GYZhBmDRZBiGGYBFk2EYZgAWTYZhmAH+A5LQU8c8XWFDAAAAAElFTkSuQmCC" alt="" />
这里注意:一旦设置的 data,就意味使用 POST 请求,如果要使用 GET 请求,请在 url 的后面加上 ? 号,再将转换好的数据放进去。
例如:
GET方法:
import urllib
params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
f = urllib.urlopen("http://www.musi-cal.com/cgi-bin/query?%s" % params)
print f.read()
POST方法:
import urllib
params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
f = urllib.urlopen("http://www.musi-cal.com/cgi-bin/query", params)
print f.read()
6. urllib.pathname2url(path)
将本地路径名转换成 url 风格的路径名,这不会产生一个完整的URL,而且得到的结果已经调用了 quote() 方法进行了处理。
例子:
a = r'd:\abc\def\123.txt'
print urllib.pathname2url(a)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARIAAAAlCAIAAAABXfHVAAAEfElEQVR4nO2czXUjKxCFKyxCeImIMKQIHjvtXga9NXtF0YtHBkqCWTQ/BRQ06Nhjjed+Cx25VRQF1KVA9jH5Ppo2O/iYYzfSe/xhbxruWj3cYWjCm8Zs13RV5smeP426G+e9e6jQvLXx3ntn7so8y3gexvnySRXhVdvS81dGWBJb5U6fRm3WP80ldd3aVAz8t91ddTZNDds3gwh3TVcqIhEDqPoKfqy+xgViM0bbh9813Y1Lr8nh6cyLNm08gp/OKAaehVWmuiNGnDvvvffuKS2eGJ8wgJDZdpPWrzeAmTd8STJWj2dQ8PN1EUpzdSW6Ed2oCInNtmzDec6ujt3mEuVKOYC2915S8lZiX9FPll92+BF+5K9VkOOYm925imdGWnaT1uJk3UeyYbv4rvNWLWN12ma6+xCJ0di9k3DiXl7YOHNPWe7MY6DbJsLK81dFKMEyPqR7rDYqNWltauZWp9r+y7HHUE3yU40r9y7kqNSq7Ms9LnFHs/pwGD3bjT5ZNm08vr+Cjec8G22rxWoTM+kmJ1NFOAXFJu3Rwm5lWYiWes/bkt6j+tNg0v7R2vz3T94UeY/jCA+Du1JXoluxzJ8fYcqMktCKeVYPF5pH48JGZGJ16u3fe++duR8zFsee/LC+eO95P76x+WlayaWGz8yRixvRjfSm6UrqX6Kb1lt6PQp+6k7bzqzWNp1R8BWURxHjUdtFpfMbX3c+G0nwt7FslqiOGTXSwek3kyLslaNvj/BzaUvNz+jr+/lE2fQIu8X7zKnVVT18uwjBm/MbZAPATwOyAWAZyAaAZSAbAJaBbABYBrIBYBnIBoBlIBsAlpmWjdWkyr8BmHnymufwlIhWfgNZ+HFGnTU/TFqj10YB/iYmZeOMqtJLfnL5ONJdzMdJzwmrF2TT+hk3T/aVWeHHavZRlFke2epYwU9hTjZrpSYmotXnu/Zoa1+RjeBn3LzzafITJMH+wPoS/VtNpD/48AbaD5+b04HM2IA3YUY2s6UmPkkZyXUjpuk43eZlI/p5QTaVH27Tez8T6cxAlkor+GYmZLN8q2GyGadC6Scdgrj8tCai5s5SnYqqeI5CoVSWQGgVjNhpq3RUj0uO3xnFrKweH9HYQS7UqBAJu7yVNhDP+3Mqm9VS46dlc9rK6iOJcp6KF5LKT7TOj1P7cfWbuh2VmhkYDj52RmnLT30TLsBbcSabV75Aixkgp9jQT7HjikKSzmJyiSjk1+zl0kHr7HbUv4itHgjFwUM2fwxj2bxQanzKgKFq+lt7/iTmllBt0gVajEcZl2tVEYhznWpzfjs6akR8b4qvBE6+s07fkdgYj7aFv9YGvDVD2bxYasRTepmmYp6FY//looiU+d9bY+xxDckto/v8pV3Pz3EtSsLhIbHLTddPPY7i/lF+2zYsYrw/bUMbZVz9i6neb5HAOzL+zzUvlJoZXmv1/n7A3wL+uAaAZSAbAJaBbABYBrIBYBnIBoBlIBsAloFsAFgGsgFgGcgGgGUgGwCWgWwAWAayAWAZyAaAZX4B1+NONa4kBcIAAAAASUVORK5CYII=" alt="" />
7. urllib.url2pathname(path)
将 url 风格的路径转换为本地路径风格,是上面方法的逆运算。
a = r'www.xxx.com/12%203/321/'
print urllib.url2pathname(a)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZMAAAA4CAIAAAC30oKvAAAIPElEQVR4nO2cv4/kNBTH/We5oQPE33ASJwrGDUfPgZQtjqU7U410OhANZSSqdT+6Gq45LhJrWB3SVtMgrVa66vQo4iT+HWcmszPZ/X40Gs14nefnZ+ebZyc77B8AAFga7NgOAADAZNhLAABYGowAAGBpQLkAAMsDygUAWB77KZfecL7R+/rQCFYxVqt97cQ4fQ/vBlWzGeIwS1tbyfPBbMR4qEvqHIPSOM8+o2YNyOFmy3yWM8rVzrAzxs6YaNJ11tJyRInuEHYmJgRyp7jrDWeV25blswmQ7+GunNqpUjI6TmWhzOgIRVquyw7c0bFuOFJBywdzK1ejk7ukzt1j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t69uL62N4B8KCBcuX46PGfkC0AThAoV44vvvs7VK7Xze2x/QLgoQPlynH+8jpUrifnV8f2C4CHDpQrh3d7EWkXACcClCvHH29v+aM3H3/59tXvN/atRqRdABwXKFeOm9sP3/z4b/8M1+vm9tNVg7QLgKMD5ZrG5bv3j59qpF0AHBco12Rubj989f0V0i4AjgiUa0fOX14j7QLgWEC5AADLA8oFAFgeUC4AwPKAcgEAlgeUCwCwPKBcAIDlAeUCACwPKBcAYHlAuQAAywPKBQBYHv8DTAyfBk7QFlQAAAAASUVORK5CYII=" alt="" />
同样的调用了 quote() 方法进行了处理
其他的内容请参考官方文档:https://docs.python.org/2/library/urllib.html