首页 > 解决方案 > 使用 python 网页抓取下载 iframe 内容

问题描述

https://anime-world.in/?trembed=0&trid=1930&trtype=2这个链接只包含一个 iframe 标签,我想下载其中链接的视频。但是, src 属性将我带到一个无法访问的页面。我想知道我怎样才能下载视频。我已经尝试访问源站点(错误 403),并且我还尝试在网络面板中查找,在那里找不到任何东西。谢谢您的帮助! 在此处输入图像描述 在此处输入图像描述

标签: pythonweb-scrapingbeautifulsoup

解决方案


您可以使用此脚本如何从 URL 下载视频:

import re
import requests
from bs4 import BeautifulSoup

url = "https://anime-world.in/?trembed=0&trid=1930&trtype=2"

headers = {
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0",
    "Referer": "https://anime-world.in/",
}

with requests.session() as s:
    soup = BeautifulSoup(s.get(url, headers=headers).content, "html.parser")
    url2 = soup.iframe["src"]
    html_doc = s.get(url2, headers=headers).text
    link = re.search(r'file:".*?(http[^",]+)', html_doc).group(1)
    print(link)

    with open("file.mp4", "wb") as f:
        f.write(s.get(link, headers=headers, verify=False).content)

    print("Done.")

这打印:

https://6-yt5mQW3xieQHnfPa.server1cdn.xyz/link/AWI165B50FD/360/bb55f30227f8db11dcd19fef59fd6e5f/?sid=f3439234305d48ff7424333b2ca2de77

Done.

并与视频一起保存file.mp4(下载需要一点时间。)


推荐阅读