(二)urllib库的基本使用

来源:互联网 发布:经纬度换算坐标软件 编辑:程序博客网 时间:2024/06/05 19:30

什么是Urllib

Urllib是python内置的HTTP请求库
包括以下模块
urllib.request 请求模块
urllib.error 异常处理模块
urllib.parse url解析模块
urllib.robotparser robots.txt解析模块

import urllib.requestresponse = urllib.request.urlopen(' print(response.read().decode('utf-8'))

urlopen一般常用的有三个参数,它的参数如下:
urllib.requeset.urlopen(url,data,timeout)
response.read()可以获取到网页的内容,如果没有read(),将返回如下内容

data参数的使用

上述的例子是通过请求百度的get请求获得百度,下面使用urllib的post请求
通过http://httpbin.org/post网站演示(该网站可以作为练习使用urllib的一个站点使用,可以
模拟各种请求操作)。

import urllib.parseimport urllib.requestdata = bytes(urllib.parse.urlencode({'word': 'hello'}), encoding='utf8')print(data)response = urllib.request.urlopen('http://httpbin.org/post', data=data)print(response.read())

这里就用到urllib.parse,通过bytes(urllib.parse.urlencode())可以将post数据进行转换放到urllib.request.urlopen的data参数中。这样就完成了一次post请求。
所以如果我们添加data参数的时候就是以post请求方式请求,如果没有data参数就是get请求方式
timeout参数的使用
在某些网络情况不好或者服务器端异常的情况会出现请求慢的情况,或者请求异常,所以这个时候我们需要给
请求设置一个超时时间,而不是让程序一直在等待结果。

import urllib.request response = urllib.request.urlopen('http://httpbin.org/get', timeout=1)print(response.read())

运行之后我们看到可以正常的返回结果,接着我们将timeout时间设置为0.1
运行程序会提示错误
需要对异常进行抓取,代码更改为

import socketimport urllib.requestimport urllib.errortry:    response = urllib.request.urlopen('http://httpbin.org/get', timeout=0.1)except urllib.error.URLError as e:   if isinstance(e.reason, socket.timeout):       print('TIME OUT')

**响应类型、状态码、响应头

