背景:

有时候想搬运一些文章,文章图片少的话,可以一个一个右键保存在本地,一旦几十张以上会非常麻烦,正好最近也在自学 Python 爬虫,不妨写个 demo 用一用,顺便水一篇文章,哦不对,是两篇(搬运+教程)🤣

核心代码

代码核心点就是拿到图片链接命名存储到本地文件夹

1
2
3
4
5
6
7
8
9
10
for img_url in soup.select("<ID、Class、Tag等等定位>"):
img_url = img_url.get("<data-src或者src>")
res = requests.get(img_url)

now = time.strftime("%Y-%m-%d_%H-%M-%S_", time.localtime()) # 当前时间-年月日时分秒
ms = str(time.time()).split(".")[1] # 当前时间-微秒
img_name = file_path + now + ms + ".jpg" #图片命名规则

with open(img_name, 'wb') as f:
f.write(res.content)

Demo 代码

本站《尖椒酿肉,下饭必备》文章里的图片是我首次用 Python 爬虫爬取的,Demo 代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- coding: utf-8 -*-
# @Time :2022/3/15 0015 09:45
# @Author:HarrisWong
# @File : img_download_wechat.py
# @Software: PyCharm

from bs4 import BeautifulSoup # 网页解析,获取数据
import requests # 制定URL,获取网页数据
import time # 用当前时间给图片取名
import urllib.request, urllib.error

def main():
url = 'https://mp.weixin.qq.com/s/O_EgogBxU8ReQO-pd60J0g' #文章链接
download(url)

# 用户代理:表示告诉服务器,我们是什么类型的浏览器,可以接收什么样水平的文件
headers = { # 模拟浏览器头部信息,向服务器发送消息
"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
}

file_path = r"F:\360MoveData\Users\Administrator\Pictures\博客专用\CDN\posts\Foods\\" #文件保存地址

def download(url):
html = askURL(url)
soup = BeautifulSoup(html, "html.parser")

#核心代码
for img_url in soup.select(".rich_media_content img"):
img_url = img_url.get("data-src")
res = requests.get(img_url)

now = time.strftime("%Y-%m-%d_%H-%M-%S_", time.localtime()) # 当前时间-年月日时分秒
ms = str(time.time()).split(".")[1] # 当前时间-微秒
img_name = file_path + now + ms + ".jpg" #图片命名规则

with open(img_name, 'wb') as f:
f.write(res.content)

def askURL(url):
#得到指定一个URL的网页内容
request = urllib.request.Request(url, headers=headers)
html = ""
try:
response = urllib.request.urlopen(request)
html = response.read().decode("utf-8")
except urllib.error.URLError as e:
if hasattr(e,"code"):
print(e.code)
if hasattr(e,"reason"):
print(e.reason)
return html

if __name__ == '__main__': # 当程序执行时
# 调用函数
main()
print("爬取完毕!")

此处我用日期+时间方式来命名爬取的每个图片,各位可自行更改命名方式,或者直接用图片的原名,即去掉链接中的域名,用剩余部分当图片名。传统功夫点到为止

参考教程

结束语

有 Python 基础的小伙伴可以自己尝试在源代码基础上更改。话说会不会有 lsp 是来学爬妹子图片的呢?🤔