Extract Facebook Open Graph (OG) tags using Python

Python

To grab or extract OG (Open Graph) tags using Python, you can use the requests library to make an HTTP request to a website and then use the beautifulsoup4 library to parse the HTML and extract the OG tags. Here is an example of how you can do this:


import requests
from bs4 import BeautifulSoup

url = 'https://www.example.com'

response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

og_title = soup.find('meta', property='og:title')
og_description = soup.find('meta', property='og:description')
og_image = soup.find('meta', property='og:image')

print(og_title['content'])
print(og_description['content'])
print(og_image['content'])

This will print the content of the OG tags for og:title, og:description, and og:image, respectively. Note that this is just an example and you may need to adjust it depending on the specific website you are trying to extract OG tags from.