原文连接:http://blog.csdn.net/dolphin_h/article/details/45296353php
慢慢的把它们总结一下,总结就是最好的学习方法html
宗述
首先来看一下他们的区别python
urllib和urllib2ajax
urllib 和urllib2都是接受URL请求的相关模块,可是urllib2能够接受一个Request类的实例来设置URL请求的headers,urllib仅能够接受URL。浏览器
这意味着,你不能够假装你的User Agent字符串等。服务器
urllib提供urlencode方法用来GET查询字符串的产生,而urllib2没有。这是为什么urllib常和urllib2一块儿使用的缘由。cookie
目前的大部分http请求都是经过urllib2来访问的app
httplibpost
httplib实现了HTTP和HTTPS的客户端协议,通常不直接使用,在python更高层的封装模块中(urllib,urllib2)使用了它的http实现。学习
urllib简单用法
urllib.urlopen(url[, data[, proxies]]) :
- google = urllib.urlopen('http://www.google.com')
- print 'http header:/n', google.info()
- print 'http status:', google.getcode()
- print 'url:', google.geturl()
- for line in google:
- print line,
- google.close()
详细使用方法见
urllib学习
urllib2简单用法
最简单的形式
- import urllib2
- response=urllib2.urlopen('http://www.douban.com')
- html=response.read()
实际步骤:
一、urllib2.Request()的功能是构造一个请求信息,返回的req就是一个构造好的请求
二、urllib2.urlopen()的功能是发送刚刚构造好的请求req,并返回一个文件类的对象response,包括了全部的返回信息。
三、经过response.read()能够读取到response里面的html,经过response.info()能够读到一些额外的信息。
以下:
- import urllib2
- req = urllib2.Request("http://www.douban.com")
- response = urllib2.urlopen(req)
- html = response.read()
- print html
有时你会碰到,程序也对,可是服务器拒绝你的访问。这是为何呢?问题出在请求中的头信息(header)。 有的服务端有洁癖,不喜欢程序来触摸它。这个时候你须要将你的程序假装成浏览器来发出请求。请求的方式就包含在header中。
常见的情形:
- import urllib
- import urllib2
- url = 'http://www.someserver.com/cgi-bin/register.cgi'
- user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
- values = {'name' : 'who','password':'123456'}
- headers = { 'User-Agent' : user_agent }
- data = urllib.urlencode(values)
- req = urllib2.Request(url, data, headers)
- response = urllib2.urlopen(req)
- the_page = response.read()
values是post数据
GET方法
例如百度:
百度是经过http://www.baidu.com/s?wd=XXX 来进行查询的,这样咱们须要将{‘wd’:’xxx’}这个字典进行urlencode
- import urllib
- import urllib2
- url = 'http://www.baidu.com/s'
- values = {'wd':'D_in'}
- data = urllib.urlencode(values)
- print data
- url2 = url+'?'+data
- response = urllib2.urlopen(url2)
- the_page = response.read()
- print the_page
POST方法
- import urllib
- import urllib2
- url = 'http://www.someserver.com/cgi-bin/register.cgi'
- user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' //将user_agent写入头信息
- values = {'name' : 'who','password':'123456'} //post数据
- headers = { 'User-Agent' : user_agent }
- data = urllib.urlencode(values) //对post数据进行url编码
- req = urllib2.Request(url, data, headers)
- response = urllib2.urlopen(req)
- the_page = response.read()
urllib2带cookie的使用
- import urllib2,urllib
- import cookielib
-
- url = r'http://www.renren.com/ajaxLogin'
-
- cj = cookielib.CookieJar()
- opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
- data = urllib.urlencode({"email":email,"password":pass})
- r = opener.open(url,data)
- print cj
httplib简单用法
简单示例
- import httplib
- import urllib
-
- def sendhttp():
- data = urllib.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'})
- headers = {"Content-type": "application/x-www-form-urlencoded",
- "Accept": "text/plain"}
- conn = httplib.HTTPConnection('bugs.python.org')
- conn.request('POST', '/', data, headers)
- httpres = conn.getresponse()
- print httpres.status
- print httpres.reason
- print httpres.read()
-
- if __name__ == '__main__':
- sendhttp()
具体用法见
httplib模块