D
D
DzeronBug2017-04-16 06:46:07
Python
DzeronBug, 2017-04-16 06:46:07

How to get headers via session.post without downloading the whole file?

There is a file storage. They can be obtained from a link like 'site_name/id_file'. I do it like this:

import requests

session = requests.Session()
session.post('site_name/авторизация', {
     'remember': 1,
})

for num in range(1, 100):
    link = 'site_name/' + str(num)
    res = session.post(link)

    if res.status_code == 200:
       file_name = res.headers['Content-Disposition']
       fd = open(file_name, 'wb')
       fd.write(res.content)
       fd.close()

The bottom line is that according to the contents of the headers, I need to filter out files, for example, download pdf, but not zip. But at the moment of session.post the file is completely downloaded. Is it possible through this method to "feel" the file, ie. throw post and then cancel to just get the header? Like canceling the download of a file in the browser.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DzeronBug, 2017-04-16
@DzeronBug

I found the head method, which allows you to get headers without downloading.
res = session.head(url='site_name/' + str(num))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question