Test if an attribute is present in a tag in BeautifulSoup

后端 未结 6 815
天涯浪人
天涯浪人 2020-12-07 22:01

I would like to get all the

6条回答
  •  醉酒成梦
    2020-12-07 22:35

    You don't need any lambdas to filter by attribute, you can simply use some_attribute=True in find or find_all.

    script_tags = soup.find_all('script', some_attribute=True)
    
    # or
    
    script_tags = soup.find_all('script', {"some-data-attribute": True})
    

    Here are more examples with other approaches as well:

    soup = bs4.BeautifulSoup(html)
    
    # Find all with a specific attribute
    
    tags = soup.find_all(src=True)
    tags = soup.select("[src]")
    
    # Find all meta with either name or http-equiv attribute.
    
    soup.select("meta[name],meta[http-equiv]")
    
    # find any tags with any name or source attribute.
    
    soup.select("[name], [src]")
    
    # find first/any script with a src attribute.
    
    tag = soup.find('script', src=True)
    tag = soup.select_one("script[src]")
    
    # find all tags with a name attribute beginning with foo
    # or any src beginning with /path
    soup.select("[name^=foo], [src^=/path]")
    
    # find all tags with a name attribute that contains foo
    # or any src containing with whatever
    soup.select("[name*=foo], [src*=whatever]")
    
    # find all tags with a name attribute that endwith foo
    # or any src that ends with  whatever
    soup.select("[name$=foo], [src$=whatever]")
    

    You can also use regular expressions with find or find_all:

    import re
    # starting with
    soup.find_all("script", src=re.compile("^whatever"))
    # contains
    soup.find_all("script", src=re.compile("whatever"))
    # ends with 
    soup.find_all("script", src=re.compile("whatever$"))
    

提交回复
热议问题