Beautiful Soup if Class “Contains” or Regex?

后端 未结 3 759
离开以前
离开以前 2020-12-05 10:46

If my class names are constantly different say for example:

listing-col-line-3-11 dpt 41
listing-col-block-1-22 dpt 41
listing-col-line-4-13 CWK 12
         


        
3条回答
  •  失恋的感觉
    2020-12-05 11:13

    You could avoid regex by using partial matching with gazpacho...

    Input:

    html = """\
    
    A
    B
    C
    """

    Partial matching code:

    from gazpacho import Soup
    
    soup = Soup(html)
    divs = soup.find("div", {"class": "listing-col-"}, partial=True)
    [div.text for div in divs]
    

    Output:

    ['A', 'B', 'C']
    

提交回复
热议问题