【问题标题】:Open URL using python requests only then proceed to download file仅使用 python 请求打开 URL 然后继续下载文件
【发布时间】:2019-02-14 07:52:43
【问题描述】:

我正在尝试使用 Python 的请求模块从网站下载文件。

但是,仅当直接从下载页面单击下载链接时,该站点才允许我下载文件。 因此,使用请求,我尝试首先使用requests.get() 访问下载页面的 URL,然后继续下载文件。但不幸的是,这似乎不起作用。要求我首先打开下载页面的文本被写入 file.torrent"

import requests

def download(username, password):
    with requests.Session() as session:
        session.post('https://website.net/forum/login.php', data={'login_username': username, 'login_password': password})

        # Download page URL
        requests.get('https://website.net/forum/viewtopic.php?t=2508126')

        # The download URL itself
        response = requests.get('https://website.net/forum/dl.php?t=2508126')

        with open('file.torrent', 'wb') as f:
            f.write(response.content)

download(username='XXXXX', password='YYYYY')

直接从下载页面下载时的响应(有效):

General :
Request URL: https://website.net/forum/dl.php?t=2508126
Request Method: GET
Status Code: 200 OK
Remote Address: 185.37.128.136:443
Referrer Policy: no-referrer-when-downgrade

Response Headers :
Cache-Control: no-store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Content-Disposition: attachment; filename="[website.net].t2508126.torrent"
Content-Length: 33641
Content-Type: application/x-bittorrent; name="[website.net].t2508126.torrent"
Date: Thu, 14 Feb 2019 07:57:08 GMT
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Last-Modified: Thu, 14 Feb 2019 07:57:09 GMT
Pragma: no-cache
Server: nginx
Set-Cookie: bb_dl=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/forum/; domain=.website.net

Request Headers :
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
Cookie: bb_t=a%3A3%3A%7Bi%3A2507902%3Bi%3A1550052944%3Bi%3A2508011%3Bi%3A1550120230%3Bi%3A2508126%3Bi%3A1550125516%3B%7D; bb_data=1-27969311-wXVPJGcedLE1I2mM9H0u-3106784170-1550128652-1550131012-3061288864-1; bb_dl=2508126
Host: website.net
Referer: https://website.net/forum/viewtopic.php?t=2508126
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3701.0 Safari/537.36

Query String Parameters :
t: 2508126

自行打开下载链接时的响应(不起作用):

General :
Request URL: https://website.net/forum/dl.php?t=2508126
Request Method: GET
Status Code: 200 OK
Remote Address: 185.37.128.136:443
Referrer Policy: no-referrer-when-downgrade

Response Headers :
Cache-Control: no-store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Content-Type: text/html; charset=windows-1251
Date: Thu, 14 Feb 2019 08:03:29 GMT
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Last-Modified: Thu, 14 Feb 2019 08:03:29 GMT
Pragma: no-cache
Server: nginx
Transfer-Encoding: chunked

Request Headers :
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
Cookie: bb_t=a%3A3%3A%7Bi%3A2507902%3Bi%3A1550052944%3Bi%3A2508011%3Bi%3A1550120230%3Bi%3A2508126%3Bi%3A1550125516%3B%7D; bb_data=1-27969311-wXVPJGcedLE1I2mM9H0u-3106784170-1550128652-1550131390-3061288864-1
Host: website.net
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3701.0 Safari/537.36

Query String Parameters :
t: 2508126

【问题讨论】:

  • 点击链接的请求有什么特别之处?检查您的浏览器开发工具以查看请求的详细信息。
  • @Lutz 感谢您的回复,我已经编辑了我的问题以包含回复。
  • 请记住在完成 Lutz Horn 所说的操作后更新问题或发布您自己的答案。
  • 尝试将Referer: https://website.net/forum/viewtopic.php?t=2508126 标头添加到您的请求中。 (只是猜测)
  • 我认为是因为 cookie!

标签: python python-requests


【解决方案1】:

这对我有用:

data={'login_username': username, 'login_password': password, 'login': ''}

并使用session.get() 而不是requests.get()

【讨论】:

    猜你喜欢
    • 2019-12-24
    • 1970-01-01
    • 2016-03-05
    • 2013-05-07
    • 2021-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多