T
T
The_Immortal2020-08-20 05:45:19
Python
The_Immortal, 2020-08-20 05:45:19

How to download a file with preinitialization?

I am parsing a site with pre-authorization through the requests library . I got to the moment of downloading the file from a link like https://somesite.org/download.php?id=433
The problem is that such a link requires preliminary initialization ( how, by the way, are such links called in English? ) - i.e. . First you access the link, and then the file itself is issued for download.

Was here , it didn't help, because local_filenamenot the file name itself is passed here, but a piece of the link (in my example it is "download.php? id=433").

Found this , however, it suggests using the urllib library , but the fact is that I work from under requestsand I don’t want to change to urllib (and requests are considered to be the standard, or something), and urllib , unfortunately, does not know anything about the session (cookies) created by the requestsurllib.request.urlretrieve(url, path_to_file) library and, accordingly, ca n’t download anything like this: because does not have access to the file.

Tell me, please, what are the solutions?

Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-08-20
@The_Immortal

this is how i download a torrent file from a famous tracker

with requests.Session() as session:
        cookies = dict(uid=uid, usess=usess)
        auth = dict(username=login, password=password)  # Данные в виде словаря, которые отправлятся в POST
        r = session.get(url, timeout=5)  # Получаем страницу с формой логина
        r.encoding = 'utf-8'
        session.post(url, auth)  # Отправляем данные в POST, в session записываются наши куки
        r = session.get(link, cookies=cookies, timeout=5)
        open('some_file_name', 'wb').write(r.content)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question