2007年10月10日 星期三 13:45
cookbook2ÖеÄ*Downloading a File from the Web*
#!/usr/bin/env python
"""File downloading from the web.
"""
def download(url):
	"""Copy the contents of a file from a given URL
	to a local file.
	"""
	import urllib
	webFile = urllib.urlopen(url)
	localFile = open(url.split('/')[-1], 'w')
	localFile.write(webFile.read())
	webFile.close()
	localFile.close()
if __name__ == '__main__':
	import sys
	if len(sys.argv) == 2:
		try:
			download(sys.argv[1])
		except IOError:
			print 'Filename not found.'
	else:
		import os
		print 'usage: %s http://server.com/path/to/filename' %
os.path.basename(sys.argv[0])
linuxÏÂʹÓÃûÓÐÎÊÌ⣬µ«ÊÇÔÚwindowsÏÂʹÓã¬ÏÂÔØÏÂÀ´µÄÎļþÎÞ·¨Ê¹Óã¬ÄÄλÄܸø½²½²ÔÒòºÍ½â¾ö°ì·¨
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071010/51019f94/attachment.htm 
2007年10月10日 星期三 13:57
On 10/10/07, bakefish <yellowfool在gmail.com> wrote: > localFile = open(url.split('/')[-1], 'w') 这个'w'改成'wb' -- Qiangning Hong http://www.douban.com/people/hongqn/
2007年10月10日 星期三 14:00
试试这个
import urllib
def download(url):
        file(url.split('/')[-1], 'wb').write(urllib.urlopen(url).read())
2007年10月10日 星期三 14:01
²»Ðеģ¬ÎÒÊÔ¹ý£¬²»ÊǶþ½øÖƵÄÎÊÌâ£¬ËÆºõÔÚwindowsƽ̨Ï»á×Ô¶¯¼ÓÈë'\r'£¬ÄãÊÔ×ÅÓÃÕâ¸ö´úÂëÏÂtxtÎļþ¾ÍÄÜ¿´µ½¡£ 2007/10/10, Qiangning Hong <hongqn在gmail.com>: > > On 10/10/07, bakefish <yellowfool在gmail.com> wrote: > > localFile = open(url.split('/')[-1], 'w') > > Õâ¸ö'w'¸Ä³É'wb' > > -- > Qiangning Hong > http://www.douban.com/people/hongqn/ > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20071010/e93c1e47/attachment.html
2007年10月10日 星期三 14:08
换urllib2看看?
我写的下载图片的方法。
如果不对文件头做判断可能会将404等网页当成文件下回来。
def downloadPic(url, filename):
    request = urllib2.Request(url)
    opener = urllib2.build_opener()
    f = opener.open(request)
    if f.headers.dict['content-type']=='image/jpeg':
        #保存到文件
        xfile = open(filename, 'wb')
        xfile.write(f.read())
        xfile.close()
        return True
    else:
        #TODO 抛出异常
        raise Exception,'not img'
在 07-10-10,bakefish<yellowfool在gmail.com> 写道:
> 不行的,我试过,不是二进制的问题,似乎在windows平台下会自动加入'\r',你试着用这个代码下txt文件就能看到。
>
>
> 2007/10/10, Qiangning Hong <hongqn在gmail.com>:
> > On 10/10/07, bakefish <yellowfool在gmail.com> wrote:
> > >  localFile = open( url.split('/')[-1], 'w')
> >
> > 这个'w'改成'wb'
> >
> > --
> > Qiangning Hong
> > http://www.douban.com/people/hongqn/
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
-- 
Blog http://vicalloy.spaces.live.com/
老照片 http://www.lzpian.com/
2007年10月10日 星期三 14:12
换urllib2看看?
我写的下载图片的方法。
如果不对文件头做判断可能会将404等网页当成文件下回来。
def downloadPic(url, filename):
    request = urllib2.Request(url)
    opener = urllib2.build_opener()
    f = opener.open(request)
    if f.headers.dict['content-type']=='image/jpeg':
        #保存到文件
        xfile = open(filename, 'wb')
        xfile.write(f.read())
        xfile.close()
        return True
    else:
        #TODO 抛出异常
        raise Exception,'not img'
