Python简单网络图片爬虫

代码实现:

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
## 这网站的壁纸挺好看的,但只给个接口,莫名其妙写出来了
import json, requests
from unittest import result
import numpy as np
from urllib.request import urlopen
for i in range(1,50):
api_url = 'http://skri.iw233.cn/api.php?sort=pc&type=json&num=1'
headers = {'User-Agent': 'python-requests/2.27.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}

send_json = json.dumps(api_url)
r = requests.get(api_url,headers=headers)
result = r.json()

pic = result.get('pic') #取出键值
pic = ' '.join(pic)
pic.strip('[]')
print(pic)

html = requests.get(pic)
file_name = pic.split('/')[-1]
try:
with open('123\\'+ file_name,'wb') as file:
file.write(html.content)
print(file_name + " 保存完毕")
print("成功")
except:
print("失败")

Python简单网络图片爬虫
http://example.com/2023/06/25/Python简单网络图片爬虫/
Author
fwlw
Posted on
June 25, 2023
Licensed under