请求是一个非常好的库.我想用它来下载大文件(> 1GB).问题是不可能将整个文件保存在内存中我需要以块的形式读取它.这是以下代码的问题
import requests
def DownloadFile(url)
local_filename = url.split('/')[-1]
r = requests.get(url)
f = open(local_filename, 'wb')
for chunk in r.iter_content(chunk_size=512 * 1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.close()
return
Run Code Online (Sandbox Code Playgroud)
由于某种原因它不起作用.在将其保存到文件之前,它仍会将响应加载到内存中.
UPDATE
如果你需要一个可以从FTP下载大文件的小客户端(Python 2.x /3.x),你可以在这里找到它.它支持多线程和重新连接(它确实监视连接),它还为下载任务调整套接字参数.
我需要通过Python中的http下载几个文件.
最明显的方法是使用urllib2:
import urllib2
u = urllib2.urlopen('http://server.com/file.html')
localFile = open('file.html', 'w')
localFile.write(u.read())
localFile.close()
Run Code Online (Sandbox Code Playgroud)
但我必须以某种方式处理那些令人讨厌的网址,比如说:http://server.com/!Run.aspx/someoddtext/somemore?id=121&m=pdf.通过浏览器下载时,该文件具有可读的名称,即.accounts.pdf.
有没有办法在python中处理它,所以我不需要知道文件名并将它们硬编码到我的脚本中?
import urllib2
website = "WEBSITE"
openwebsite = urllib2.urlopen(website)
html = getwebsite.read()
print html
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.
但我只希望纯文本HTML中的href链接.我怎么解决这个问题?
python有没有办法将整个html页面及其内容(图像,css)下载到给定URL的本地文件夹.并更新本地html文件以在本地选择内容.