鱼C论坛

 找回密码
 立即注册
查看: 1879|回复: 3

[已解决]那为大佬帮我看看为什么我爬的图片保存下来后打不开谢谢

[复制链接]
发表于 2022-6-9 17:44:50 | 显示全部楼层 |阅读模式

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

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

x
好心的大佬帮帮忙
也帮我看看怎么提高爬取速度
  1. #1. requests 发送请求,从服务器获取数据。 要自己安装
  2. #2. beautifulsoup 来解析页面源代码。 要自己安装

  3. import requests
  4. from bs4 import BeautifulSoup
  5. import time

  6. headers = {
  7.     "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36"
  8. }
  9. aa = "https://mmzztt.com" #网站
  10. resp = requests.get(aa,headers=headers)
  11. resp.encoding = 'utf-8'
  12. #解析
  13. qq = BeautifulSoup(resp.text,"html.parser")
  14. ww = qq.find('ul',attrs={'class':'uk-grid uk-grid-match uk-child-width-1-4 uk-child-width-1-6@s g-list'}).find_all('a',attrs={"uk-inline u-thumb-f"})#获取热门标签
  15. c = 1
  16. for a in ww:
  17.     heji = a.get("href")#在热门标签里面拿到网站
  18.     resp1 = requests.get(heji, headers=headers)#发送请求
  19.     resp1.encoding = 'utf-8'
  20.     qq1 = BeautifulSoup(resp1.text, "html.parser")
  21.     yeshu = qq1.find('nav',attrs={"uk-container uk-padding-small m-pagination"}).find_next('li').find_next('li').find_next('li').find_next('li').find_next('li').text#获取一共有多少页
  22.     yeshu = int(yeshu)#把页数转为整数
  23.     print(yeshu)#打印有多少页
  24.     c = 1 #网站后缀页数初始值
  25.     n = 1 #图片顺序初始值
  26.     while yeshu > 0:
  27.         heji1 = heji+"page/%s/"%c#把网站拼接后缀页数
  28.         #print(heji1)
  29.         yeshu -= 1#把网站拼接后缀页数依次减少
  30.         c += 1##网站后缀页数初始加1

  31.         resp2 = requests.get(heji1, headers=headers)#发送请求
  32.         resp2.encoding = 'utf-8'
  33.         qq2 = BeautifulSoup(resp1.text, "html.parser")
  34.         ww1 = qq2.find('ul',attrs={'class': 'uk-grid uk-grid-match uk-child-width-1-2@s uk-child-width-1-3@m g-list'}).find_all('img')#获取图片地址
  35.         #mingz = qq2.find('ul', attrs={'class': 'uk-grid uk-grid-match uk-child-width-1-2@s uk-child-width-1-3@m g-list'}).find_all('div', attrs={'class': 'uk-card-body uk-padding-remove'})#获取图片名字
  36.         for img in ww1:
  37.             tupian = img.get('data-srcset')#[0]#获取所有图片地址"""
  38.             #保存数据
  39.             time.sleep(1)
  40.             print(tupian)
  41.             f = open("%s.jpg"%n,mode="wb")  # 保存文件
  42.             f.write(requests.get(tupian).content)  # 向外那图片
  43.             n += 1##网站后缀页数初始加初始
  44.             print('成功')#"""
