鱼C论坛

 找回密码
 立即注册
查看: 1306|回复: 1

[已解决]爬取图片的问题

[复制链接]
发表于 2017-12-25 11:13:51 | 显示全部楼层 |阅读模式

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

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

x
爬取猫片的时候,为什么保存的文件不是jpg格式呢?虽然选择照片查看器能打开,但是感觉有点麻烦
哪里有问题吗




import easygui as g
import urllib.request as ur
Fields = ['宽','高']
size = [width,height] = [ 400,600]
SIZE = []
SIZE = g.multenterbox(title = '下载一只喵',msg = '请填写喵的尺寸',fields = Fields,values = size)
while 1:
    errmsg = ''
    try:
        width = int(SIZE[0].strip())
    except:
        errmsg += '宽度必须为整数!'

    try:
        height = int(SIZE[1].strip())
    except:
        errmsg += '高度必须为整数!'

    if errmsg == '':
        break
    SIZE = g.multenterbox(msg = errmsg,title='下载一只喵',fields = Fields,values = SIZE)
   
SIZE = [int(SIZE[0]),int(SIZE[1])]
url = 'http://placekitten.com/g/%d/%d'%(SIZE[0],SIZE[1])
response = ur.urlopen(url)
filename = 'cat_image%d_%d'%(SIZE[0],SIZE[1])
cat_image = response.read()

filepath = g.diropenbox('请选择保存路径')
place = '%s/cat_image%d_%d'% (filepath,SIZE[0],SIZE[1])
if filepath:
    with open(place,'wb') as f:
        f.write(cat_image)
else:
    with open(filename,'wb') as f:
        f.write(cat_image)
最佳答案
2017-12-25 12:25:23
SIZE = [int(SIZE[0]),int(SIZE[1])]
url = 'http://placekitten.com/g/%d/%d'%(SIZE[0],SIZE[1])
response = ur.urlopen(url)
filename = 'cat_image%d_%d.jpg'%(SIZE[0],SIZE[1])
cat_image = response.read()

filepath = g.diropenbox('请选择保存路径')
place = '%s/cat_image%d_%d.jpg'% (filepath,SIZE[0],SIZE[1])
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-12-25 12:25:23 | 显示全部楼层    本楼为最佳答案   
SIZE = [int(SIZE[0]),int(SIZE[1])]
url = 'http://placekitten.com/g/%d/%d'%(SIZE[0],SIZE[1])
response = ur.urlopen(url)
filename = 'cat_image%d_%d.jpg'%(SIZE[0],SIZE[1])
cat_image = response.read()

filepath = g.diropenbox('请选择保存路径')
place = '%s/cat_image%d_%d.jpg'% (filepath,SIZE[0],SIZE[1])
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-18 09:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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