Search code examples
pythonbeautifulsoupmeta

BeautifulSoup not picking up meta tag


I have a simple script that gets an html page and tries to output the contents of the meta tag for keywords. Somehow, it is not picking up the content of the keywords meta tag even through the html contains the tag. Any help is appreciated.

    url = “https://www.mediapost.com/publications/article/316086/google-facebook-others-pitch-in-app-ads-brand-s.html”
    req = urllib2.Request(url=url)
    f = urllib2.urlopen(req)
    mycontent = f.read()
    soup = BeautifulSoup(mycontent, 'html.parser')
    keywords = soup.find("meta", property="keywords")
    print keywords

Solution

  • If you check it properly the meta tag you are looking for has attribute name not property so change your code to

    keywords = soup.find("meta", attrs={'name':'keywords'})
    

    and then to display the content you need to write

    print keywords['content']
    

    Output:

    Many more major brands are pumping big ad dollars into mobile games, pushing Google, Facebook and others into the in-app gaming ad space. Some believe this is in response to brands searching for a secure, safe place to run video ads and engage with consumers. 03/16/2018