Python: saving large web page to file

后端 未结 1 1094
一整个雨季
一整个雨季 2020-12-18 06:55

Let me start off by saying, I\'m not new to programming but am very new to python.

I\'ve written a program using urllib2 that requests a web page that I would then

相关标签:
1条回答
  • 2020-12-18 07:21

    I'd use a handy fileobject copier function provided by shutil module. It worked on my machine :)

    >>> import urllib2
    >>> import shutil
    >>> remote_fo = urllib2.urlopen('http://docs.python.org/library/shutil.html')
    >>> with open('bigfile', 'wb') as local_fo:
    ...     shutil.copyfileobj(remote_fo, local_fo)
    ... 
    >>> 
    

    UPDATE: You may want to pass the 3rd argument to copyfileobj that controls the size of internal buffer used to transfer bytes.

    UPDATE2: There's nothing fancy about shutil.copyfileobj. It simply reads a chunk of bytes from source file object and writes it the destination file object repeatedly until there's nothing more to read. Here's the actual source code of it that I grabbed from inside Python standard library:

    def copyfileobj(fsrc, fdst, length=16*1024):
        """copy data from file-like object fsrc to file-like object fdst"""
        while 1:
            buf = fsrc.read(length)
            if not buf:
                break
            fdst.write(buf)
    
    0 讨论(0)
提交回复
热议问题