爬虫基本请求库的使用(一) urllib

urllib(内置库):

requests: 最基本的请求模块 仅需要传入URL和额外的参数
error: 异常处理模块
parse: URL的工具模块
robotparser: 识别网站robot.txt文件, 判断网站是否可爬

urlopen():

import urllib.request

response = urllib.request.urlopen('https://www.python.org')

# print(response.read().decode('utf-8'))
# 查看response是什么类型的数据
# print(type(response))

# 响应状态码
# print(response.status)
# # 响应头
# print(response.getheaders())
# # 响应头的某一个具体信息
# print(response.getheader('Content-Type'))

data参数:
传入以后,请求方式改变

import urllib.request
import urllib.parse

data = bytes(urllib.parse.urlencode({'word':'hello'}), encoding='utf-8')
response =  urllib.request.urlopen('http://httpbin.org/post', data = data)
print(response.read())

timeout参数:
超过时间没有响应,抛出异常

import urllib.request


response =  urllib.request.urlopen('http://httpbin.org/get', timeout=0.1)
print(response.read())
import urllib.request
import socket
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')

其他参数:
context等

Request(更强大的urllib库,可以传入请求头):
urlopen()方法可以实现最基本请求的发起,但这几个简单的参数并不足以构建一
个完整的请求(例如不可以传入请求头)
第一个参数url
第二个参数data
第三个参数hearders是字典
第四个参数origin_req_host 请求方ip
第五个参数unverifiable 请求验证
第六个参数method 请求方法

高级用法:
HITPDefaultErrorHandler: 处理HTTP响应错误
HTTPRedirectHandler: 处理重定向
HTTPCookieProcessor: 处理Cookies
ProxyHandler: 设置代理 默认为空
HTTPPasswordMgr: 用于管理密码
HTTPBasicAuthHandler: 管理认证
Opener可以实现更高级的功能

输入登录密码的时候

from urllib.request import HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, build_opener
from urllib.error import URLError

username = 'username'
password = 'password'
url = 'http://localhost:5000/'

p = HTTPPasswordMgrWithDefaultRealm()
p.add_password(None, url, username, password)
auth_handler = HTTPBasicAuthHandler(p)
opener = build_opener(auth_handler)

try:
    result = opener.open(url)
    html = result.read().decode('utf-8')
    print(html)
except URLError as e:
    print(e.reason)

代理:

from urllib.error import URLError
# ProxyHandler设置代理 build_opener构造代理
from urllib.request import ProxyHandler, build_opener

proxy_hander = ProxyHandler({
    'http': 'http://127.0.0.1:9743',
    'https': 'https://127.0.0.1:9743'
})
opener = build_opener(proxy_hander)
try:
    response = opener.open('https://www.baidu.com')
    print(response.read().decode('utf-8'))
except URLError as e:
    print(e.reason)

Cookies:

获取网站Cookies

import http.cookiejar, urllib.request

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

将Cookies保存为TXT格式文件

import http.cookiejar, urllib.request

filename = 'cookies.txt'
# 这里是将Cookies保存为txt格式的文件
cookie = http.cookiejar.MozillaCookieJar(filename)
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
reponse = opener.open('http://www.baidu.com')
cookie.save(ignore_discard=True, ignore_expires=True)

保存成LWP格式的Cookies文件

# cookie = http.cookiejar.LWPCookieJar(filename)

处理异常:
URLError:
urllib库的error模块 继承自OSError类

from urllib import request, error

try:
    response = request.urlopen('https://cuiqingcai.com/index.htm')
except error.HTTPError as e:
    # sep据估计是输出一个 则打一个换行符
    print(e.reason, e.code, e.headers, sep='\n')
except error.URLError as e:
    print(e.reason)
else:
    print('Requests Successfully')
HTTPError:
	URLError的子类
	code: 返回的HTTP状态码
	reason: 返回错误原因
	headers: 返回请求头
	'''
	from urllib import request, error

	try:
	    response = request.urlopen('https://cuiqingcai.com/index.htm')
	except error.HTTPError as e:
	    # sep据估计是输出一个 则打一个换行符
	    print(e.reason, e.code, e.headers, sep='\n')
	'''

	优化(很好的写法)
from urllib import request, error

try:
    response = request.urlopen('https://cuiqingcai.com/index.htm')
except error.HTTPError as e:
    # sep据估计是输出一个 则打一个换行符
    print(e.reason, e.code, e.headers, sep='\n')
except error.URLError as e:
    print(e.reason)
else:
    print('Requests Successfully')

特例:
reason属性的结果是socket.timeout类

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(e.reason)
    if isinstance(e.reason, socket.timeout):
        print('TIME OUT')

解析链接:
urllib库提供了parse模块,实现URL各部分的抽取等功能
urlparse():

from urllib.parse import urlparse

result = urlparse('http://www.baidu.com/index.html;user?id=5#comment')
print(type(result), result)

secheme: 协议
netloc: 域名
path: 访问路径
params: 参数
query: 查询条件
标准链接格式(scheme://netloc/path ;params?query#fragment)
urlstring: 待解析的URL
scheme: 默认协议(默认为HTTP/HTTPS)
allow_fragments: 是否忽略fragment
获取到的数据实际是一个元祖

urlunparse():
对应urlparse(),长度必须为6
url构造方法

from urllib.parse import urlunparse

data = ['http', 'www.baidu.com', 'index.html', 'user', 'a=6', 'comment']
print(urlunparse(data))

urlsplit():
类似于urlparse()
也是元祖类型
可以用属性,也可以用索引来获取

from urllib.parse import urlsplit

result = urlsplit('http://www.baidu.com/index.html;user?a=6#comment')
print(result)
print(result.scheme, result[0])

unlunsplit():
类似于urlunparse()
将链接各个部分组成完整的链接
唯一区别就是传入长度必须为5

from urllib.parse import urlunsplit

data = ['http', 'www.baidu.com', 'index.html', 'a=6', 'comment']
print(urlunsplit(data))

urljoin():
提供一个基础链接(第一个参数)
提供新的链接(第二个参数)
该方法会自动分析两个链接的差异
组成一个新的缺失部分

# urljoin() 也是合并链接的一种方式,相比之前俩种方法,没有之前特定的指定参数对象的长度  
# urljoin()方法提供俩个参数,base_url(基础链接)作为第一个参数,讲新的链接作为第二个参数,该方法会分析base_url的scheme,netloc和path  
# 这三个内容并对新链接确实的部分进行补充,最后返回结果  
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','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)

parse_qs():
反序列化
字典类型

from urllib.parse import parse_qs

query = 'name=germey&age=22'
print(parse_qs(query))

parse_qsl():
反序列化
元祖类型

from urllib.parse import parse_qsl

query = 'name=germey&age=22'
print(parse_qsl(query))

quote():
将内容转化为URL编码的格式
中文的时候可能导致乱码

from urllib.parse import quote

keyword = '壁纸'
url = 'https://www.baidu.com/s?wd=' + quote(keyword)

print(url)

unquote():
对URL进行解码

from urllib.parse import unquote

url = 'https://www.baidu.com/s?wd=%E5%A3%81%E7%BA%B8'
print(unquote(url))

Robots协议:
爬虫协议
哪些页面可以抓取
哪些页面不可以抓取
详情参考: 《python3网络爬虫实战开发》3.1.4节

加油

猜你喜欢

转载自blog.csdn.net/qq_40258748/article/details/89321471