-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathbing_photos.py
50 lines (28 loc) · 1.05 KB
/
bing_photos.py
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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# -*- author:arron ni-*-
# python3抓取bing主页所有背景图片
import urllib.request
import re
import sys
import os
def get_bing_backphoto():
if os.path.exists('photos') == False:
os.mkdir('photos')
for i in range(0, 30):
url = 'http://cn.bing.com/HPImageArchive.aspx?format=js&idx=' + str(i) + '&n=1&nc=1361089515117&FORM=HYLH1'
html = urllib.request.urlopen(url).read()
if html == 'null':
print('open & read bing error!')
sys.exit(-1)
html = html.decode('utf-8')
reg = re.compile('"url":"(.*?)","urlbase"', re.S)
text = re.findall(reg, html)
# http://s.cn.bing.net/az/hprichbg/rb/LongJi_ZH-CN8658435963_1366x768.jpg
for imgurl in text:
right = imgurl.rindex('/')
name = imgurl.replace(imgurl[:right + 1], '')
savepath = 'photos/' + name
urllib.request.urlretrieve(imgurl, savepath)
print(name + ' save success!')
get_bing_backphoto()