Search code examples
pythonbeautifulsoupmetadata

Python: Read multiple meta tag contents with the same property with BeautifulSoup?


Currently I'm pretty new with python and so I'm trying some things with BeautifulSoup.

My Problem is: I want to read the video tags of a youtube video over python. ex.

<meta property="og:video:tag" content="Official">
<meta property="og:video:tag" content="Trailer"> 
<meta property="og:video:tag" content="Movie">
<meta property="og:video:tag" content="Clip">

with:

yturl = soup.find("meta", {"property":"og:video:tag"})['content']

I can read just the first tag

and with:

yttag = soup.findAll("meta", {"property":"og:video:tag"})

everything after "og:video:tag" is listed and I just want the content. Is there any possibility to use BeautifulSoup that I can read multiple meta tag contents with the same name/property?


Solution

  • Using list comprehension:

    >>> html = '''
    ... <meta property="og:video:tag" content="Official">
    ... <meta property="og:video:tag" content="Trailer"> 
    ... <meta property="og:video:tag" content="Movie">
    ... <meta property="og:video:tag" content="Clip">
    ... '''
    >>> 
    >>> from bs4 import BeautifulSoup
    >>> soup = BeautifulSoup(html, 'lxml')
    >>> [tag['content'] for tag in soup.findAll("meta", {"property":"og:video:tag"})]
    ['Official', 'Trailer', 'Movie', 'Clip']
    # maps Tag elements to their content attributes