PYTHON笔记二:通过PYTHON批量下载图片

本文的代码是通过PYTHON来对当前页的图片地址进行解析,获取地址列表后,分别获取每一页的特定图片链接,并进行批量下载。

爬取页面:https://wall.alphacoders.com/

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
url = "https://wall.alphacoders.com/popular.php?lang=Chinese"
import requests
headers = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"}
resp = requests.get(url,headers=headers)
print(resp.status_code)
resp.encoding = "utf-8"
#print(resp.text)

from bs4 import BeautifulSoup
soup = BeautifulSoup(resp.text,"html.parser")
tags = soup("a")
import os

lst = list()
for tag in tags:
if tag.get("rel") and tag.get("href") and tag.get("title"):
url = "https://wall.alphacoders.com" + tag.get("href")
lst.append(url)
#print(lst)

count = 0
for url in lst:
resp = requests.get(url,headers=headers)
# print(resp.status_code)
resp.encoding = "utf-8"
soup = BeautifulSoup(resp.text,"html.parser")
tags = soup("img")
count = count + 1

for tag in tags:
if tag.get("src") and tag.get("title"):
img_src = tag.get("src")
img_class = tag.get("class")
filename = os.path.basename(img_src)
with open(f"picture/{filename}","wb") as fout:
fout.write(requests.get(img_src).content)
print(count,img_src,filename,img_class)

print(count,"pictures are downloaded")

运行代码后,结果如下:

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
200 # CODE200可以正常爬取
1 https://images3.alphacoders.com/121/thumb-1920-1210173.jpg thumb-1920-1210173.jpg ['main-content']
2 https://images2.alphacoders.com/120/thumb-1920-1209425.png thumb-1920-1209425.png ['main-content']
3 https://images6.alphacoders.com/121/thumb-1920-1210764.png thumb-1920-1210764.png ['main-content']
4 https://images2.alphacoders.com/120/thumb-1920-1209249.jpg thumb-1920-1209249.jpg ['main-content']
5 https://images4.alphacoders.com/120/thumb-1920-1209427.jpg thumb-1920-1209427.jpg ['main-content']
6 https://images5.alphacoders.com/120/thumb-1920-1209251.jpg thumb-1920-1209251.jpg ['main-content']
7 https://images.alphacoders.com/120/thumb-1920-1209368.jpg thumb-1920-1209368.jpg ['main-content']
8 https://images.alphacoders.com/120/thumb-1920-1209632.jpg thumb-1920-1209632.jpg ['main-content']
9 https://images8.alphacoders.com/121/thumb-1920-1210064.jpg thumb-1920-1210064.jpg ['main-content']
10 https://images5.alphacoders.com/120/thumb-1920-1209734.jpg thumb-1920-1209734.jpg ['main-content']
11 https://images4.alphacoders.com/120/thumb-1920-1209815.jpg thumb-1920-1209815.jpg ['main-content']
12 https://images5.alphacoders.com/120/thumb-1920-1209796.jpg thumb-1920-1209796.jpg ['main-content']
13 https://images3.alphacoders.com/121/thumb-1920-1210062.jpg thumb-1920-1210062.jpg ['main-content']
14 https://images2.alphacoders.com/120/thumb-1920-1209938.jpg thumb-1920-1209938.jpg ['main-content']
15 https://images8.alphacoders.com/121/thumb-1920-1210309.jpg thumb-1920-1210309.jpg ['main-content']
16 https://images6.alphacoders.com/121/thumb-1920-1210276.jpg thumb-1920-1210276.jpg ['main-content']
17 https://images4.alphacoders.com/120/thumb-1920-1209318.png thumb-1920-1209318.png ['main-content']
18 https://images6.alphacoders.com/120/thumb-1920-1209428.jpg thumb-1920-1209428.jpg ['main-content']
19 https://images7.alphacoders.com/120/thumb-1920-1209250.jpg thumb-1920-1209250.jpg ['main-content']
20 https://images6.alphacoders.com/120/thumb-1920-1209629.jpg thumb-1920-1209629.jpg ['main-content']
21 https://images2.alphacoders.com/120/thumb-1920-1209392.png thumb-1920-1209392.png ['main-content']
22 https://images7.alphacoders.com/120/thumb-1920-1209633.jpg thumb-1920-1209633.jpg ['main-content']
23 https://images7.alphacoders.com/120/thumb-1920-1209319.png thumb-1920-1209319.png ['main-content']
24 https://images8.alphacoders.com/120/thumb-1920-1209315.png thumb-1920-1209315.png ['main-content']
25 https://images8.alphacoders.com/120/thumb-1920-1209791.jpg thumb-1920-1209791.jpg ['main-content']
26 https://images3.alphacoders.com/120/thumb-1920-1209426.jpg thumb-1920-1209426.jpg ['main-content']
27 https://images8.alphacoders.com/120/thumb-1920-1209859.png thumb-1920-1209859.png ['main-content']
28 https://images3.alphacoders.com/121/thumb-1920-1210339.jpg thumb-1920-1210339.jpg ['main-content']
29 https://images5.alphacoders.com/121/thumb-1920-1210580.png thumb-1920-1210580.png ['main-content']
30 https://images4.alphacoders.com/121/thumb-1920-1210291.jpg thumb-1920-1210291.jpg ['main-content']
30 pictures are downloaded #共下载30个文件
  • 本文作者:括囊无誉
  • 本文链接: Python/python-2-download-img/
  • 版权声明: 本博客所有文章均为原创作品,转载请注明出处!
------ 本文结束 ------
坚持原创文章分享,您的支持将鼓励我继续创作!