Python urllib库
Python urllib库用于操作网页URL,并对网页的内容进行抓取处理。
urllib包 包含以下几个模块:
	- urllib.request - 打开和读取URL。
 
	- urllib.error - 包含urllib.request抛出的异常。
 
	- urllib.parse - 解析URL。
 
	- urllib.robotparser - 解析robots.txt文件。
 
需要用的就是每个模块的内置方法和函数。大概方法如下图:

urllib.request模块
urllib.request定义了一些打开URL的函数和类,包含授权验证、重定向、浏览器cookies等。
urllib.request可以模拟浏览器的一个请求发起过程。
这里主要介绍两个常用方法,urlopen和Request。
urlopen函数
语法格式如下:
	
		
			| 
			 1 
			 | 
			
			 urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None) 
			 | 
		
	
	- url:url地址。
 
	- data:发送到服务器的其他数据对象,默认为None。
 
	- timeout:设置访问超时时间。
 
	- cafile 和 capath:cafile为CA证书,capath为CA证书的路径,使用HTTPS需要用到。
 
	- cadefault:已经被弃用。
 
	- context:ssl.SSLContext类型,用来指定SSL设置。
 
示例:
	
		
			| 
			 1 
			2 
			3 
			4 
			5 
			6 
			 | 
			
			 import urllib.request 
			#导入urllib.request模块 
			url=urllib.request.urlopen("https://www.baidu.com") 
			#打开读取baidu信息 
			print(url.read().decode('utf-8')) 
			#read获取所有信息,并decode()命令将网页的信息进行解码 
			 | 
		
	
