python版本:2.7.15

1.简单用法urllib.urlopen()

语法:
urllib.urlopen(url[, data[, proxies]]) :
打开一个url的方法,返回一个文件对象,然后可以进行类似文件对象的操作。

示例代码:
googleResponse = urllib.urlopen('http://www.google.com.hk')
print 'http header:/n', googleResponse.info()
print 'http status:', googleResponse.getcode()
print 'url:', googleResponse.geturl()
# 读取html页面的第一行
firstLine = googleResponse.readline()
# 就像在操作本地文件
for line in googleResponse:
print line,
googleResponse.close()

urlopen返回对象提供方法:
- read() , readline() ,readlines() , fileno() , close() :这些方法的使用方式与文件对象完全一样
- info():返回一个httplib.HTTPMessage对象,表示远程服务器返回的头信息
- getcode():返回Http状态码。如果是http请求,200请求成功完成;404网址未找到
- geturl():返回请求的url

2.存储为文件urllib.urlretrieve()
语法:
urllib.urlretrieve(url[,filename[,reporthook[,data]]])
urlretrieve方法将url定位到的html文件下载到你本地的硬盘中。如果不指定filename,则会存为临时文件。
urlretrieve()返回一个二元组(filename,mine_hdrs)

临时存放:
filename = urllib.urlretrieve('http://www.google.com.hk/')
prtin type(filename)
# <type 'tuple'>
prtin filename[0]
# '/tmp/tmp8eVLjq'
print filename[1]
# <httplib.HTTPMessage instance at 0xb6a363ec>

存为本地文件:
filename = urllib.urlretrieve('http://www.google.com.hk/',filename='/home/python/google.html')
print type(filename)
# <type 'tuple'>
print filename[0]
# '/home/python/google.html'
print filename[1]
# <httplib.HTTPMessage instance at 0xb6e2c38c>

3.使用urllib实现post方法和get方法

需要用到urllib.urlencode(query)将URL中的参数键值对以连接符&划分

GET方法:
import urllib
params=urllib.urlencode({'name':'aaron','pwd':'123456','rem':0})
print params
# 'pwd=123456&name=aaron&rem=0'
f=urllib.urlopen("http://dev.xxx.com/login?%s" % params)
print f.read()

POST方法:
import urllib
parmas = urllib.urlencode({'name':'aaron','pwd':'123456','rem':0})
f=urllib.urlopen("http://dev.xxx.com/login",parmas)
f.read()

4.其它方法

urllib.urlcleanup()
清除由于urllib.urlretrieve()所产生的缓存

urllib.quote(url)和urllib.quote_plus(url)
将url数据获取之后,并将其编码,从而适用与URL字符串中,使其能被打印和被web服务器接受。
print urllib.quote('http://www.baidu.com')
# 'http%3A//www.baidu.com'
print urllib.quote_plus('http://www.baidu.com')
# 'http%3A%2F%2Fwww.baidu.com'

urllib.unquote(url)和urllib.unquote_plus(url)
与urllib.quote(url)和urllib.quote_plus(url)函数相反。

done!

python核心类库:urllib使用详解的更多相关文章

  1. Python爬虫系列-Urllib库详解

    Urllib库详解 Python内置的Http请求库: * urllib.request 请求模块 * urllib.error 异常处理模块 * urllib.parse url解析模块 * url ...

  2. Python爬虫之urllib.parse详解

    Python爬虫之urllib.parse 转载地址 Python 中的 urllib.parse 模块提供了很多解析和组建 URL 的函数. 解析url 解析url( urlparse() ) ur ...

  3. [转]使用python来操作redis用法详解

    转自:使用python来操作redis用法详解 class CommRedisBase(): def __init__(self): REDIS_CONF = {} connection_pool = ...

  4. 爬虫入门之urllib库详解(二)

    爬虫入门之urllib库详解(二) 1 urllib模块 urllib模块是一个运用于URL的包 urllib.request用于访问和读取URLS urllib.error包括了所有urllib.r ...

  5. 【转】maven核心,pom.xml详解

    感谢如下博主: http://www.cnblogs.com/qq78292959/p/3711501.html maven核心,pom.xml详解 什么是pom?    pom作为项目对象模型.通过 ...

  6. WebService核心之WSDL深入详解

    WebService核心之WSDL深入详解 根据上一篇文章开发的Web Service实例生成的WSDL文档如下: XML里两个属性介绍: targetNamespace          相当于ja ...

  7. Python安装、配置图文详解(转载)

    Python安装.配置图文详解 目录: 一. Python简介 二. 安装python 1. 在windows下安装 2. 在Linux下安装 三. 在windows下配置python集成开发环境(I ...

  8. 【和我一起学python吧】Python安装、配置图文详解

     Python安装.配置图文详解 目录: 一. Python简介 二. 安装python 1. 在windows下安装 2. 在Linux下安装 三. 在windows下配置python集成开发环境( ...

  9. Python中的高级数据结构详解

    这篇文章主要介绍了Python中的高级数据结构详解,本文讲解了Collection.Array.Heapq.Bisect.Weakref.Copy以及Pprint这些数据结构的用法,需要的朋友可以参考 ...

  10. Nginx核心配置文件常用参数详解

    Nginx核心配置文件常用参数详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 关于Nginx权威文档的话童鞋们可以参考Nginx官方文档介绍:http://nginx.org/ ...

随机推荐

  1. L255

    If a farmer wishes to succeed, he must try to keep a wide gap between his consumption and his produc ...

  2. vue--http请求的封装--session

    export function Fecth (url, data, file, _method) { if (file) { // 需要上传文件 return new Promise((resolve ...

  3. 【Python】xml遍历练习

      <?xml version="1.0" encoding="utf-8" ?> <!--this is a test about xml. ...

  4. 转 linux常用查看硬件设备信息命令

    转载自:http://blog.chinaunix.net/uid-26782198-id-3242120.html 系统 # uname -a               # 查看内核/操作系统/C ...

  5. opengl学习,一篇就够你基本了解

    http://blog.csdn.net/iduosi/article/details/7835624

  6. Linux矫正时间

    ntpdate -u ntp.api.bz 可以写到定时任务里,每天矫正一次

  7. Gym101986: Asia Tsukuba Regional Contest(寒假自训第12场)

    A .Secret of Chocolate Poles 题意:有黑白两种木块,黑色有1,K两种长度: 白色只有1一种长度,问满足黑白黑...白黑形式,长度为L的组合种类. 思路:直接DP即可. #i ...

  8. 管道符和作业 shell变量 环境变量

    管道符  |      前一个命令的输出,变成后一个命令的输入 ctrl +z   暂停 bg  cmd   后台运行 fg  #  调回前台 直接让程序进入后台,可以在后面加上 cmd    &am ...

  9. Guava Cache 总结

    想对Guava cache部分进行总结,但思索之后,文档才是最全面.详细的.所以,决定对guava文档进行翻译. 英文地址如下:https://github.com/google/guava/wiki ...

  10. Go Example--switch

    package main import ( "fmt" "time" ) func main() { i := 2 fmt.Print("write ...