How to Retrieve Content Inside a Meta Tag Using Python?

Estimated read time 2 min read

To retrieve the content inside a <meta> tag using Python, you can utilize a combination of libraries such as requests and BeautifulSoup. Here’s an example:

import requests
from bs4 import BeautifulSoup

url = ""

# Send a GET request to the URL
response = requests.get(url)

# Create a BeautifulSoup object to parse the HTML content
soup = BeautifulSoup(response.text, "html.parser")

# Find the meta tag using the "name" attribute
meta_tag = soup.find("meta", attrs={"name": "description"})

# Extract the content attribute of the meta tag
content = meta_tag.get("content")


In this example, we retrieve the HTML content from a URL using the requests.get() method. Then, we create a BeautifulSoup object by passing the HTML content and the parser library (“html.parser”).

Next, we use the find() method of the BeautifulSoup object to locate the <meta> tag with the desired attribute, in this case, “name”=”description”. You can modify the attribute or use other methods like find_all() to match specific criteria.

Once the desired <meta> tag is found, we extract the value of the “content” attribute using the get() method. This retrieves the content inside the <meta> tag.

Finally, we print the extracted content.

Note that you may need to install the requests and beautifulsoup4 libraries if you haven’t already (pip install requests beautifulsoup4). Additionally, adjust the URL and the attribute criteria as per your specific needs.

By using requests to fetch the HTML content and BeautifulSoup to parse it, you can retrieve the content inside a <meta> tag using Python.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply