retrieve content inside the meta tag python 1

retrieve content inside the meta tag python

title = soup.find("meta",  property="og:title")
url = soup.find("meta",  property="og:url")

print(title["content"] if title else "No meta title given")
print(url["content"] if url else "No meta url given")

Here is what the above code is Doing:
1. We’re using the requests library to get the HTML from the URL we want to scrape.
2. We’re using the BeautifulSoup library to parse the HTML.
3. We’re using the find method to find the first meta tag with the property attribute set to og:title.
4. We’re using the find method to find the first meta tag with the property attribute set to og:url.
5. We’re printing the content attribute of the meta tag if it exists, otherwise we’re printing a message saying that the meta tag wasn’t found.

Similar Posts