爬虫只能爬一张图····
请大神帮忙分析下,为什么下面的代码只能爬一张图,我是要想把搜到的图片都爬下来,且(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()
本帖最后由 小甲鱼的铁粉 于 2020-7-20 19:23 编辑
不好意思我看错了 小甲鱼的铁粉 发表于 2020-7-20 19:22
不好意思我看错了
??????
改成这样:
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() Twilight6 发表于 2020-7-20 20:25
改成这样:
不愧是大佬
页:
[1]