鱼C论坛

 找回密码
 立即注册
查看: 1526|回复: 7

[已解决]python爬虫爬取套图保存问题

[复制链接]
发表于 2020-3-14 18:25:14 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 一个账号 于 2020-3-14 18:32 编辑

如题,已经获取到每张图片的链接与标题,怎么保存到本地文件夹,我的是错的

  1. import urllib.request
  2. import re
  3. import os
  4. from bs4 import BeautifulSoup
  5. def request_url():
  6.     url='https://www.tooopen.com/topiclist/9620.aspx'
  7.     response =urllib.request.Request(url)
  8.     html = urllib.request.urlopen(response)
  9.     #html.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36')

  10.     html = html.read().decode('utf-8')
  11.     reg =r'<a class="pic" href="(.*?)" title="(.*?)" target="_blank">'
  12.     photo_url= re.findall(reg,html)
  13.     #print(photo_url[0][0])
  14.     for i in photo_url:
  15.         #urls =i[0]
  16.         #name =i[1]
  17.         urls,name =i

  18.     r =urllib.request.urlopen(urls).read().decode('utf-8')
  19.     #print(r)
  20.     soup =BeautifulSoup(r,"html.parser")
  21.     img_url = soup.find("div",class_="img-list").find_all("a")


  22.    
  23.     reg=r'http(.*?)jpg'
  24.     link = re.findall(reg,str(img_url))
  25.    
  26.     for i in link:
  27.         i='http'+i+'jpg'
  28.         print(i)
  29.         download_imgs = urllib.request.urlopen(i).read()

  30.         
  31.         with open('.jpg','wb') as f:
  32.             f.write(i)

  33.         




  34. request_url()

  35.      
复制代码
最佳答案
2020-3-14 18:45:10
小小蛙 发表于 2020-3-14 18:39
版主好,我用pip安装requests模块一直会报错,可以用urllib模块演示吗
  1. import urllib.request
  2. import re
  3. import os
  4. import random
  5. from bs4 import BeautifulSoup
  6. def request_url():
  7.     url='https://www.tooopen.com/topiclist/9620.aspx'
  8.     response =urllib.request.Request(url)
  9.     html = urllib.request.urlopen(response)
  10.     #html.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36')

  11.     html = html.read().decode('utf-8')
  12.     reg =r'<a class="pic" href="(.*?)" title="(.*?)" target="_blank">'
  13.     photo_url= re.findall(reg,html)
  14.     #print(photo_url[0][0])
  15.     for i in photo_url:
  16.         #urls =i[0]
  17.         #name =i[1]
  18.         urls,name =i

  19.     r =urllib.request.urlopen(urls).read().decode('utf-8')
  20.     #print(r)
  21.     soup =BeautifulSoup(r,"html.parser")
  22.     img_url = soup.find("div",class_="img-list").find_all("a")


  23.    
  24.     reg=r'http(.*?)jpg'
  25.     link = re.findall(reg,str(img_url))
  26.    
  27.     for i in link:
  28.         i='http'+i+'jpg'
  29.         print(i)
  30.         download_imgs = urllib.request.urlopen(i).read()

  31.         res = urllib.request.urlopen(i)
  32.         data = res.read()

  33.         
  34.         with open(f'{random.randint(1, 1000)}.jpg','wb') as f:
  35.             f.write(data)

  36.         




  37. request_url()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-14 18:34:09 | 显示全部楼层
上一个帖子谁删的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-14 18:34:34 | 显示全部楼层
wp231957 发表于 2020-3-14 18:34
上一个帖子谁删的

一位版主大哥
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-14 18:35:07 | 显示全部楼层
本帖最后由 一个账号 于 2020-3-14 18:37 编辑


代码帮楼主改好了:

  1. import urllib.request
  2. import re
  3. import os
  4. import random
  5. import requests
  6. from bs4 import BeautifulSoup
  7. def request_url():
  8.     url='https://www.tooopen.com/topiclist/9620.aspx'
  9.     response =urllib.request.Request(url)
  10.     html = urllib.request.urlopen(response)
  11.     #html.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36')

  12.     html = html.read().decode('utf-8')
  13.     reg =r'<a class="pic" href="(.*?)" title="(.*?)" target="_blank">'
  14.     photo_url= re.findall(reg,html)
  15.     #print(photo_url[0][0])
  16.     for i in photo_url:
  17.         #urls =i[0]
  18.         #name =i[1]
  19.         urls,name =i

  20.     r =urllib.request.urlopen(urls).read().decode('utf-8')
  21.     #print(r)
  22.     soup =BeautifulSoup(r,"html.parser")
  23.     img_url = soup.find("div",class_="img-list").find_all("a")


  24.    
  25.     reg=r'http(.*?)jpg'
  26.     link = re.findall(reg,str(img_url))
  27.    
  28.     for i in link:
  29.         i='http'+i+'jpg'
  30.         print(i)
  31.         download_imgs = urllib.request.urlopen(i).read()

  32.         res = requests.get(i)

  33.         
  34.         with open(f'{random.randint(1, 1000)}.jpg','wb') as f:
  35.             f.write(res.content)

  36.         




  37. request_url()

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

使用道具 举报

 楼主| 发表于 2020-3-14 18:39:54 | 显示全部楼层
本帖最后由 一个账号 于 2020-3-14 19:07 编辑


我用pip安装requests模块一直会报错,可以用urllib模块演示吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-14 18:45:10 | 显示全部楼层    本楼为最佳答案   
小小蛙 发表于 2020-3-14 18:39
版主好,我用pip安装requests模块一直会报错,可以用urllib模块演示吗
  1. import urllib.request
  2. import re
  3. import os
  4. import random
  5. from bs4 import BeautifulSoup
  6. def request_url():
  7.     url='https://www.tooopen.com/topiclist/9620.aspx'
  8.     response =urllib.request.Request(url)
  9.     html = urllib.request.urlopen(response)
  10.     #html.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36')

  11.     html = html.read().decode('utf-8')
  12.     reg =r'<a class="pic" href="(.*?)" title="(.*?)" target="_blank">'
  13.     photo_url= re.findall(reg,html)
  14.     #print(photo_url[0][0])
  15.     for i in photo_url:
  16.         #urls =i[0]
  17.         #name =i[1]
  18.         urls,name =i

  19.     r =urllib.request.urlopen(urls).read().decode('utf-8')
  20.     #print(r)
  21.     soup =BeautifulSoup(r,"html.parser")
  22.     img_url = soup.find("div",class_="img-list").find_all("a")


  23.    
  24.     reg=r'http(.*?)jpg'
  25.     link = re.findall(reg,str(img_url))
  26.    
  27.     for i in link:
  28.         i='http'+i+'jpg'
  29.         print(i)
  30.         download_imgs = urllib.request.urlopen(i).read()

  31.         res = urllib.request.urlopen(i)
  32.         data = res.read()

  33.         
  34.         with open(f'{random.randint(1, 1000)}.jpg','wb') as f:
  35.             f.write(data)

  36.         




  37. request_url()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-14 22:19:25 | 显示全部楼层

老哥,可以把照片名字打印上去吗,已经提取到了上面那个name
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-8 05:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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