|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
请大神帮忙分析下,为什么下面的代码只能爬一张图,我是要想把搜到的图片都爬下来,且(f+=1)每次都有打印,图片却只有一张:
- import requests
- from bs4 import BeautifulSoup
- def getUrls():
- rootUrl='http://sc.chinaz.com/tupian/'
- r=requests.get(rootUrl)
- r.encoding='utf-8'
- soup=BeautifulSoup(r.text,'html.parser')
- notes=soup.find_all('img',)
- links=[]
- for note in notes:
- link=note['src2']
- links.append(link)
- return links
- def getImgs():
- imgs=[]
- for link in getUrls():
- r=requests.get(link)
- r.encoding='utf-8'
- imgs.append(r.content)
- return imgs
- def downLoad():
- f='1'
- with open('%s.jpg'%f,'wb') as fout:
- for img in getImgs():
- fout.write(img)
- print(f)
- f+='1'
- downLoad()
复制代码
改成这样:
- import requests
- from bs4 import BeautifulSoup
- def getUrls():
- rootUrl='http://sc.chinaz.com/tupian/'
- r=requests.get(rootUrl)
- r.encoding='utf-8'
- soup=BeautifulSoup(r.text,'html.parser')
- notes=soup.find_all('img',)
- links=[]
- for note in notes:
- link=note['src2']
- links.append(link)
- return links
- def getImgs():
- imgs=[]
- for link in getUrls():
- r=requests.get(link)
- r.encoding='utf-8'
- imgs.append(r.content)
- return imgs
- def downLoad():
- f='1'
- for img in getImgs():
- with open('%s.jpg' % f, 'wb') as fout:
- fout.write(img)
- print(f)
- f+='1'
- downLoad()
复制代码
|
-
-
|