本篇博客,我们将详细的介绍Urllib库,虽然在实际使用中我们更常用request库,它更强大;但是requests库也是基于urllib的,所以我们需要对他做一些了解。
目录
1. 什么是Urllib
Python内置的HTTP请求库。包含四大模块:
- urllib.request 请求模块
- urllib.error 异常处理模块
- urllib.parse url解析模块
- urllib.robotparser robot.txt解析模块
相比Python2变化:
- python2
import urllib2
response = urllib2.urlopen('http://www.baidu.com')
- python3
import urllib.request
response = urllib.request.urlopen('http://www.baidu.com')
2. urlopen
urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)
urlopen在urllib.request模块中。第一个参数是请求url;第二个是要上传的数据 post请求;第三个timeout是超时时间。这三个参数比较常用,其他简单了解。
示例:
import urllib.request
#get请求
response = urllib.request.urlopen('http://www.baidu.com')
print(response.read().decode('utf-8')) #read()返回的是bytes类型 需要解码 百度首页源码
import urllib.parse
import urllib.request
#post请求(有data参数就是post请求 没有就是get) 上传data
#对字典数据进行编码 在转换为bytes
data = bytes(urllib.parse.urlencode({'word': 'hello'}), encoding='utf8')
response = urllib.request.urlopen('http://httpbin.org/post', data=data)
print(response.read())
import urllib.request
#设置超时时间 请求超过1s就会报错
response = urllib.request.urlopen('http://httpbin.org/get', timeout=1)
print(response.read())
import socket
import urllib.request
import urllib.error
try:
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')
3. 响应
- 响应类型
import urllib.request
response = urllib.request.urlopen('https://www.python.org')
print(type(response))
- 状态码、响应头
import urllib.request
response = urllib.request.urlopen('https://www.python.org')
print(response.status)
print(response.getheaders())
print(response.getheader('Server'))
import urllib.request
response = urllib.request.urlopen('https://www.python.org')
print(response.read().decode('utf-8'))
4. Request
import urllib.request
request = urllib.request.Request('https://python.org')#把url封装成request对象
response = urllib.request.urlopen(request) #发送request对象 不直接发送url
print(response.read().decode('utf-8'))
from urllib import request, parse
#可以使用request对象 添加headers
url = 'http://httpbin.org/post'
headers = {
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)',
'Host': 'httpbin.org'
}
dict = {
'name': 'Germey'
}
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, parse
url = 'http://httpbin.org/post'
dict = {
'name': 'Germey'
}
data = bytes(parse.urlencode(dict), encoding='utf8')
req = request.Request(url=url, data=data, method='POST')
#可以直接在request对象中添加headers 也可以在request对象中追加headers
#第一个参数是headers中的字段类型 第二个是字段值
req.add_header('User-Agent', 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')
response = request.urlopen(req)
print(response.read().decode('utf-8'))
5. Handler
- 代理
import urllib.request
#构建IP代理 handler
proxy_handler = urllib.request.ProxyHandler({
'http': 'http://127.0.0.1:9743',
'https': 'https://127.0.0.1:9743'
})
#构建opener
opener = urllib.request.build_opener(proxy_handler)
response = opener.open('http://httpbin.org/get') #该网址可以返回请求的IP地址
print(response.read()) #在爬取过程中利用代理handler可以不断更换ip 服务器认为是来自不同地方的请求 就不会被封
- Cookie
import http.cookiejar, urllib.request
#cookie包含登录信息 可以维持登录状态
#可以用cookie爬取一些需要登录验证的网站
cookie = 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)
import http.cookiejar, urllib.request
filename = "cookie.txt"
cookie = http.cookiejar.MozillaCookieJar(filename) #火狐浏览器cookie格式
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保存在cookie.txt(火狐浏览器格式)中
import http.cookiejar, urllib.request
filename = 'cookie.txt'
cookie = http.cookiejar.LWPCookieJar(filename) #LWP格式
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保存在cookie.txt(LWP格式)中
import http.cookiejar, urllib.request
cookie = http.cookiejar.LWPCookieJar()
#读取cookie
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'))
6. 异常处理
from urllib import request, error
try:
response = request.urlopen('http://cuiqingcai.com/index.htm')
except error.URLError as e: #except中可以添加重试或直接退出等操作
print(e.reason)
from urllib import request, error
#HTTPerror是子类 URLerroe是父类
#写异常处理时 最好先写子类 再写父类
try:
response = request.urlopen('http://cuiqingcai.com/index.htm')
except error.HTTPError as e: #子类有三个属性
print(e.reason, e.code, e.headers, sep='\n')
except error.URLError as e: #父类只有一个
print(e.reason)
else:
print('Request Successfully')
import socket
import urllib.request
import urllib.error
try:
response = urllib.request.urlopen('https://www.baidu.com', timeout=0.01)
except urllib.error.URLError as e:
print(type(e.reason)) #打印原因类型
if isinstance(e.reason, socket.timeout): #判断类型是否匹配
print('TIME OUT')
7. URL解析
- urlparse
urllib.parse.urlparse(urlstring, scheme='', allow_fragments=True)
from urllib.parse import urlparse
#解析url 协议、网址(//后)、路径(/后)、参数(;后)、query(?后)、fragment(#后)
result = urlparse('http://www.baidu.com/index.html;user?id=5#comment')
print(type(result), result)
from urllib.parse import urlparse
#scheme可以添加协议类型 如果url没有的话
result = urlparse('www.baidu.com/index.html;user?id=5#comment', scheme='https')
print(result)
from urllib.parse import urlparse
#如果url有协议类型 scheme参数无效
result = urlparse('http://www.baidu.com/index.html;user?id=5#comment', scheme='https')
print(result)
from urllib.parse import urlparse
#fragment如果为false url解析结果中fragment就会为空 如果url中有fragment 就会拼接到前面的query
result = urlparse('http://www.baidu.com/index.html;user?id=5#comment', allow_fragments=False)
print(result)
from urllib.parse import urlparse
#如果没有query 就会继续向前拼接
result = urlparse('http://www.baidu.com/index.html#comment', allow_fragments=False)
print(result)
- urlunparse
from urllib.parse import urlunparse
#拼接url
data = ['http', 'www.baidu.com', 'index.html', 'user', 'a=6', 'comment']
print(urlunparse(data))
- urljoin
from urllib.parse import urljoin
#拼接两个url
#以后面的url为基准 如果有就按后面 没有就用前面的补充
print(urljoin('http://www.baidu.com', 'FAQ.html'))
print(urljoin('http://www.baidu.com', 'https://cuiqingcai.com/FAQ.html'))
print(urljoin('http://www.baidu.com/about.html', 'https://cuiqingcai.com/FAQ.html'))
print(urljoin('http://www.baidu.com/about.html', 'https://cuiqingcai.com/FAQ.html?question=2'))
print(urljoin('http://www.baidu.com?wd=abc', 'https://cuiqingcai.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'))
- urlencode
from urllib.parse import urlencode
params = {
'name': 'germey',
'age': 22
}
base_url = 'http://www.baidu.com?'
url = base_url + urlencode(params) #可以把字典转化为请求参数
print(url)