运行结果
<!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta content="always" name="
html{color:#000;overflow-y:scroll;overflow:-moz-scrollbars}
body,button,input,select,textarea{font-size:12px;font-family:Arial,sans-serif}  
h1,h2,h3,h4,h5,h6{font-size:100%}                  
em{font-style:normal}
small{font-size:12px}
ol,ul{list-style:none}
a{text-decoration:none}
a:hover{text-decoration:underline}
legend{color:#000}
fieldset,img{border:0}
button,input,select,textarea{font-size:100%} 
...
response对象是http.client.HTTPResponse类型,主要包含read、readinto、getheader、getheaders、fileno等方法,以及msg、version、status、reason、debuglevel、closed等属性。
常用方法:
	- read():是读取整个网页内容,也可以指定读取的长度,如read(300)。获取到的是二进制的乱码,所以需要用到decode()命令将网页的信息进行解码。
 
	- readline(): 读取文件的一行内容。
 
	- readlines(): 读取文件的全部内容,它会把读取的内容赋值给一个列表变量。
 
	- info():返回HTTPMessage对象,表示远程服务器返回的头信息。
 
	- getcode():返回Http状态码。如果是http请求,200请求成功完成;404网址未找到。
 
	- geturl():返回请求的链接。
 
Request 类
我们抓取网页一般需要对headers(网页头信息)进行模拟,否则网页很容易判定程序为爬虫,从而禁止访问。这时候需要使用到urllib.request.Request类:
	
		
			| 
			 1 
			 | 
			
			 class urllib.request.Request(url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None) 
			 | 
		
	
	- url:url地址。
 
	- data:发送到服务器的其他数据对象,默认为None。
 
	- headers:HTTP请求的头部信息,字典格式。
 
	- origin_req_host:请求的主机地址,IP或域名。
 
	- unverifiable:很少用整个参数,用于设置网页是否需要验证,默认是False。
 
	- method:请求方法, 如 GET、POST、DELETE、PUT等。
 
示例:
	
		
			| 
			 1 
			2 
			3 
			4 
			5 
			6 
			7 
			8 
			9 
			10 
			11 
			12 
			13 
			 | 
			
			 import urllib.request 
			#导入模块 
			url = "https://www.baidu.com" 
			#网页连接 
			headers = { 
			"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36" 
			} 
			#定义headers,模拟浏览器访问 
			req = urllib.request.Request(url=url,headers=headers) 
			#模拟浏览器发送,访问网页 
			response = urllib.request.urlopen(req) 
			#获取页面信息 
			print(response.read().decode("utf-8")) 
			 | 
		
	
urllib.error模块
urllib.error模块为urllib.request所引发的异常定义了异常类,基础异常类是URLError。
urllib.error包含了两个方法,URLError和HTTPError。
URLError是OSError的一个子类,用于处理程序在遇到问题时会引发此异常(或其派生的异常),包含的属性reason为引发异常的原因。
HTTPError是URLError的一个子类,用于处理特殊HTTP错误例如作为认证请求的时候,包含的属性code为HTTP的状态码,reason为引发异常的原因,headers为导致HTTPError的特定HTTP请求的HTTP响应头。
区别:
	- URLError封装的错误信息一般是由网络引起的,包括url错误。
 
	- HTTPError封装的错误信息一般是服务器返回了错误状态码。
 
关系:
URLError是OSERROR的子类,HTTPError是URLError的子类。
URLError 示例
	
		
			| 
			 1 
			2 
			3 
			4 
			5 
			6 
			7 
			8 
			9 
			10 
			11 
			12 
			13 
			 | 
			
			 from urllib import request 
			from urllib import error 
			  
			if __name__ == "__main__": 
			    #一个不存在的连接 
			    url = "http://www.baiiiduuuu.com/" 
			    req = request.Request(url) 
			    try: 
			        response = request.urlopen(req) 
			        html = response.read().decode('utf-8') 
			        print(html) 
			    except error.URLError as e: 
			        print(e.reason) 
			 | 
		
	
返回结果
[Errno -2] Name or service not known
此错误的原因。它可以是一个消息字符串或另一个异常实例。
HTTPError示例
	
		
			| 
			 1 
			2 
			3 
			4 
			5 
			6 
			7 
			8 
			9 
			10 
			11 
			12 
			13 
			 | 
			
			 from urllib import request 
			from urllib import error 
			  
			if __name__ == "__main__": 
			    #网站服务器上不存在资源 
			    url = "http://www.baidu.com/no.html" 
			    req = request.Request(url) 
			    try: 
			        response = request.urlopen(req) 
			        html = response.read().decode('utf-8') 
			        print(html) 
			    except error.HTTPError as e: 
			        print(e.code) 
			 | 
		
	
output
404
URLError和HTTPError混合使用
注意:由于HTTPError是URLError的子类,所以捕获的时候HTTPError要放在URLError的上面。
示例:
	
		
			| 
			 1 
			2 
			3 
			4 
			5 
			6 
			7 
			8 
			9 
			10 
			11 
			12 
			13 
			14 
			 | 
			
			 from urllib import request 
			from urllib import error 
			  
			if __name__ == "__main__": 
			    #网站服务器上不存在资源 
			    url = "http://www.baidu.com/no.html" 
			    req = request.Request(url) 
			    try: 
			        response = request.urlopen(req) 
			 #       html = response.read().decode('utf-8') 
			    except error.HTTPError as e: 
			        print(e.code) 
			    except error.URLError as e: 
			        print(e.code) 
			 | 
		
	
如果不用上面的方法,可以直接用判断的形式
	
		
			| 
			 1 
			2 
			3 
			4 
			5 
			6 
			7 
			8 
			9 
			10 
			11 
			12 
			13 
			14 
			15 
			16 
			17 
			 | 
			
			 from urllib import request 
			from urllib import error 
			  
			if __name__ == "__main__": 
			    #网站服务器上不存在资源 
			    url = "http://www.baidu.com/no.html" 
			    req = request.Request(url) 
			    try: 
			        response = request.urlopen(req) 
			 #       html = response.read().decode('utf-8') 
			    except error.URLError as e: 
			        if hasattr(e, 'code'): 
			            print("HTTPError") 
			            print(e.code) 
			        elif hasattr(e, 'reason'): 
			            print("URLError") 
			            print(e.reason) 
			 | 
		
	
output
HTTPError
404
urllib.parse模块
模块定义的函数可分为两个主要门类:URL解析和URL转码。
urlparse()
urllib.parse用于解析URL,格式如下:
	
		
			| 
			 1 
			 | 
			
			 urllib.parse.urlparse(urlstring, scheme='', allow_fragments=True) 
			 | 
		
	
urlstring为字符串的url地址,scheme为协议类型。
allow_fragments参数为false,则无法识别片段标识符。相反,它们被解析为路径,参数或查询组件的一部分,并fragment在返回值中设置为空字符串。
标准链接格式为:
	
		
			| 
			 1 
			 | 
			
			 scheme://netloc/path;params?query#fragment 
			 | 
		
	
对象中包含了六个元素,分别为:协议(scheme)、域名(netloc)、路径(path)、路径参数(params)、查询参数(query)、片段(fragment)。
示例:
	
		
			| 
			 1 
			2 
			3 
			4 
			5 
			6 
			7 
			8 
			9 
			10 
			11 
			 | 
			
			 from urllib.parse import urlparse 
			  
			o = urlparse("https://docs.python.org/zh-cn/3/library/urllib.parse.html#module-urllib.parse") 
			  
			print('scheme  :', o.scheme) 
			print('netloc  :', o.netloc) 
			print('path    :', o.path) 
			print('params  :', o.params) 
			print('query   :', o.query) 
			print('fragment:', o.fragment) 
			print('hostname:', o.hostname) 
			 | 
		
	
output
scheme  : https
netloc  : docs.python.org
path    : /zh-cn/3/library/urllib.parse.html
params  :
query   :
fragment: module-urllib.parse
hostname: docs.python.org
以上还可以通过索引获取,如通过
	
		
			| 
			 1 
			2 
			3 
			 | 
			
			 print(o[0]) 
			... 
			print(o[5]) 
			 | 
		
	
urlunparse()
urlunparse()可以实现URL的构造。(构造URL)
urlunparse()接收一个是一个长度为6的可迭代对象,将URL的多个部分组合为一个URL。若可迭代对象长度不等于6,则抛出异常。
示例:
	
		
			| 
			 1 
			2 
			3 
			 | 
			
			 from urllib.parse import urlunparse 
			url_compos = ['http','www.baidu.com','index.html','user= test','a=6','comment'] 
			print(urlunparse(url_compos)) 
			 | 
		
	
output
http://www.baidu.com/index.html;user= test?a=6#comment
urlsplit()
urlsplit()函数也能对 URL进行拆分,所不同的是,urlsplit()并不会把 路径参数(params) 从 路径(path) 中分离出来。
当URL中路径部分包含多个参数时,使用urlparse()解析是有问题的,这时可以使用urlsplit()来解析.
urlunsplit()与urlunparse()类似,(构造URL),传入对象必须是可迭代对象,且长度必须是5。
示例:
	
		
			| 
			 1 
			2 
			3 
			 | 
			
			 from urllib.parse import urlunsplit 
			url_compos = ['http','www.baidu.com','index.html','user= test','a = 2'] 
			print(urlunsplit(url_compos))urlunsplit() 
			 | 
		
	
output
http://www.baidu.com/index.html?user= test#a = 2
urljoin()
同样可以构造URL。
传递一个基础链接,根据基础链接可以将某一个不完整的链接拼接为一个完整链接.
注:连接两个参数的url, 将第二个参数中缺的部分用第一个参数的补齐,如果第二个有完整的路径,则以第二个为主。
URL 转码
python中提供urllib.parse模块用来编码和解码,分别是urlencode()与unquote()。
编码quote(string)
URL转码函数的功能是接收程序数据并通过对特殊字符进行转码并正确编码非ASCII文本来将其转为可以安全地用作URL组成部分的形式。它们还支持逆转此操作以便从作为URL组成部分的内容中重建原始数据,如果上述的URL解析函数还未覆盖此功能的话
语法:
	
		
			| 
			 1 
			 | 
			
			 urllib.parse.quote(string, safe='/', encoding=None, errors=None) 
			 | 
		
	
使用%xx转义符替换string中的特殊字符。字母、数字和 '_.-~' 等字符一定不会被转码。在默认情况下,此函数只对URL的路径部分进行转码。可选的safe形参额外指定不应被转码的ASCII字符 --- 其默认值为 '/'。
string可以是str或bytes对象。
示例:
	
		
			| 
			 1 
			2 
			3 
			4 
			5 
			6 
			7 
			8 
			9 
			 | 
			
			 from urllib import parse 
			  
			url = "http://www.baidu.com/s?wd={}" 
			words = "爬虫" 
			  
			#quote()只能对字符串进行编码 
			query_string = parse.quote(words) 
			url = url.format(query_string) 
			print(url) 
			 | 
		
	
执行结果:
http://www.baidu.com/s?wd=%E7%88%AC%E8%99%AB
编码urlencode()
quote()只能对字符串编码,而urlencode()可以对查询字符串进行编码。
	
		
			| 
			 1 
			2 
			3 
			4 
			5 
			6 
			7 
			8 
			9 
			10 
			 | 
			
			 # 导入parse模块 
			from urllib import parse 
			  
			#调用parse模块的urlencode()进行编码 
			query_string = {'wd':'爬虫'} 
			result = parse.urlencode(query_string) 
			  
			# format函数格式化字符串,进行url拼接 
			url = 'http://www.baidu.com/s?{}'.format(result) 
			print(url) 
			 | 
		
	
output
http://www.baidu.com/s?wd=%E7%88%AC%E8%99%AB
解码 unquote(string)
解码就是对编码后的url进行还原。
示例:
	
		
			| 
			 1 
			2 
			3 
			4 
			 | 
			
			 from urllib import parse 
			string = '%E7%88%AC%E8%99%AB' 
			result = parse.unquote(string) 
			print(result) 
			 | 
		
	
执行结果:
爬虫
urllib.robotparser模块
(在网络爬虫中基本不会用到,使用较少,仅作了解)
urllib.robotparser用于解析robots.txt文件。
robots.txt(统一小写)是一种存放于网站根目录下的robots协议,它通常用于告诉搜索引擎对网站的抓取规则。
Robots协议也称作爬虫协议,机器人协议,网络爬虫排除协议,用来告诉爬虫哪些页面是可以爬取的,哪些页面是不可爬取的。它通常是一个robots.txt的文本文件,一般放在网站的根目录上。
当爬虫访问一个站点的时候,会首先检查这个站点目录是否存在robots.txt文件,如果存在,搜索爬虫会根据其中定义的爬取范围进行爬取。如果没有找到这个文件,搜索爬虫会访问所有可直接访问的页面。
urllib.robotparser提供了RobotFileParser类,语法如下:
	
		
			| 
			 1 
			 | 
			
			 class urllib.robotparser.RobotFileParser(url='') 
			 | 
		
	
这个类提供了一些可以读取、解析robots.txt文件的方法:
	- set_url(url) - 设置robots.txt文件的URL。
 
	- read() - 读取robots.txt URL并将其输入解析器。
 
	- parse(lines) - 解析行参数。
 
	- can_fetch(useragent, url) - 如果允许useragent按照被解析robots.txt文件中的规则来获取url则返回True。
 
	- mtime() - 返回最近一次获取robots.txt文件的时间。这适用于需要定期检查robots.txt文件更新情况的长时间运行的网页爬虫。
 
	- modified() - 将最近一次获取robots.txt文件的时间设置为当前时间。
 
	- crawl_delay(useragent) - 为指定的useragent从robots.txt 返回Crawl-delay形参。如果此形参不存在或不适用于指定的useragent 或者此形参的robots.txt条目存在语法错误,则返回None。
 
	- request_rate(useragent) -以 named tuple RequestRate(requests, seconds)的形式从robots.txt返回Request-rate形参的内容。如果此形参不存在或不适用于指定的useragent或者此形参的robots.txt条目存在语法错误,则返回None。
 
	- site_maps() - 以list()的形式从robots.txt返回Sitemap形参的内容。如果此形参不存在或者此形参的robots.txt条目存在语法错误,则返回None。