import re import requests from bs4 import BeautifulSoup # pattern = re.compile(r'!\[.*?\]\(.*?\)') #pattern = re.compile(r'!\[.*?\]\(.*?\)') linkmatches = re.compile(r'\b(?:https?://|www\.)\S+\b') test_string = "https://tenor.com/view/i-love-you-iloveyou-i-love-it-love-you-love-u-gif-1804813945664624577" links = linkmatches.findall(test_string) for match in links: response = requests.get(match) soup = BeautifulSoup(response.text, 'html.parser') gif_title = soup.find('meta', property='og:title')['content'] gif_url = soup.find('meta', property='og:image')['content'] test_string = test_string.replace(match, "") test_string += f"![{gif_title}]({gif_url})" print(test_string)