Get meta tag content property with BeautifulSoup and Python

后端 未结 4 1014
遇见更好的自我
遇见更好的自我 2021-01-30 16:43

I am trying to use python and beautiful soup to extract the content part of the tags below:


&l         


        
相关标签:
4条回答
  • 2021-01-30 16:46

    A way I like to solve this is as follows:
    (Is neater when using with lists of properties to look up...)

    title = soup.find("meta",  {"property":"og:title"})
    url = soup.find("meta",  {"property":"og:url"})
    
    # Using same method as above answer
    title = title["content"] if title else None
    url = url["content"] if url else None
    
    0 讨论(0)
  • 2021-01-30 16:50

    Provide the meta tag name as the first argument to find(). Then, use keyword arguments to check the specific attributes:

    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")
    

    The if/else checks here would be optional if you know that the title and url meta properties would always be present.

    0 讨论(0)
  • 2021-01-30 16:50

    You could grab the content inside the meta tag with gazpacho:

    from gazpacho import Soup
    
    html = """\
    <meta property="og:title" content="Super Fun Event 1" />
    <meta property="og:url" content="http://superfunevents.com/events/super-fun-event-1/" />
    """
    
    soup = Soup(html)
    soup.find("meta", {"property": "og:title"}).attrs['content']
    

    Which would output:

    'Super Fun Event 1'
    
    0 讨论(0)
  • 2021-01-30 16:56

    try this :

    soup = BeautifulSoup(webpage)
    for tag in soup.find_all("meta"):
        if tag.get("property", None) == "og:title":
            print tag.get("content", None)
        elif tag.get("property", None) == "og:url":
            print tag.get("content", None)
    
    0 讨论(0)
提交回复
热议问题