1.urllib.urlopen(url[,data[,proxies]])

打开一个url的方法,返回一个文件对象,然后可以进行类似文件对象的操作。本例试着打开google

>>> import urllib
>>> f = urllib.urlopen('http://www.google.com.hk/')
>>> firstLine = f.readline() #读取html页面的第一行
>>> firstLine
'<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage"><head><meta content="/images/google_favicon_128.png" itemprop="image"><title>Google</title><script>(function(){\n'

urlopen返回对象提供方法:

-         read() , readline() ,readlines() , fileno() , close() :这些方法的使用方式与文件对象完全一样

-         info():返回一个httplib.HTTPMessage对象,表示远程服务器返回的头信息

-         getcode():返回Http状态码。如果是http请求,200请求成功完成;404网址未找到

-         geturl():返回请求的url

2.urllib.urlretrieve(url[,filename[,reporthook[,data]]])

urlretrieve方法将url定位到的html文件下载到你本地的硬盘中。如果不指定filename,则会存为临时文件。

urlretrieve()返回一个二元组(filename,mine_hdrs)

临时存放:

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

存为本地文件:

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

3.urllib.urlcleanup()

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

4.urllib.quote(url)和urllib.quote_plus(url)

将url数据获取之后,并将其编码,从而适用与URL字符串中,使其能被打印和被web服务器接受。

>>> urllib.quote('http://www.baidu.com')
'http%3A//www.baidu.com'
>>> urllib.quote_plus('http://www.baidu.com')
'http%3A%2F%2Fwww.baidu.com'

5.urllib.unquote(url)和urllib.unquote_plus(url)

与4的函数相反。

6.urllib.urlencode(query)

将URL中的键值对以连接符&划分

这里可以与urlopen结合以实现post方法和get方法:

GET方法:

>>> import urllib
>>> params=urllib.urlencode({'spam':1,'eggs':2,'bacon':0})
>>> params
'eggs=2&bacon=0&spam=1'
>>> f=urllib.urlopen("http://python.org/query?%s" % params)
>>> print f.read()

POST方法:

>>> import urllib
>>> parmas = urllib.urlencode({'spam':1,'eggs':2,'bacon':0})
>>> f=urllib.urlopen("http://python.org/query",parmas)
>>> f.read()

最新文章

  1. JAVA对MySQL数据库的操作
  2. 多节点ssh免密匙登录
  3. iOS 苹果自带地图定位Core Location
  4. (转)WebApi自动生成在线文档WebApiTestClient
  5. RAW格式
  6. IE jquery mouseenter,mouseover超奇葩问题
  7. 生产环境CentOS服务器系统安全配置
  8. gmapping 学习
  9. JSON跟JSONP的区别以及实战
  10. 谈谈语音通信中的各种tone
  11. 微信浏览器无法跳转到apk下载链接 微信屏蔽了我的APP下载链接如何处理
  12. Spring源码学习笔记1
  13. 朱晔的互联网架构实践心得S2E2:写业务代码最容易掉的10种坑
  14. js语法没有任何问题但是就是不走,检查js中命名的变量名,用 service-area错误,改service_area (原)
  15. maven 引入外部jar包的几种方式
  16. PHPexcel 导入import 数据到 mysql: mysql 查询数据是否存在, 如果存在返回id, 不存在, 插入返回id. 2) mysql_query , mysql_connect, mysql_select_db, mysql_error, mysql_num_rows,mysql_close
  17. Andorid第一次作业
  18. VisualSVN 破解方法
  19. 【设计模式】—— 享元模式Flyweight
  20. iOS系统库头文件中NS_AVAILABLE相关

热门文章

  1. Vim统计字符串出现次数
  2. python2与python3编码
  3. python相关软件安装流程图解——Windows下安装Redis以及可视化工具——Redis-x64-3.2.100——redis-desktop-manager-0.9.3.817
  4. swiper在loop模式,当轮播到最后一张图时候,做其他事件
  5. .NETFramework:Exception
  6. System.Object.cs
  7. LTE基本架构
  8. 最全Linux常用命令大全
  9. 保持SSH连接的linux服务器不断线
  10. adb 使用记录