urlib模块介绍

uril模块介绍(原文)

爬虫


爬虫所需要的功能,基本上在urllib中都能找到,学习这个标准库,可以更加深入的理解后面更加便利的requests库。

  • 首先:
  • 在Pytho2.x中使用import urllib2——-对应的,在Python3.x中会使用import urllib.request,urllib.error
  • 在Pytho2.x中使用import urllib——-对应的,在Python3.x中会使用import urllib.request,urllib.error,urllib.parse
  • 在Pytho2.x中使用import urlparse——-对应的,在Python3.x中会使用import urllib.parse
  • 在Pytho2.x中使用import urlopen——-对应的,在Python3.x中会使用import urllib.request.urlopen
  • 在Pytho2.x中使用import urlencode——-对应的,在Python3.x中会使用import urllib.parse.urlencode
  • 在Pytho2.x中使用import urllib.quote——-对应的,在Python3.x中会使用import urllib.request.quote
  • 在Pytho2.x中使用cookielib.CookieJar——-对应的,在Python3.x中会使用http.CookieJar
  • 在Pytho2.x中使用urllib2.Request——-对应的,在Python3.x中会使用urllib.request.Request

urllib是Python自带的标准库,无需安装,直接可以用。 
提供了如下功能:

  • 网页请求(urllib.request)
  • URL解析(urllib.parse)
  • 代理和cookie设置
  • 异常处理(urllib.error)
  • robots.txt解析模块(urllib.robotparser)

urllib包中urllib.request模块

1、urllib.request.urlopen

urlopen一般常用的有三个参数,它的参数如下: 
r = urllib.requeset.urlopen(url,data,timeout) 
url:链接格式:协议://主机名:[端口]/路径 
data:附加参数 必须是字节流编码格式的内容(bytes类型),可通过bytes()函数转化,如果要传递这个参数,请求方式就不再是GET方式请求,而是POST方式 
timeout: 超时 单位为秒

get请求


  1. import urllib
  2. r = urllib.urlopen('http://www.google.com.hk/')
  3. datatLine = r.readline() #读取html页面的第一行
  4. data=file.read() #读取全部
  5. f=open("./1.html","wb") # 网页保存在本地
  6. f.write(data)
  7. f.close()

urlopen返回对象提供方法:

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

urllib.quote(url)和urllib.quote_plus(url),对关键字进行编码可使得urlopen能够识别

POST请求


  1. import urllib.request
  2. import urllib.parse
  3. url = 'https://passport.cnblogs.com/user/signin?'
  4. post = {
  5. 'username': 'xxx',
  6. 'password': 'xxxx'
  7. }
  8. postdata = urllib.parse.urlencode(post).encode('utf-8')
  9. req = urllib.request.Request(url, postdata)
  10. r = urllib.request.urlopen(req)

我们在进行注册、登录等操作时,会通过POST表单传递信息 
这时,我们需要分析页面结构,构建表单数据post,使用urlencode()进行编码处理,返回字符串,再指定’utf-8’的编码格式,这是因为POSTdata只能是bytes或者file object。最后通过Request()对象传递postdata,使用urlopen()发送请求。

2、urllib.request.Request

urlopen()方法可以实现最基本请求的发起,但这几个简单的参数并不足以 构建一个完整的请求,如果请求中需要加入headers(请求头)等信息模拟浏览器,我们就可以利用更强大的Request类来构建一个请求。


  1. #用Request类构建了一个完整的请求,增加了headers等一些信息
  2. import urllib.request
  3. import urllib.parse
  4. url = 'http://httpbin.org/post'
  5. headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
  6. headers['Host'] = 'httpbin.org'
  7. dict = {'name':'Germey'}
  8. data = urllib.parse.urlencode(dict).encode('utf-8')
  9. #data参数如果要传必须传bytes(字节流)类型的,如果是一个字典,先用urllib.parse.urlencode()编码。
  10. request = urllib.request.Request(url = url,data = data,headers = headers,method = 'POST')
  11. response = urllib.request.urlopen(request)
  12. html = response.read().decode('utf-8')
  13. print(html)

3、urllib.request.BaseHandler

在上面的过程中,我们虽然可以构造Request ,但是一些更高级的操作,比如 Cookies处理,代理该怎样来设置? 接下来就需要更强大的工具 Handler 登场了 基本的urlopen()函数不支持验证、cookie、代理或其他HTTP高级功能。要支持这些功能,必须使用build_opener()函数来创建自己的自定义opener对象。 首先介绍下 urllib.request.BaseHandler ,它是所有其他 Handler 的父类,它提供了最基本的 Handler 的方法。

  • HTTPDefaultErrorHandler 用于处理HTTP响应错误,错误都会抛出 HTTPError 类型的异常。
  • HTTPRedirectHandler 用于处理重定向
  • HTTPCookieProcessor 用于处理 Cookie 。
  • ProxyHandler 用于设置代理,默认代理为空。
  • HTTPPasswordMgr用于管理密码,它维护了用户名密码的表。
  • HTTPBasicAuthHandler 用于管理认证,如果一个链接打开时需要认证,那么可以用它来解决认证问题。

