1 import urllib2,urllib 2 import re 3 import os,sys 4 def download_img(name,fname): 5 fname = fname.strip() #去除首位空格 6 is_exists = os.path.exists(fname) 7 if not is_exists: 8 os.makedirs(fname) 9 print fname+" mkdir success!" 10 else: 11 print "file exists!" 12 sys.exit(0) 13 url = "https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1493196924875_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&hs=2&word="+urllib.quote(name) 14 t = 0 15 r = urllib.urlopen(url) 16 pattern = "objURL\":\"(.*?)\"," 17 res = re.findall(pattern,r.read()) 18 for i in res: 19 finame = fname+"/"+str(t)+str(".jpg") 20 urllib.urlretrieve(i,filename=finame) 21 t+=1 22 if __name__ == '__main__': 23 download_img("陈奕迅",'test')
在下载图片的时候报错,如下,导致我每一次只能下载18张图片,但是获取的url有30张,求大佬解决!!