本次目的:
python 抓取某某站图片
- 系统性分析页面
- 多页面数据解析
- 海量图片数据保存
- 解释器版本 >>> python 3.8
- 代码编辑器 >>> pycharm 2021.2
- requests >>> pip install requests
- parsel >>> pip install parsel
- 找到目标网站 链接
- 获取每个相册的地址 去批量的下载图片
- 发送网络请求 向相册列表页 链接
- 获取数据 网页源代码
- 筛选数据 相册地址
- 发送网络请求 向相册页面 并且 获取数据 网页源代码
- 筛选数据 图片地址
- 发送网络请求 & 获取数据 向图片地址
- 保存图片
import requests
import parsel # lxml re bs4
import re
import os
2. 加一个伪装
headers = {
'referer': 'https://www.jdlingyu.com/tuji',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36'
}
for page in range(1, 393):
url = f'https://www.jdlingyu.com/tuji/page/{page}'
3. 发送网络请求
response = requests.get(url, headers=headers)
# : 请求成功
4. 获取数据 网页源代码
html_data = response.text
5. 筛选数据
# re
# url_list = re.findall('.*?', html_data)
# xpath
selector = parsel.Selector(html_data)
url_list = selector.xpath('//div[@class="post-info"]/h2/a/@href').getall()
6. 发送网络请求 向相册页面 并且 获取数据 网页源代码
for detail_url in url_list:
detail_html = requests.get(detail_url, headers=headers).text
相对应的安装包/安装教程/激活码/使用教程/学习资料/工具插件 可以点击免费领取
7. 筛选数据 图片地址
detail_selector = parsel.Selector(detail_html)
title = detail_selector.xpath('//h1/text()').get()
# 如果不存在 img/title
if not os.path.exists('img/' + title):
# 那就新建
os.mkdir('img/' + title)
img_list = detail_selector.xpath('//div[@class="entry-content"]//img/@src').getall()
for img in img_list:
# 获取二进制数据
img_data = requests.get(img, headers=headers).content
img_title = img.split('/')[-1]
with open(f'img/{title}/{img_title}', mode='wb') as f:
f.write(img_data)
print(f'正在爬取: {img_title}')
好了,我的这篇文章写到这里就结束啦!
有更多建议或问题可以评论区或私信我哦!一起加油努力叭(ง •_•)ง
喜欢就关注一下博主,或点赞收藏评论一下我的文章叭!!!