To download a Facebook video using Python, you can utilize the pytube
library, which is a lightweight library for downloading YouTube videos. Although pytube
is primarily designed for YouTube, it can also be used to download videos from other websites, including Facebook. Here’s an example of how you can use pytube
to download a Facebook video:
Install the pytube
library by running the following command:
pip install pytube
Create a Python script and import the necessary modules:
from pytube import YouTube
import requests
import re
Define a function to extract the video URL from the Facebook page:
def extract_video_url(fb_url):
response = requests.get(fb_url)
html_content = response.text
pattern = r'"source":"([^"]+)"'
matches = re.findall(pattern, html_content)
if matches:
return matches[0]
return None
Prompt the user to enter the Facebook video URL:
fb_url = input("Enter the Facebook video URL: ")
Extract the video URL using the extract_video_url
function:
video_url = extract_video_url(fb_url)
if video_url:
print("Video URL:", video_url)
else:
print("Failed to extract video URL.")
exit()
Use pytube
to download the video:
try:
youtube = YouTube(video_url)
video = youtube.streams.get_highest_resolution()
video.download()
print("Video downloaded successfully.")
except Exception as e:
print("Failed to download the video:", str(e))
That’s it! When you run the script, it will prompt you to enter the Facebook video URL. After entering the URL, the script will extract the video URL using the extract_video_url
function and then use pytube
to download the video in the highest resolution available.
Certainly! Here’s the complete code with the necessary steps to download a Facebook video using Python and the pytube
library:
from pytube import YouTube
import requests
import re
def extract_video_url(fb_url):
response = requests.get(fb_url)
html_content = response.text
pattern = r'"source":"([^"]+)"'
matches = re.findall(pattern, html_content)
if matches:
return matches[0]
return None
def download_facebook_video(fb_url):
video_url = extract_video_url(fb_url)
if video_url:
try:
youtube = YouTube(video_url)
video = youtube.streams.get_highest_resolution()
video.download()
print("Video downloaded successfully.")
except Exception as e:
print("Failed to download the video:", str(e))
else:
print("Failed to extract video URL.")
# Prompt the user to enter the Facebook video URL
fb_url = input("Enter the Facebook video URL: ")
# Download the video
download_facebook_video(fb_url)
To use this code, follow these steps:
- Install the
pytube
library by running the following command:
pip install pytube
- Copy the code into a Python script and run the script.
- When prompted, enter the URL of the Facebook video you want to download.
The script will extract the video URL using the extract_video_url
function, and then use pytube
to download the video in the highest available resolution. If the video is successfully downloaded, you will see the message “Video downloaded successfully.” If any error occurs during the process, an appropriate error message will be displayed.
Please note that downloading videos from Facebook may be subject to the platform’s terms of service or content usage restrictions. Ensure that you have the necessary permissions or rights to download the videos you intend to access.