Closed AJaySi closed 2 months ago
import streamlit as st
from bs4 import BeautifulSoup
import requests
import spacy
nlp = spacy.load("en_core_web_sm") # Load a small English language model
st.title("Open Graph Tag Generator")
url = st.text_input("Enter URL:")
title = st.text_input("Override Title (optional):")
description = st.text_area("Override Description (optional):")
image_url = st.text_input("Override Image URL (optional):")
if st.button("Generate Open Graph Tags"):
if url:
try:
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
extracted_title = soup.find('title').text
extracted_description = soup.find('meta', attrs={'name': 'description'})['content'] if soup.find('meta', attrs={'name': 'description'}) else None
extracted_image_url = soup.find('meta', attrs={'property': 'og:image'})['content'] if soup.find('meta', attrs={'property': 'og:image'}) else None
except:
extracted_title = None
extracted_description = None
extracted_image_url = None
og_tags = """
<meta property="og:title" content="{title}">
<meta property="og:type" content="website">
<meta property="og:url" content="{url}">
<meta property="og:image" content="{image_url}">
<meta property="og:description" content="{description}">
""".format(
title=title or extracted_title,
url=url,
image_url=image_url or extracted_image_url,
description=description or extracted_description
)
st.code(og_tags, language='html')
else:
st.warning("Please enter a valid URL.")
Explanation:
streamlit
, BeautifulSoup
, requests
, and spacy
.st.title("Open Graph Tag Generator")
- Set the title of your app.st.text_input("Enter URL:")
- Create a text input field for the user to enter the URL.st.text_input("Override Title (optional):")
- Create a text input field for the user to optionally override the title.st.text_area("Override Description (optional):")
- Create a text area for the user to optionally override the description.st.text_input("Override Image URL (optional):")
- Create a text input field for the user to optionally override the image URL.st.button("Generate Open Graph Tags")
- Create a button that triggers the tag generation when clicked.if url:
- Check if the user has entered a URL.response = requests.get(url)
- Use requests
to get the HTML content of the URL.soup = BeautifulSoup(response.content, 'html.parser')
- Parse the HTML content with BeautifulSoup
.extracted_title = soup.find('title').text
- Try to extract the title from the HTML.extracted_description = soup.find('meta', attrs={'name': 'description'})['content']
- Try to extract the description from the HTML.extracted_image_url = soup.find('meta', attrs={'property': 'og:image'})['content']
- Try to extract the og:image
URL from the HTML.except:
- If any errors occur while fetching or parsing the content, set the extracted values to None
.format()
to create the HTML code for the OG tags, using the provided values or the extracted values.st.code(og_tags, language='html')
- Display the generated HTML code using st.code()
.st.warning("Please enter a valid URL.")
- Display a warning message if the user has not entered a valid URL.To run this code:
pip install streamlit beautifulsoup4 requests spacy en_core_web_sm
app.py
.streamlit run app.py
This will open the app in your web browser.
Enhancements:
st.image()
to display the extracted image in a preview.This Streamlit app will give you a good starting point for building your Open Graph tag generator. Feel free to customize and extend it further.
Committed changes.
AI tool generator for OpenGraph Tags. This will help in SEO of the social media posts.