R
R
Russian Federation2016-03-23 01:45:26
Python
Russian Federation, 2016-03-23 01:45:26

How to check if a file is downloaded from a link?

The task is this.
There is a list of links.
When you click on some of them, the file starts to load.
But how can you filter out these links (that is, determine whether the file will be loaded or not)?
It is desirable to code in Python or a link where to read about it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nirvimel, 2016-03-23
@afydoz

Among the HTTP response headers is Content-Type, which takes the value of one of the MIME types . The value text/htmlcorresponds to the HTML page and the value application/octet-streamcorresponds to the file being uploaded.

import requests

if __name__ == "__main__":
    r = requests.get('https://toster.ru/q/303883')
    r.raise_for_status()
    mime_type = r.headers['content-type']
    if mime_type == 'application/octet-stream':
        print('It is a binary file')
    if mime_type.startswith('text/'):
        print('It is a text')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question