import requests
import re
import os
#如果目录不存在,那么创建目录,反之不创建
if not os.path.exists(’./qiutuLibs’):
os.mkdir('./qiutuLibs')
url = ‘https://www.qiushibaike.com/imgrank/’
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
}
#解析页面以二进制的形式
page_text = requests.get(url=url,headers=headers).text
#利用正则定位出图片url地址,以便于下载
ex = ‘
.
?<img src="(.?)" alt.*?
’
img_src_list = re.findall(ex,page_text,re.S)
for src in img_src_list:
#拼接出一个完整的链接
src = 'https:'+src
#请求到图片的二进制数据
img_data = requests.get(url=src,headers=headers).content
#给你的图片命名
img_name = src.split('/')[-1]
#图片路径设置
imgPath = './qiutuLibs/'+img_name
#下载图片
with open(imgPath,'wb') as fp:
fp.write(img_data)
print(img_name,'SUCCESSFUL!!!!!!!!!!!!!!')
[]( )3、爬取多页面数据
------------------------------------------------------------------------
import requests
import re
import os
if not os.path.exists(’./qiutuLibs’):
os.mkdir('./qiutuLibs')
#设置一个通用的url模板即可实现多页爬取
url = ‘https://www.qiushibaike.com/imgrank/page/%d’
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
}
#利用for循环,循环出页码,针对每页进行解析下载
#类似于打印乘法口诀那样
for pageNum in range(1,3):
new_url = format(url%pageNum)
page_text = requests.get(url=new_url,headers=headers).text
ex = '<div class="thumb">.*?<img src="(.*?)" alt.*?</div>'
img_src_list = re.findall(ex,page_text,re.S)
# print(img_src_list)
for src in img_src_list:
#拼接出一个完整的链接
src = 'https:'+src
#请求到图片的二进制数据
img_data = requests.get(url=src,headers=headers).content
img_name = src.split('/')[-1]
imgPath = './qiutuLibs/'+img_name
with open(imgPath,'wb') as fp:
fp.write(img_data)
print(img_name,'SUCCESSFUL!!!!!!!!!!!!!!')
[]( )4、贪婪模式和非贪婪模式
---------------------------------------------------------------------------
### 最后
> **笔者已经把面试题和答案整理成了面试专题文档**
>
> **[CodeChina开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频】](https://codechina.csdn.net/m0_60958482/java-p7)**
![image](https://img-blog.csdnimg.cn/img_convert/24665a3f512164a3f895b0e4f4726b90.png)
![image](https://img-blog.csdnimg.cn/img_convert/d17764a544b93f81a76f27a12d7b603d.png)
![image](https://img-blog.csdnimg.cn/img_convert/047f1898e0b3432ffaabe55912dda465.png)
![image](https://img-blog.csdnimg.cn/img_convert/90e4b9e81ed9f702356d96a16d1e575b.png)
![image](https://img-blog.csdnimg.cn/img_convert/9774a4f7ca0f235e3ac51c5afdfa2452.png)
)]
[外链图片转存中...(img-YwQXll1E-1630929738692)]
[外链图片转存中...(img-sASLdr5a-1630929738693)]
[外链图片转存中...(img-cTQYk5OQ-1630929738695)]
[外链图片转存中...(img-xLfvGMt6-1630929738696)]
![image](https://img-blog.csdnimg.cn/img_convert/8a02592cd22916e3e750d045a5ee6d00.png)