复制代码
最佳答案
2022-6-12 13:19:15
爬取图片时,需要加headers
提速的话需要使用代理ip,没有代理ip就算用多线程也容易因为ip访问速度过快而被网站封禁
  1. #1. requests 发送请求,从服务器获取数据。 要自己安装
  2. #2. beautifulsoup 来解析页面源代码。 要自己安装

  3. import requests
  4. from bs4 import BeautifulSoup
  5. import time

  6. headers = {
  7.     "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36"
  8. }
  9. aa = "https://mmzztt.com" #网站
  10. resp = requests.get(aa,headers=headers)
  11. resp.encoding = 'utf-8'
  12. #解析
  13. qq = BeautifulSoup(resp.text,"html.parser")
  14. ww = qq.find('ul',attrs={'class':'uk-grid uk-grid-match uk-child-width-1-4 uk-child-width-1-6@s g-list'}).find_all('a',attrs={"uk-inline u-thumb-f"})#获取热门标签
  15. c = 1
  16. for a in ww:
  17.     heji = a.get("href")#在热门标签里面拿到网站
  18.     resp1 = requests.get(heji, headers=headers)#发送请求
  19.     resp1.encoding = 'utf-8'
  20.     qq1 = BeautifulSoup(resp1.text, "html.parser")
  21.     yeshu = qq1.find('nav',attrs={"uk-container uk-padding-small m-pagination"}).find_next('li').find_next('li').find_next('li').find_next('li').find_next('li').text#获取一共有多少页
  22.     yeshu = int(yeshu)#把页数转为整数
  23.     print(yeshu)#打印有多少页
  24.     c = 1 #网站后缀页数初始值
  25.     n = 1 #图片顺序初始值
  26.     while yeshu > 0:
  27.         heji1 = heji+"page/%s/"%c#把网站拼接后缀页数
  28.         #print(heji1)
  29.         yeshu -= 1#把网站拼接后缀页数依次减少
  30.         c += 1##网站后缀页数初始加1

  31.         resp2 = requests.get(heji1, headers=headers)#发送请求
  32.         resp2.encoding = 'utf-8'
  33.         qq2 = BeautifulSoup(resp1.text, "html.parser")
  34.         ww1 = qq2.find('ul',attrs={'class': 'uk-grid uk-grid-match uk-child-width-1-2@s uk-child-width-1-3@m g-list'}).find_all('img')#获取图片地址
  35.         #mingz = qq2.find('ul', attrs={'class': 'uk-grid uk-grid-match uk-child-width-1-2@s uk-child-width-1-3@m g-list'}).find_all('div', attrs={'class': 'uk-card-body uk-padding-remove'})#获取图片名字
  36.         for img in ww1:
  37.             tupian = img.get('data-srcset')#[0]#获取所有图片地址"""
  38.             #保存数据
  39.             time.sleep(1)
  40.             print(tupian)
  41.             f = open("%s.jpg"%n,mode="wb")  # 保存文件
  42.             f.write(requests.get(tupian, headers=headers).content)  # 向外那图片
  43.             n += 1##网站后缀页数初始加初始
  44.             print('成功')#"""
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-6-9 18:54:35 From FishC Mobile | 显示全部楼层
f.close()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-6-11 16:13:00 | 显示全部楼层
f.write(requests.get(tupian).content) 这行加上user-agent
提速的话开多线程
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-6-12 13:19:15 | 显示全部楼层    本楼为最佳答案   
爬取图片时,需要加headers
提速的话需要使用代理ip,没有代理ip就算用多线程也容易因为ip访问速度过快而被网站封禁
  1. #1. requests 发送请求,从服务器获取数据。 要自己安装
  2. #2. beautifulsoup 来解析页面源代码。 要自己安装

  3. import requests
  4. from bs4 import BeautifulSoup
  5. import time

  6. headers = {
  7.     "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36"
  8. }
  9. aa = "https://mmzztt.com" #网站
  10. resp = requests.get(aa,headers=headers)
  11. resp.encoding = 'utf-8'
  12. #解析
  13. qq = BeautifulSoup(resp.text,"html.parser")
  14. ww = qq.find('ul',attrs={'class':'uk-grid uk-grid-match uk-child-width-1-4 uk-child-width-1-6@s g-list'}).find_all('a',attrs={"uk-inline u-thumb-f"})#获取热门标签
  15. c = 1
  16. for a in ww:
  17.     heji = a.get("href")#在热门标签里面拿到网站
  18.     resp1 = requests.get(heji, headers=headers)#发送请求
  19.     resp1.encoding = 'utf-8'
  20.     qq1 = BeautifulSoup(resp1.text, "html.parser")
  21.     yeshu = qq1.find('nav',attrs={"uk-container uk-padding-small m-pagination"}).find_next('li').find_next('li').find_next('li').find_next('li').find_next('li').text#获取一共有多少页
  22.     yeshu = int(yeshu)#把页数转为整数
  23.     print(yeshu)#打印有多少页
  24.     c = 1 #网站后缀页数初始值
  25.     n = 1 #图片顺序初始值
  26.     while yeshu > 0:
  27.         heji1 = heji+"page/%s/"%c#把网站拼接后缀页数
  28.         #print(heji1)
  29.         yeshu -= 1#把网站拼接后缀页数依次减少
  30.         c += 1##网站后缀页数初始加1

  31.         resp2 = requests.get(heji1, headers=headers)#发送请求
  32.         resp2.encoding = 'utf-8'
  33.         qq2 = BeautifulSoup(resp1.text, "html.parser")
  34.         ww1 = qq2.find('ul',attrs={'class': 'uk-grid uk-grid-match uk-child-width-1-2@s uk-child-width-1-3@m g-list'}).find_all('img')#获取图片地址
  35.         #mingz = qq2.find('ul', attrs={'class': 'uk-grid uk-grid-match uk-child-width-1-2@s uk-child-width-1-3@m g-list'}).find_all('div', attrs={'class': 'uk-card-body uk-padding-remove'})#获取图片名字
  36.         for img in ww1:
  37.             tupian = img.get('data-srcset')#[0]#获取所有图片地址"""
  38.             #保存数据
  39.             time.sleep(1)
  40.             print(tupian)
  41.             f = open("%s.jpg"%n,mode="wb")  # 保存文件
  42.             f.write(requests.get(tupian, headers=headers).content)  # 向外那图片
  43.             n += 1##网站后缀页数初始加初始
  44.             print('成功')#"""
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-22 19:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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