1- #!/usr/bin/env python
1+ #!/usr/bin/env python
22
33# -*- coding:utf-8 -*-
44
55# -*- author:arron ni-*-
66
77# python3抓取bing主页所有背景图片
88
9- import urllib .request ,re ,sys ,os
9+ import urllib .request
10+ import re
11+ import sys
12+ import os
1013
11- def get_bing_backphoto ():
12-
13- if (os .path .exists ('photos' )== False ):
1414
15+ def get_bing_backphoto ():
16+ if os .path .exists ('photos' ) == False :
1517 os .mkdir ('photos' )
1618
17- for i in range (0 ,30 ):
19+ for i in range (0 , 30 ):
1820
19- url = 'http://cn.bing.com/HPImageArchive.aspx?format=js&idx=' + str (i )+ '&n=1&nc=1361089515117&FORM=HYLH1'
21+ url = 'http://cn.bing.com/HPImageArchive.aspx?format=js&idx=' + str (i ) + '&n=1&nc=1361089515117&FORM=HYLH1'
2022
2123 html = urllib .request .urlopen (url ).read ()
2224
2325 if html == 'null' :
24-
25- print ( 'open & read bing error!' )
26+ print ('open & read bing error!' )
2627
2728 sys .exit (- 1 )
2829
2930 html = html .decode ('utf-8' )
3031
31- reg = re .compile ('"url":"(.*?)","urlbase"' ,re .S )
32+ reg = re .compile ('"url":"(.*?)","urlbase"' , re .S )
3233
33- text = re .findall (reg ,html )
34+ text = re .findall (reg , html )
3435
35- #http://s.cn.bing.net/az/hprichbg/rb/LongJi_ZH-CN8658435963_1366x768.jpg
36-
37- for imgurl in text :
36+ # http://s.cn.bing.net/az/hprichbg/rb/LongJi_ZH-CN8658435963_1366x768.jpg
3837
38+ for imgurl in text :
3939 right = imgurl .rindex ('/' )
4040
41- name = imgurl .replace (imgurl [:right + 1 ],'' )
41+ name = imgurl .replace (imgurl [:right + 1 ], '' )
4242
43- savepath = 'photos/' + name
43+ savepath = 'photos/' + name
4444
4545 urllib .request .urlretrieve (imgurl , savepath )
4646
47- print (name + ' save success!' )
48-
47+ print (name + ' save success!' )
48+
49+
4950get_bing_backphoto ()
0 commit comments