无法用Python从PHP中提取图像

2024-09-30 03:22:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用Python构建一个程序,该程序将从一个网站获取图像并下载,但该网站返回以下链接:

http://www.tapmusic.net/lastfm/collage.php?user=kogam7&type=7day&size=5x5

它没有*.jpg或*.png扩展名。所以当我尝试使用以下代码时:

import urllib , urllib2, os , sys , time
img = urllib.urlopen("http://www.tapmusic.net/lastfm/collage.php?user=kogam7&type=7day&size=5x5").read()
hand0 = open("test.jpg" , "w")
hand0.write(img)
hand0.close()

它写的图像如下所示:

http://i.stack.imgur.com/yWCBg.jpg

有人知道出什么事了吗?在


Tags: 图像程序httpnet网站wwwtypejpg
2条回答

必须通过将"wb"传递给open(),以二进制模式打开输出文件。如果你在使用Windows(我认为你是因为你的代码应该可以正常工作),这一点很重要

import urllib , urllib2, os , sys , time
img = urllib.urlopen("http://www.tapmusic.net/lastfm/collage.php?user=kogam7&type=7day&size=5x5").read()
hand0 = open("test.jpg" , "wb")
hand0.write(img)
hand0.close()
from urllib2 import urlopen

# Use 'wb' over 'w' 
with open('image.jpg', 'wb') as f:
    f.write(urlopen('http://www.tapmusic.net/lastfm/collage.php?user=kogam7&type=7day&size=5x5').read())

了解“wb”代表什么以及在windows上为什么需要wb。在

What is the 'wb' mean in this code, using Python?

相关问题 更多 >

    热门问题