代理服务器设置


  1. def use_proxy(proxy_addr,url):
  2. import urllib.request
  3. #构建代理
  4. proxy=urllib.request.ProxyHandler({'http':proxy_addr})
  5. # 构建opener对象
  6. opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandler)
  7. # 安装到全局
  8. # urllib.request.install_opener(opener)
  9. # data=urllib.request.urlopen(url).read().decode('utf8') 以全局方式打开
  10. data=opener.open(url) # 直接用句柄方式打开
  11. return data
  12. proxy_addr='61.163.39.70:9999'
  13. data=use_proxy(proxy_addr,'http://www.baidu.com')
  14. print(len(data))
  15. ## 异常处理以及日输出
  • opener通常是build_opener()创建的opener对象。
  • install_opener(opener) 安装opener作为urlopen()使用的全局URL opener

cookie的使用

获取Cookie保存到变量


  1. import http.cookiejar, urllib.request
  2. #使用http.cookiejar.CookieJar()创建CookieJar对象
  3. cookie = http.cookiejar.CookieJar()
  4. handler = urllib.request.HTTPCookieProcessor(cookie)
  5. #使用HTTPCookieProcessor创建cookie处理器,并以其为参数构建opener对象
  6. opener = urllib.request.build_opener(handler)
  7. #将opener安装为全局
  8. urllib.request.install_opener(opener)
  9. response = urllib.request.urlopen('http://www.baidu.com')
  10. #response = opener.open('http://www.baidu.com')
  11. for item in cookie:
  12. print 'Name = '+item.name
  13. print 'Value = '+item.value

首先我们必须声明一个 CookieJar 对象,接下来我们就需要利用 HTTPCookieProcessor 来构建一个 handler ,最后利用 build_opener 方法构建出 opener ,执行 open() 即可。 最后循环输出cookiejar

获取Cookie保存到本地


  1. import cookielib
  2. import urllib
  3. #设置保存cookie的文件,同级目录下的cookie.txt
  4. filename = 'cookie.txt'
  5. #声明一个MozillaCookieJar对象实例来保存cookie,之后写入文件
  6. cookie = cookielib.MozillaCookieJar(filename)
  7. #利用urllib库的HTTPCookieProcessor对象来创建cookie处理器
  8. handler = urllib.request.HTTPCookieProcessor(cookie)
  9. #通过handler来构建opener
  10. opener = urllib.request.build_opener(handler)
  11. #创建一个请求,原理同urllib2的urlopen
  12. response = opener.open("http://www.baidu.com")
  13. #保存cookie到文件
  14. cookie.save(ignore_discard=True, ignore_expires=True)

从文件中获取Cookie并访问


  1. mport cookielib
  2. import urllib2
  3. #创建MozillaCookieJar实例对象
  4. cookie = cookielib.MozillaCookieJar()
  5. #从文件中读取cookie内容到变量
  6. cookie.load('cookie.txt', ignore_discard=True, ignore_expires=True)
  7. #创建请求的request
  8. req = urllib.request.Request("http://www.baidu.com")
  9. #利用urllib的build_opener方法创建一个opener
  10. opener = urllib.request.build_opener(urllib2.HTTPCookieProcessor(cookie))
  11. response = opener.open(req)
  12. print response.read()

异常处理

异常处理结构如下


  1. try:
  2. # 要执行的代码
  3. print(...)
  4. except:
  5. #try代码块里的代码如果抛出异常了,该执行什么内容
  6. print(...)
  7. else:
  8. #try代码块里的代码如果没有跑出异常,就执行这里
  9. print(...)
  10. finally:
  11. #不管如何,finally里的代码,是总会执行的
  12. print(...)

URLerror产生原因:

  • 1、网络未连接(即不能上网)

  1. from urllib import request, error
  2. try:
  3. r=request.urlopen('http://www.baidu.com')
  4. except error.URLError as e:
  5. print(e.reason)
  • 2、访问页面不存(HTTPError)

客户端向服务器发送请求,如果成功地获得请求的资源,则返回的状态码为200,表示响应成功。如果请求的资源不存在, 则通常返回404错误。


  1. from urllib imort request, error
  2. try:
  3. response = request.urlopen('http://www.baodu.com')
  4. except error.HTTPError as e:
  5. print(e.reason, e.code, e.headers, sep='\n')
  6. else:
  7. print("Request Successfully')
  8. # 加入 hasattr属性提前对属性,进行判断原因
  9. from urllib import request,error
  10. try:
  11. response=request.urlopen('http://blog.csdn.ne')
  12. except error.HTTPError as e:
  13. if hasattr(e,'code'):
  14. print('the server couldn\'t fulfill the request')
  15. print('Error code:',e.code)
  16. elif hasattr(e,'reason'):
  17. print('we failed to reach a server')
  18. print('Reason:',e.reason)
  19. else:
  20. print('no exception was raised')
  21. # everything is ok

你可能感兴趣的:(py3爬虫)