如何用Selenium下载animefreak.tv视频?脚本运行时src属性不显示
src in Your AnimeFreak Scraper Hey there! I’ve run into this exact issue with video streaming sites before—they’re tricky because they rarely serve up video links directly in the initial HTML. Let’s walk through the most likely reasons your script isn’t finding the src attribute, plus actionable fixes to try:
1. The Video Source is Loaded Dynamically with JavaScript
Most modern streaming platforms (including AnimeFreak) don’t include the video src in the raw HTML you get from tools like requests.get(). Instead, they use JavaScript to load the video player and its source after the page finishes loading. Static scrapers can’t execute this JS, so they miss the video data entirely.
Fix: Use a Headless Browser to Render JavaScript
Tools like Selenium or Playwright simulate a real browser, letting the page’s JavaScript run and populate the video element. Here’s a quick Selenium example to get you started:
First, install Selenium and grab the ChromeDriver (or your browser’s equivalent driver):
pip install selenium
Then adjust your script to use a headless browser:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options import time # Mimic a real browser to avoid being flagged as a bot chrome_options = Options() chrome_options.add_argument("--headless=new") # Run without a visible window (remove to see browser actions) chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36") driver = webdriver.Chrome(options=chrome_options) target_url = "http://animefreak.tv/watch/hacklegend-twilight-bracelet-episode-1-english-dubbed-online-free" try: driver.get(target_url) time.sleep(3) # Give JS time to load the video player (adjust based on your internet speed) # Check if the video is embedded in an iframe (super common for streaming sites) iframes = driver.find_elements(By.TAG_NAME, "iframe") if iframes: driver.switch_to.frame(iframes[0]) # Switch to the first iframe containing the player # Grab the video element and its source video = driver.find_element(By.TAG_NAME, "video") video_src = video.get_attribute("src") if video_src: print(f"Found video source: {video_src}") else: # Some sites use `data-src` instead of `src` for lazy loading data_src = video.get_attribute("data-src") if data_src: print(f"Found lazy-loaded video source: {data_src}") else: print("No direct source found—you might need to inspect network requests next.") finally: driver.quit()
2. Anti-Scraping Measures Are Blocking Your Script
AnimeFreak might detect your script as a bot and return a stripped-down version of the page (without the video element) to block you.
Fix: Add Realistic Request Headers
If you’re using requests, make sure to include headers that mimic a real browser to avoid being flagged:
import requests from bs4 import BeautifulSoup headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36", "Referer": "http://animefreak.tv/" } response = requests.get(target_url, headers=headers) soup = BeautifulSoup(response.text, "html.parser") # Continue parsing with BeautifulSoup
Note: Even with headers, some sites might still block you. Using a proxy or rotating user agents can help, but always respect the site’s robots.txt and terms of service.
3. The Video Source is Hidden in Network Requests
Sometimes the video link is loaded via a separate API call, not directly in the video element. To find this:
- Open the target page in Chrome/Firefox.
- Open DevTools (F12) → Navigate to the Network tab.
- Filter for "Media" type requests. You’ll see the video file load here—copy its URL.
- Replicate this API call in your script (make sure to include any required cookies or headers from the original request).
Important Notes
- Respect Copyright & Rules: Scraping copyrighted content might violate AnimeFreak’s terms or local laws. Only download content for personal, offline use as allowed.
- Be Gentle: Don’t spam requests—add delays between calls to avoid getting your IP blocked.
内容的提问来源于stack exchange,提问作者Jeric

