Regex within html tags

前端 未结 5 572
孤独总比滥情好
孤独总比滥情好 2021-01-24 10:19

I would like to parse the HD price from the following snipper of HTML. I am only have fragments of the html code, so I cannot use an HTML parser for this.



        
5条回答
  •  Happy的楠姐
    2021-01-24 10:38

    BeautifulSoup is very lenient to the HTML it parses, you can use it for the chunks/parts of HTML too:

    # -*- coding: utf-8 -*-
    from bs4 import BeautifulSoup
    
    data = u"""
    
    View In iTunes £19.99
    • HD Version
    • """ soup = BeautifulSoup(data) print soup.find('span', class_='price').text[1:]

    Prints:

    19.99
    

提交回复
热议问题