爬虫基本原理及urllib库的基本使用
爬虫的基本原理
爬虫定义:请求网站并提取数据的自动化程序
1.能按做者要求下载数据或内容
2.能自动在网络上流窜
爬虫的分类
1.通用爬虫 (不分类)
2.专用爬虫(聚焦爬虫)(主讲)
基本流程
1.发起请求 HTTP Request 请求方式:GET POST
2.获取相应内容 HTTP Response
3.解析内容
4.保存数据
Request
请求方式:GET POST
请求URL:全球统一的资源定位符 一个网页文档 一张图片 一个视频 均可用URL惟一来肯定
请求头:头部信息 such as:User-Agent Host Cookies
请求体:请求时额外携带的数据
Response
状态码:(Status):200(成功) 301(跳转) 404(找不到页面) 502(服务器错误)
响应头:内容类型 内容长度 服务器信息 设置Cookie......
响应体:最主要的部分 包含了请求资源的内容 such as: 网页HTML 图片二进制数据......
能抓取怎样的数据
1.网页文本:HTML文档 Json格式文本
2.图片:获取到的是二进制文件 保存为图片格式
3.视频:同为二进制文件 保存为视频格式便可
4.其余
解析方式
1.直接处理
2.Json解析(Json格式)
3.正则表达式
4.BeautifulSoup
5.XPath
6.PyQuery
Urllib库
Urllib库是Python自带的一个HTTP请求库,包含如下几个模块:
urllib.request 请求模块
urllib.error 异常处理模块
urllib.parse url解析模块
urllib.robotparser robots.txt解析模块
复制代码
import urllib.request
response = urllib.request.urlopen('http://www.baidu.com')
print(response.read().decode('utf-8'))
import urllib.parse
import urllib.request
d = byte(urllib.parse.urlencode({'name':'kobe'}), encoding = 'utf-8')
response = urllib.response.urlopen('http://www.baidu.com',data = d)
print(response.read().decode('utf-8'))
import socket
import urllib.request
try:
response = urllib.request.urlopen('http://www.baidu.com',timeout = o.o1)
except urllib.error.UELError as e:
if isinstance(e.reason,socket.timeout)
print('Time Out')
import urllib.request
response = urllib.request.urlopen('http://www.baidu.com')
print(response.status)
print(response.getHeaders())
print(response.getHeader('Server'))
import urllib.request
import urllib.parse
url = 'http://httpbin.org/post'
headers = {
'User-Agent':'......'
'Host':'......'
}
dict = {'name':'kobe'}
data = bytes(parse.uelopen(dict),encoding = post)
req = request.Request(url = url,data =data,headers = deaders,method = post)
response = request.urlopen(req)
print(response.read().decode('utf-8'))
import urllib.request
import urllib.error
try:
response = request.urlopen('http://www.baidu.com')
except error.HTTPError as e:
print(e.reason,e.code,e.header,sep = '\n')
except error.URLError as e:
print(e.reason)
else:
print('Request Successfully!')
from urllib.parse import urlparse
result = urlparse('http://www.baidu.com/index.html;user?id=5#comment',scheme='https')
print(result)
from urllib.parse import urlunparse
data = ['http','www.baidu.com','index.html','user','a=6','comment']
print(urlunparse(data))
from urllib.parse import urljoin
print(urljoin('http://www.baidu.com','index.html'))
print(urljoin('http://www.baidu.com#comment','?username="zhangsan"'))
print(urljoin('http://www.baidu.com','www.sohu.com'))
from urllib.parse import urlencode
params = {
'name':'zhangsan',
'age':22
}
base_url = 'http://www.baidu.com?'
url = base_url + urlencode(params)
print(url)
复制代码
复制代码