import urllib.requestresponse = urllib.request.urlopen(' print(type(response))

可以看到结果为:

**request**设置Headers有很多网站为了防止程序爬虫爬网站造成网站瘫痪,会需要携带一些headers头部信息才能访问,最常见的有user-agent参数import urllib.requestrequest = urllib.request.Request('https://python.org')response = urllib.request.urlopen(request)print(response.read().decode('utf-8'))

给请求添加头部信息,从而定制自己请求网站是时的头部信息

from urllib import request, parseurl = 'http://httpbin.org/post'headers = {    'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)',    'Host': 'httpbin.org'}dict = {    'name': 'zhaofan'}data = bytes(parse.urlencode(dict), encoding='utf8')req = request.Request(url=url, data=data, headers=headers, method='POST')response = request.urlopen(req)print(response.read().decode('utf-8'))

添加请求头的第二种方式

from urllib import request, parseurl = 'http://httpbin.org/post'dict = {    'name': 'Germey'}data = bytes(parse.urlencode(dict), encoding='utf8')req = request.Request(url=url, data=data, method='POST')req.add_header('User-Agent', 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')response = request.urlopen(req)print(response.read().decode('utf-8'))

这种添加方式有个好处是自己可以定义一个请求头字典,然后循环进行添加

**高级用法各种handler
代理,ProxyHandler**

通过rulllib.request.ProxyHandler()可以设置代理,网站它会检测某一段时间某个IP 的访问次数,如果访问次数过多,它会禁止你的访问,所以这个时候需要通过设置代理来爬取数据

import urllib.requestproxy_handler = urllib.request.ProxyHandler({    'http': 'http://127.0.0.1:9743',    'https': 'https://127.0.0.1:9743'})opener = urllib.request.build_opener(proxy_handler)response = opener.open('http://httpbin.org/get')print(response.read())

cookie,HTTPCookiProcessor
cookie中保存中我们常见的登录信息,有时候爬取网站需要携带cookie信息访问,这里用到了http.cookijar,用于获取cookie以及存储cookie

import http.cookiejar, urllib.requestcookie = http.cookiejar.CookieJar()handler = urllib.request.HTTPCookieProcessor(cookie)opener = urllib.request.build_opener(handler)response = opener.open('http://www.baidu.com')for item in cookie:    print(item.name+"="+item.value)

同时cookie可以写入到文件中保存,有两种方式http.cookiejar.MozillaCookieJar和http.cookiejar.LWPCookieJar()
http.cookiejar.MozillaCookieJar()方式

import http.cookiejar, urllib.requestfilename = "cookie.txt"cookie = http.cookiejar.MozillaCookieJar(filename)handler = urllib.request.HTTPCookieProcessor(cookie)opener = urllib.request.build_opener(handler)response = opener.open('http://www.baidu.com')cookie.save(ignore_discard=True, ignore_expires=True)

http.cookiejar.LWPCookieJar()方式

import http.cookiejar, urllib.requestfilename = 'cookie.txt'cookie = http.cookiejar.LWPCookieJar(filename)handler = urllib.request.HTTPCookieProcessor(cookie)opener = urllib.request.build_opener(handler)response = opener.open('http://www.baidu.com')cookie.save(ignore_discard=True, ignore_expires=True)

如果想要通过获取文件中的cookie获取的话可以通过load方式,当然用哪种方式写入的,就用哪种方式读取

import http.cookiejar, urllib.requestcookie = http.cookiejar.LWPCookieJar()cookie.load('cookie.txt', ignore_discard=True, ignore_expires=True)handler = urllib.request.HTTPCookieProcessor(cookie)opener = urllib.request.build_opener(handler)response = opener.open('http://www.baidu.com')print(response.read().decode('utf-8'))

异常处理
在很多时候我们通过程序访问页面的时候,有的页面可能会出现错误,类似404,500等错误
捕捉异常简单代码

from urllib import request,errortry:    response = request.urlopen("http://pythonsite.com/1111.html")except error.URLError as e:    print(e.reason)

上述代码访问的是一个不存在的页面,通过捕捉异常,我们可以打印异常错误
这里我们需要知道的是在urllb异常这里有两个个异常错误:
URLError,HTTPError,HTTPError是URLError的子类
URLError里只有一个属性:reason,即抓异常的时候只能打印错误信息,类似上面的例子
HTTPError里有三个属性:code,reason,headers,即抓异常的时候可以获得code,reson,headers三个信息

from urllib import request,errortry:    response = request.urlopen("http://pythonsite.com/1111.html")except error.HTTPError as e:    print(e.reason)    print(e.code)    print(e.headers)except error.URLError as e:    print(e.reason)else:    print("reqeust successfully")

同时,e.reason其实也可以在做深入的判断

import socketfrom urllib import error,requesttry:    response = request.urlopen("http://www.pythonsite.com/",timeout=0.001)except error.URLError as e:    print(type(e.reason))    if isinstance(e.reason,socket.timeout):        print("time out")

URL解析

from urllib.parse import urlparseresult = urlparse(" print(result)

可以对你传入的url地址进行拆分
同时我们是可以指定协议类型:
result = urlparse(“www.baidu.com/index.html;user?id=5#comment”,scheme=”https”)
这样拆分的时候协议类型部分就会是你指定的部分,当然如果你的url里面已经带了协议,你再通过scheme指定的协议就不会生效

urlunpars
其实功能和urlparse的功能相反,它是用于拼接

from urllib.parse import urlunparsedata = ['http',' print(urlunparse(data))

urljoin
拼接

from urllib.parse import urljoinprint(urljoin('http://www.baidu.com', 'FAQ.html'))print(urljoin('http://www.baidu.com', 'https://pythonsite.com/FAQ.html'))print(urljoin('http://www.baidu.com/about.html', 'https://pythonsite.com/FAQ.html'))print(urljoin('http://www.baidu.com/about.html', 'https://pythonsite.com/FAQ.html?question=2'))print(urljoin('http://www.baidu.com?wd=abc', 'https://pythonsite.com/index.php'))print(urljoin('http://www.baidu.com', '?category=2#comment'))print(urljoin('www.baidu.com', '?category=2#comment'))print(urljoin('www.baidu.com#comment', '?category=2'))

拼接的时候后面的优先级高于前面的url

urlencode
这个方法可以将字典转换为url参数,

from urllib.parse import urlencodeparams = {    "name":"zhaofan",    "age":23,}base_url = "http://www.baidu.com?"url = base_url+urlencode(params)print(url)