下载前获取文件名

时间:2016-12-07 11:33:23

标签: python

我有这样的事情:

demo_id = 'demoid=25044'

download_link = "http://www.hltv.org/interfaces/download.php?"+demo_id

print download_link

with open(demo_id+'.rar', 'wb') as handle:
    response = requests.get(download_link, stream=True)

    print response.headers['Content-Type']

    if not response.ok:
    # Something went wrong
        print "fcuk"

    for block in response.iter_content(1024):
        handle.write(block)

如果我到链接: http://www.hltv.org/interfaces/download.php?demoid=25044 然后它会提示下载和文件名。 无论如何都要检索该文件名?

1 个答案:

答案 0 :(得分:0)

以下代码将获取文件名而不下载此情况的文件:

import requests
import os

demo_id = 'demoid=25044'

download_link = "http://www.hltv.org/interfaces/download.php?"+demo_id
downloadheaders =  requests.head(download_link)
header = downloadheaders.headers['location']
name = header.rsplit('/', 1)[-1]
print name