在 07-10-10,bakefish<yellowfool在gmail.com> 写道:
> 不行的,我试过,不是二进制的问题,似乎在windows平台下会自动加入'\r',你试着用这个代码下txt文件就能看到。
>
>
> 2007/10/10, Qiangning Hong <hongqn在gmail.com>:
> > On 10/10/07, bakefish <yellowfool在gmail.com> wrote:
> > >  localFile = open( url.split('/')[-1], 'w')
> >
> > 这个'w'改成'wb'
> >
> > --
> > Qiangning Hong
> > http://www.douban.com/people/hongqn/
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
-- 
Blog http://vicalloy.spaces.live.com/
老照片 http://www.lzpian.com/
2007年10月10日 星期三 14:19
лл¸÷룬ÎÊÌâ½â¾öÁË£¬È·ÊµÊÇ"w"¸ÄΪ"wb"¼´¿É
ÎÊÌâ³öÔÚÐ޸ĴúÂëºó£¬ÎÒÊÇÕâôÓõģ¬ÔÚeclipse+pydevµÄinterpreter»·¾³Àï
>>>del module_download
>>>import module_download
>>>module_download.download("http://.........")
ÎÒ·¢ÏÖ£¬ÕâʱimportÆäʵÊÇÎÞЧ£¬ËäÈ»delÁË£¬ÓõϹÊÇÐÞ¸ÄǰµÄ´úÂë
ÔÚ07-10-10£¬bakefish <yellowfool在gmail.com> дµÀ£º
>
> cookbook2ÖеÄ*Downloading a File from the Web*
>
> #!/usr/bin/env python
>
> """File downloading from the web.
> """
>
> def download(url):
> 	"""Copy the contents of a file from a given URL
> 	to a local file.
> 	"""
> 	import urllib
> 	webFile = urllib.urlopen(url)
> 	localFile = open(url.split('/')[-1], 'w')
> 	localFile.write(webFile.read())
> 	webFile.close()
> 	localFile.close()
>
> if __name__ == '__main__':
> 	import sys
> 	if len(sys.argv) == 2:
> 		try:
> 			download(sys.argv[1])
> 		except IOError:
> 			print 'Filename not found.'
> 	else:
> 		import os
> 		print 'usage: %s http://server.com/path/to/filename' % os.path.basename(sys.argv[0])
>
> linuxÏÂʹÓÃûÓÐÎÊÌ⣬µ«ÊÇÔÚwindowsÏÂʹÓã¬ÏÂÔØÏÂÀ´µÄÎļþÎÞ·¨Ê¹Óã¬ÄÄλÄܸø½²½²ÔÒòºÍ½â¾ö°ì·¨
>
>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071010/0ed2bf04/attachment.htm 
2007年10月10日 星期三 14:22
加 \r 就是 DOS 文本文件的德性, 重新导入模块用 reload module_download 在 07-10-10,bakefish<yellowfool at gmail.com> 写道: > 谢谢各位,问题解决了,确实是"w"改为"wb"即可
2007年10月10日 星期三 22:04
在 2007-10-10三的 13:45 +0800,bakefish写道:
> cookbook2中的Downloading a File from the Web 
> #!/usr/bin/env python
> 
> """File downloading from the web.
> """
> 
> def download(url):
> 	"""Copy the contents of a file from a given URL
> 	to a local file.
> 	"""
> 	import urllib
> 	webFile = urllib.urlopen(url)
> 	localFile = open(url.split('/')[-1], 'w')
> 	localFile.write(webFile.read())
> 	webFile.close()
> 	localFile.close()
> 
> if __name__ == '__main__':
> 	import sys
> 	if len(sys.argv) == 2:
> 		try:
> 			download(sys.argv[1])
> 		except IOError:
> 			print 'Filename not found.'
> 	else:
> 		import os
> 		print 'usage: %s http://server.com/path/to/filename' % os.path.basename(sys.argv[0])
> linux下使用没有问题,但是在windows下使用,下载下来的文件无法使用,哪位能给讲讲原因和解决办法
用urllib.urlretrieve多好呢
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20071010/813bb7ff/attachment.html 
Zeuux © 2025
京ICP备05028076号