无法通过请求下载内容

问题描述 投票:0回答:1

我正在尝试使用 Python requests 模块从付费专区后面的网站下载 mp4 视频。这是我的代码:

link_href = # Found the mp4 link on the website
with open('D:/filename','wb') as f:
    response = requests.get(link_href)
    f.write(response.content)

我查看了

response.content
,这是网站登录页面的 html。我如何获得 mp4?

python authentication python-requests http-get
1个回答
0
投票

好的,看来您正在尝试从需要先登录的网站下载 mp4 视频。由于您未在请求中提供任何登录凭据,因此会返回网站的登录页面 HTML。要通过付费专区,您需要使用正确的登录程序对自己进行身份验证,例如使用您的登录凭据向登录端点发送请求。

下面,我将向您展示如何使用解决方案的请求模块示例来处理这种情况的概述:

    import requests
from bs4 import BeautifulSoup

# Change these variables with your actual login information
login_url = "https://www.example.com/login"
username = "your_username"
password = "your_password"
mp4_link = "https://www.example.com/video.mp4"  # Found the mp4 link on the website

# Step 1: Visit the login page
with requests.Session() as session:
    login_page_response = session.get(login_url)

# Step 2: Parse the login page to get CSRF token or other required information (if necessary)
soup = BeautifulSoup(login_page_response.content, "html.parser")
csrf_token = soup.find("input", {"name": "_csrf"})["value"]  # Example of getting CSRF token

# Step 3: Prepare the login data
login_data = {
    "username": username,
    "password": password,
    "_csrf": csrf_token,
}

# Step 4: Send a POST request to the login endpoint with the login data
login_response = session.post(login_url, data=login_data)

# Step 5: Verify if the login is successful by checking the response
# (e.g., by checking if the redirected URL is the expected page after login)

# Step 6: Download the mp4 file
if login_response.status_code == 200:
    video_response = session.get(mp4_link)
    if video_response.status_code == 200:
        with open('D:/filename.mp4','wb') as f:
            f.write(video_response.content)

您可以通过上面的内容了解如何修复您的代码,如果有任何错误,请回复我

© www.soinside.com 2019 - 2024. All rights reserved.