Python爬虫入门案例之回车桌面壁纸网美女图片采集
作者:松鼠爱吃饼干 时间:2022-12-25 19:40:57
知识点
requests
parsel
re
os
环境
python3.8
pycharm2021
目标网址:
https://mm.enterdesk.com/bizhi/63899-347866.html
【付费VIP完整版】只要看了就能学会的教程,80集Python基础入门视频教学
点这里即可免费在线观看
注意: 在我们查看网页源代码的时候 (1. 控制台为准 2. 以右键查看网页源代码 3. 元素面板)
发送网络请求
获取网页源代码
提取想要的图片链接 css样式提取 xpath re正则表达式 bs4
替换所有的图片链接 换成大图
保存图片
爬虫代码
导入模块
import requests # 第三方库 pip install requests
import parsel # 第三方库 pip install parsel
import os # 新建文件夹
发送网络请求
response = requests.get('https://mm.enterdesk.com/bizhi/64011-348522.html')
获取网页源代码
data_html = response_1.text
提取每个相册的详情页链接地址
selector_1 = parsel.Selector(data_html)
photo_url_list = selector_1.css('.egeli_pic_dl dd a::attr(href)').getall()
title_list = selector_1.css('.egeli_pic_dl dd a img::attr(title)').getall()
for photo_url, title in zip(photo_url_list, title_list):
print(f'*****************正在爬取{title}*****************')
response = requests.get(photo_url)
# <Response [200]>: 请求成功的标识
selector = parsel.Selector(response.text)
# 提取想要的图片链接[第一个链接, 第二个链接,....]
img_src_list = selector.css('.swiper-wrapper a img::attr(src)').getall()
# 新建一个文件夹
if not os.path.exists('img/' + title):
os.mkdir('img/' + title)
替换所有的图片链接 换成大图
for img_src in img_src_list:
# 字符串的替换
img_url = img_src.replace('_360_360', '_source')
保存图片 图片名字
# 图片 音频 视频 二进制数据content
img_data = requests.get(img_url).content
# 图片名称 字符串分割
# 分割完之后 会给我们返回一个列表
img_title = img_url.split('/')[-1]
with open(f'img/{title}/{img_title}', mode='wb') as f:
f.write(img_data)
print(img_title, '保存成功!!!')
翻页
page_html = requests.get('https://mm.enterdesk.com/').text
counts = parsel.Selector(page_html).css('.wrap.no_a::attr(href)').get().split('/')[-1].split('.')[0]
for page in range(1, int(counts) + 1):
print(f'------------------------------------正在爬取第{page}页------------------------------------')
发送网络请求
response_1 = requests.get(f'https://mm.enterdesk.com/{page}.html')
爬取结果
来源:https://pythonjx.blog.csdn.net/article/details/120567725
标签:Python,图片采集,案例
0
投稿
猜你喜欢
Python参数解析器configparser简介
2021-04-22 02:23:31
go语言中值类型和指针类型的深入理解
2024-04-28 09:18:29
python中unittest框架应用详解
2023-05-26 14:27:42
利用python的socket发送http(s)请求方法示例
2022-06-06 08:33:39
python 爬取英雄联盟皮肤并下载的示例
2023-07-22 09:57:45
jsp/javascript打印九九乘法表代码
2024-03-23 02:23:17
XML卷之实战锦囊(2):动态查询
2008-09-05 17:20:00
python和shell监控linux服务器的详细代码
2021-11-09 06:23:22
使用Python在Windows下获取USB PID&VID的方法
2021-02-15 19:31:20
jquery ajax传递中文参数乱码问题及解决方法说明
2024-04-22 22:21:54
在asp中调用sql server的存储过程方法
2007-08-13 13:28:00
mysql5.5.28安装教程 超详细!
2024-01-15 16:02:05
一文带你了解Python闭包的基本用法
2022-01-01 19:54:25
更改Ubuntu默认python版本的两种方法python-> Anaconda
2021-07-30 15:33:28
效率高的Javscript字符串替换函数的benchmark
2024-04-22 12:47:39
python基于paramiko库远程执行 SSH 命令,实现 sftp 下载文件
2022-11-09 23:31:31
Mysql 忘记root密码的完美解决方法
2024-01-18 14:31:05
Python关于OS文件目录处理的实例分享
2022-12-29 08:52:07
Python中使用logging模块代替print(logging简明指南)
2023-05-13 16:02:04
疯狂上涨的Python 开发者应从2.x还是3.x着手?
2021-10-25 16:41:54