Python 爬虫基础学习(tow)
1.URLError\HTTPError
简介:1.HTTPError类是URLError类的子类
2.导入的包urllib.error.HTTPError urllib.error.URLError
3.http错误:http错误是针对浏览器无法连接到服务器而增加出来的错误提示。引导并告诉浏览者该页是哪里出了问题。
4.通过urllib发送请求的时候,有可能会发送失败,这个时候如果想让你的代码更加的健壮,可以通过try-except进行捕获异常,异常有两类,URLError\HTTPError
eg:
import urllib.request
import urllib.error
url = 'http://www.fendoudezhuozi.com'
headers = {
'User - Agent': 'Mozilla / 5.0(Windows NT 10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 74.0.3729.169Safari / 537.36'
}
request = urllib.request.Request(url=url,headers=headers)
try:
response = urllib.request.urlopen(request)
content = response.read()
print(content)
except urllib.error.HTTPError as e:
print(e)
except urllib.error.URLError as e:
print(e)
2.cookie登录
使用案例:
1.人人网登陆
2.weibo登陆
3.Handler处理器
为什么要学习handler?
urllib.request.urlopen(url)
不能定制请求头
urllib.request.Request(url,headers,data)
可以定制请求头
Handler
定制更高级的请求头(随着业务逻辑的复杂 请求对象的定制已经满足不了我们的需求(动态cookie和代理不能使用请求对象的定制)
eg:
import urllib.request
url = 'http://www.baidu.com'
headers = {
'User - Agent': 'Mozilla / 5.0(Windows NT 10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 74.0.3729.169Safari / 537.36'
}
request = urllib.request.Request(url=url,headers=headers)
handler = urllib.request.HTTPHandler()
opener = urllib.request.build_opener(handler)
response = opener.open(request)
print(response.read().decode('utf-8'))
4.代理服务器
1.什么是代理服务器?
是一种重要的服务器安全功能,它的工作主要在开放系统互联(OSI)模型的会话层,从而起到防火墙的作用
翻墙,是指绕过相应的IP封锁、内容过滤、域名劫持、流量限制等
2.代理的常用功能?
1.突破自身IP访问限制,访问国外站点。
2.访问一些单位或团体内部资源
扩展:某大学FTP(前提是该代理地址在该资源的允许访问范围之内),使用教育网内地址段免费代理服务器,就可以用于对教育网开放的各类FTP下载上传,以及各类资料查询共享等服务。
3.提高访问速度
扩展:通常代理服务器都设置一个较大的硬盘缓冲区,当有外界的信息通过时,同时也将其保存到缓冲区中,当其他用户再访问相同的信息时, 则直接由缓冲区中取出信息,传给用户,以提高访问速度。
4.隐藏真实IP
扩展:上网者也可以通过这种方法隐藏自己的IP,免受攻击。
3.代码配置代理
创建Reuqest对象
创建ProxyHandler对象
用handler对象创建opener对象
使用opener.open函数发送请求
eg:
import urllib.request
url = 'http://www.baidu.com/s?wd=ip'
headers = {
'User - Agent': 'Mozilla / 5.0(Windows NT 10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 74.0.3729.169Safari / 537.36'
}
request = urllib.request.Request(url=url,headers=headers)
proxies = {'http':'117.141.155.244:53281'}
handler = urllib.request.ProxyHandler(proxies=proxies)
opener = urllib.request.build_opener(handler)
response = opener.open(request)
content = response.read().decode('utf-8')
with open('daili.html','w',encoding='utf-8')as fp:
fp.write(content)
扩展:1.代理池
#将代理的地址放在列表中
proxy_pool = [
{'http':'122.114.112.242:16816'},
{'http':'222.223.162.133:3128'}
]
proxies = random.choice(proxy_pool)
request = urllib.request.Request(url=url,headers=headers)
#注意handler等号的右边是ProxyHandler(proxies=proxies),
#不再是HTTPHandler()
handler = urllib.request.ProxyHandler(proxies=proxies)
2.快代理
import urllib.request
import zlib
import ssl
# 全局取消证书验证,避免访问https网页报错
ssl._create_default_https_context = ssl._create_unverified_context
"""使用urllib.request模块请求代理服务器,http和https网页均适用"""
# http://kps.kdlapi.com/api/getkps/?orderid=916567331800728&num=1&pt=1&sep=1
kuaidali_url = 'http://kps.kdlapi.com/api/getkps/?orderid=916567331800728&num=1&pt=1&sep=1'
response_kuaidaili = urllib.request.urlopen(url=kuaidali_url)
# 方法返回的是ip+端口号
content_kuaidaili = response_kuaidaili.read().decode('utf-8')
print(content_kuaidaili)
# 要访问的目标网页
page_url = "http://www.baidu.com/s?wd=ip"
# 代理服务器
proxy = content_kuaidaili
# 用户名和密码(私密代理/独享代理)
username = "用户名"
password = "密码"
proxies = {
"http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {'user': username, 'pwd': password, 'proxy': proxy},
"https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {'user': username, 'pwd': password, 'proxy': proxy}
}
headers = {
"Accept-Encoding": "Gzip", # 使用gzip压缩传输数据让访问更快
}
proxy_hander = urllib.request.ProxyHandler(proxies)
opener = urllib.request.build_opener(proxy_hander)
req = urllib.request.Request(url=page_url, headers=headers)
result = opener.open(req)
print(result.status) # 获取Response的返回码
content_encoding = result.headers.get('Content-Encoding')
if content_encoding and "gzip" in content_encoding:
# 获取页面内容
content = zlib.decompress(result.read(), 16 + zlib.MAX_WBITS).decode('utf-8')
else:
# 获取页面内容
content = result.read().decode('utf-8')
with open('kuaidaili.html','w',encoding='utf-8')as fp:
fp.write(content)
5.cookie库
抓取登录接口:
(1)network : preserve log打勾
(2).(1)的打勾输入用户名后,随便输入密码,跳转获取请求
post请求接口看参数
定制更高级的请求handler
1.cookie库能干啥:通过handler登陆会自动的保存登录之后的cookie
2.cookie库配置
创建一个CookieJar对象
使用cookiejar对象,创建一个handler对象
使用handler创建一个opener
通过opener登录
handler会自动的保存登录之后的cookie
案例:全书网
# 登陆之后进入到藏书架
import urllib.request
import urllib.parse
import http.cookiejar
url_login = 'http://www.quanshuwang.com/login.php?do=submit'
headers = {
'User - Agent': 'Mozilla / 5.0(Windows NT 10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 74.0.3729.169Safari / 537.36'
}
data = {
'username': 'action',
'password': 'action',
'action': 'login',
}
# 对参数进行编码
data = urllib.parse.urlencode(data).encode('utf-8')
# 请求对象的定制
request = urllib.request.Request(url=url_login,headers=headers,data=data)
# 获取一个cookiejar对象
cookiejar = http.cookiejar.CookieJar()
# 获取一个handler对象
handler = urllib.request.HTTPCookieProcessor(cookiejar=cookiejar)
# 获取opener对象
opener = urllib.request.build_opener(handler)
# 获取response对象
response = opener.open(request)
url_bookcase = 'http://www.quanshuwang.com/modules/article/bookcase.php'
request_bookcase = urllib.request.Request(url=url_bookcase,headers=headers)
# 因为之前的opener中 已经包含了登陆的cookie 那么再次使用opener 就会携带已经存在cookie
# 去访问了
response_bookcase = opener.open(request_bookcase)
content = response_bookcase.read().decode('gbk')
with open('bookcase.html','w',encoding='gbk')as fp:
fp.write(content)
6.正则表达式回顾
单字修饰符:
1. . 匹配任意字符,除了换行符
2. [] 用来表示一组字符,单独列出:[abc] 匹配 'a','b'或'c'
3. \d 匹配任意数字,等价于 [0-9].
4. \D 匹配任意非数字
5. \w 匹配字母数字及下划线
6. \W 匹配非字母数字及下划线
7. \s 匹配任意空白字符,等价于 [\t\n\r\f].
8. \S 匹配任意非空字符
数量修饰符
1.* 匹配0个或多个的表达式
2.+ 匹配1个或多个的表达式
3.? 匹配0个或1个由前面的正则表达式定义的片段
4.{m} 前面字符出现m次
5.{m,} 前面字符出现至少m次
6.{m,n}前面字符出现m~n次
边界修饰符
1.^ 以...开始
2.$ 以...结尾
eg:
'^abc' 匹配以abc开头
‘abc$' 匹配以abc结尾
分组修饰符
1.() 匹配括号内的表达式,也表示一个组
2.\1 \2 匹配第1、2个分组的内容
eg:
(.*):(.*)
\1 \2
贪婪模式/非贪婪模式
贪婪模式:在整个表达式匹配成功的前提下,尽可能多的匹配 ( * );
非贪婪模式:在整个表达式匹配成功的前提下,尽可能少的匹配 ( ? );
Python里数量词默认是贪婪的。
示例一 : 源字符串:abbbc
使用贪婪的数量词的正则表达式 ab* ,匹配结果: abbb。
* 决定了尽可能多匹配 b,所以a后面所有的 b 都出现了。
使用非贪婪的数量词的正则表达式ab*?,匹配结果: a。
即使前面有 *,但是 ? 决定了尽可能少匹配 b,所以没有 b。
示例二 : 源字符串:aa<div>test1</div>bb<div>test2</div>cc
使用贪婪的数量词的正则表达式:<div>.*</div>
匹配结果:<div>test1</div>bb<div>test2</div>
这里采用的是贪婪模式。在匹配到第一个“</div>”时已经可以使整个表达
式匹配成功,但是由于采用的是贪婪模式,所以仍然要向右尝试匹配,
查看是否还有更长的可以成功匹配的子串。匹配到第二个“</div>”后,
向右再没有可以成功匹配的子串,匹配结束,匹配结果为
“<div>test1</div>bb<div>test2</div>”
使用非贪婪的数量词的正则表达式:<div>.*?</div>
匹配结果:<div>test1</div>
正则表达式二采用的是非贪婪模式,在匹配到第一个“</div>”
时使整个表达式匹配成功,由于采用的是非贪婪模式,
所以结束匹配,不再向右尝试,匹配结果为“<div>test1</div>”。
模式修饰符
regular expression
1.re.S 单行模式
2.re.M 多行模式
3.re.I 忽略大小写
使用步骤:
re.complie()
compile 函数用于编译正则表达式,生成一个正则表达式Pattern对象
Pattern对象 = re.compile(正则表达式)
Pattern对象》find all(html)
findall()
在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表。
案例:糗事百科
1.使用正则表达式 下载糗事百科 24小时中 前10的所有的图片
图片的下载:#urllib.request.urlretrieve(url=url,filename='tan.jpg')
retrieve : 检索数据
创建request对象
获取网页源码
解析图片的路径
下载图片
# 1 创建request对象
# 2 获取网页源码
# 3 解析出图片的路径
# 4 下载图片
import urllib.request
import re
# 1 创建request对象
def create_request(page):
url = 'https://www.qiushibaike.com/hot/page/' + str(page) + '/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
}
request = urllib.request.Request(url=url,headers=headers)
return request
# 2 获取网页源码
def get_content(request):
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
return content
# 3 解析出图片的路径
# <div class="thumb">
# <a href="/article/122119754" target="_blank">
# <img src="//pic.qiushibaike.com/system/pictures/12211/122119754/medium/S18OUY90ZVAU3WSK.jpg" alt="糗事#122119754" class="illustration" width="100%" height="auto">
# </a>
# </div>
def parse_srclist(content):
pattern = re.compile('<div class="thumb">.*?<img src="(.*?)" alt=".*?"',re.S)
src_list = pattern.findall(content)
return src_list
# 4 下载图片
def down_load(src_list):
for src in src_list:
name = src.split('/')[-1]
filename = './qiubai/' + name
url = 'https:' + src
urllib.request.urlretrieve(url=url,filename=filename)
if __name__ == '__main__':
start_page = int(input('请输入起始页码'))
end_page = int(input('请输入结束页码'))
for page in range(start_page,end_page+1):
request = create_request(page)
content = get_content(request)
src_list = parse_srclist(content)
down_load(src_list)
- 站长素材 下载所有的黑人图片
# 1 创建request对象
# 2 获取网页源码
# 3 解析出图片的路径
# 4 下载图片
import urllib.request
import re
# 1 创建request对象
def create_request(page):
url = 'https://www.qiushibaike.com/hot/page/' + str(page) + '/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
}
request = urllib.request.Request(url=url,headers=headers)
return request
# 2 获取网页源码
def get_content(request):
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
return content
# 3 解析出图片的路径
# <div class="thumb">
# <a href="/article/122119754" target="_blank">
# <img src="//pic.qiushibaike.com/system/pictures/12211/122119754/medium/S18OUY90ZVAU3WSK.jpg" alt="糗事#122119754" class="illustration" width="100%" height="auto">
# </a>
# </div>
def parse_srclist(content):
pattern = re.compile('<div class="thumb">.*?<img src="(.*?)" alt=".*?"',re.S)
src_list = pattern.findall(content)
return src_list
# 4 下载图片
def down_load(src_list):
for src in src_list:
name = src.split('/')[-1]
filename = './qiubai/' + name
url = 'https:' + src
urllib.request.urlretrieve(url=url,filename=filename)
if __name__ == '__main__':
start_page = int(input('请输入起始页码'))
end_page = int(input('请输入结束页码'))
for page in range(start_page,end_page+1):
request = create_request(page)
content = get_content(request)
src_list = parse_srclist(content)
down_load(src_list)
7.xpath
xpath基本语法:
1.路径查询
//:查找所有子孙节点,不考虑层级关系
/:找直接子节点(第一级子标签)
2.谓词查询
//div[@id]
//div[@id="maincontent"]
3.属性查询
//@class
4.模糊查询
#id下含有he的标签
//div[contains(@id, "he")]
//div[starts-with(@id, "he")]
5.内容查询
//div/h1/text()
6.逻辑运算
//div[@id="head" and @class="s_down"]
//title | //price
xpath使用:
注意:提前安装xpath插件
1.安装lxml库
pip install lxml -i https://pypi.douban.com/simple
2.导入lxml.etree
from lxml import etree
3.etree.parse() 解析本地文件
html_tree = etree.parse('XX.html')
4.etree.HTML() 服务器响应文件
html_tree = etree.HTML(response.read().decode('utf-8')
4.html_tree.xpath(xpath路径)
etree.HTML 和etree.tostring的区别
etree.HTML
调用HTML类对HTML文本进行初始化,成功构造XPath解析对象,同时可以自动修正HMTL文本(标签缺少闭合自动添加上)
etree.tostring
tostring( )方法可以输出修正之后的HTML代码,也可以直接读取文本进行解析,但是结果为bytes类型,因此需要利用decode()方法将其转成str类型
应用案例:1.使用xpath解析糗事百科页面中所有的图片路径 --》xpath和正则的对比
2.使用xpath解析糗事百科的用户信息(用户头像,用户名字,用户等级)–》用户信息不对等
for i in range(len(src_list)):
src = src_list[i]
# print(src)
name = name_list[i]
if src =='//static.qiushibaike.com/images/thumb/anony.png?v=b61e7f5162d14b7c0d5f419cd6649c87':
age_list.insert(i,'')
print('===========',age_list)
age = age_list[i]
user = {}
user['src'] = src
user['name'] = name
user['age'] = age
users.append(user)
with open('user.text2222222222','w',encoding='utf-8') as fp:
fp.write(str(users))
3.站长素材图片抓取并且下载(http://sc.chinaz.com/tupian/shuaigetupian.html)–》懒加载
既然懒加载的原理是基于判断元素是否出现在窗口可视范围内,图片在可视范围内就加载,不在可视范围内就先不加载
src_list = tree.xpath('//div[@id="container"]/div/div/a/img/@src2')