|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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)
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])
|
|