Skip to content

Commit bca9799

Browse files
committed
add code
1 parent a66c8b5 commit bca9799

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

doudou/2021-09-28-wallpaper/app.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# coding: utf8
2+
3+
import urllib.request
4+
import requests as req
5+
import time
6+
import json
7+
8+
9+
# 下载图片
10+
def download_img(img_url, file_name):
11+
print(F'downloading {file_name}, img_url = {img_url}')
12+
request = urllib.request.Request(img_url)
13+
try:
14+
response = urllib.request.urlopen(request)
15+
if (response.getcode() == 200):
16+
with open(file_name, "wb") as f:
17+
f.write(response.read()) # 将内容写入图片
18+
return 'ok'
19+
except:
20+
return "fail"
21+
22+
23+
def send_get(url, params):
24+
time.sleep(2)
25+
response = req.get(url, headers=None, params=params, verify=False)
26+
return response.text
27+
28+
29+
def deal_result(result, page):
30+
index = page * 12
31+
for i in range(len(result)):
32+
img_url = result[i]['urls']['full']
33+
index += 1
34+
download_img(img_url, str(index) + '.png')
35+
36+
37+
def loop():
38+
for i in range(3):
39+
url = 'https://unsplash.com/napi/photos?per_page=12&page=' + str(i)
40+
print(F'page = {i}, url = {url}')
41+
response = json.loads(send_get(url, None))
42+
deal_result(response, i)
43+
44+
45+
if __name__ == '__main__':
46+
loop()

doudou/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Python技术 公众号文章代码库
88

99
## 实例代码
1010

11+
[神器 | 一键下载海量高清壁纸](https://github.com/JustDoPython/python-examples/tree/master/doudou/2021-09-28-wallpaper)
12+
1113
[吊炸天!十行代码我把情书藏进了小姐姐的微信头像里](https://github.com/JustDoPython/python-examples/tree/master/doudou/2021-09-08-text-img)
1214

1315
[解放双手,提高生产力,看我如何用 Python 实现自动化剪视频](https://github.com/JustDoPython/python-examples/tree/master/doudou/2021-06-30-pyautogui)

0 commit comments

Comments
 (0)