鱼C论坛

 找回密码
 立即注册
查看: 1554|回复: 4

[已解决]爬虫只能爬一张图····

[复制链接]
发表于 2020-7-20 19:04:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
请大神帮忙分析下,为什么下面的代码只能爬一张图,我是要想把搜到的图片都爬下来,且(f+=1)每次都有打印,图片却只有一张:
  1. import requests
  2. from bs4 import BeautifulSoup
  3. def getUrls():
  4.     rootUrl='http://sc.chinaz.com/tupian/'
  5.     r=requests.get(rootUrl)
  6.     r.encoding='utf-8'
  7.     soup=BeautifulSoup(r.text,'html.parser')
  8.     notes=soup.find_all('img',)
  9.     links=[]
  10.     for note in notes:
  11.         link=note['src2']
  12.         links.append(link)
  13.     return links
  14. def getImgs():
  15.     imgs=[]
  16.     for link in getUrls():
  17.         r=requests.get(link)
  18.         r.encoding='utf-8'
  19.         imgs.append(r.content)
  20.     return imgs

  21. def downLoad():
  22.     f='1'
  23.     with open('%s.jpg'%f,'wb') as fout:
  24.         for img in getImgs():
  25.             fout.write(img)
  26.             print(f)
  27.             f+='1'
  28. downLoad()
复制代码

最佳答案
2020-7-20 20:25:58

改成这样:

  1. import requests
  2. from bs4 import BeautifulSoup
  3. def getUrls():
  4.     rootUrl='http://sc.chinaz.com/tupian/'
  5.     r=requests.get(rootUrl)
  6.     r.encoding='utf-8'
  7.     soup=BeautifulSoup(r.text,'html.parser')
  8.     notes=soup.find_all('img',)
  9.     links=[]
  10.     for note in notes:
  11.         link=note['src2']
  12.         links.append(link)
  13.     return links
  14. def getImgs():
  15.     imgs=[]
  16.     for link in getUrls():
  17.         r=requests.get(link)
  18.         r.encoding='utf-8'
  19.         imgs.append(r.content)
  20.     return imgs

  21. def downLoad():
  22.     f='1'
  23.     for img in getImgs():
  24.         with open('%s.jpg' % f, 'wb') as fout:
  25.             fout.write(img)
  26.             print(f)
  27.             f+='1'
  28. downLoad()
复制代码
1595242864(1).jpg
1595242891(1).jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-20 19:22:35 | 显示全部楼层
本帖最后由 小甲鱼的铁粉 于 2020-7-20 19:23 编辑

不好意思我看错了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-20 19:28:43 | 显示全部楼层

??????
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-20 20:25:58 | 显示全部楼层    本楼为最佳答案   

改成这样:

  1. import requests
  2. from bs4 import BeautifulSoup
  3. def getUrls():
  4.     rootUrl='http://sc.chinaz.com/tupian/'
  5.     r=requests.get(rootUrl)
  6.     r.encoding='utf-8'
  7.     soup=BeautifulSoup(r.text,'html.parser')
  8.     notes=soup.find_all('img',)
  9.     links=[]
  10.     for note in notes:
  11.         link=note['src2']
  12.         links.append(link)
  13.     return links
  14. def getImgs():
  15.     imgs=[]
  16.     for link in getUrls():
  17.         r=requests.get(link)
  18.         r.encoding='utf-8'
  19.         imgs.append(r.content)
  20.     return imgs

  21. def downLoad():
  22.     f='1'
  23.     for img in getImgs():
  24.         with open('%s.jpg' % f, 'wb') as fout:
  25.             fout.write(img)
  26.             print(f)
  27.             f+='1'
  28. downLoad()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-7-20 20:38:53 | 显示全部楼层

不愧是大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-23 21:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表