[Python] how to select data from valid html tags in python?

Discussão em 'Python' iniciado por Stack, Janeiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    I have following text data in a text file. the text has some HTML like tags but not uniform , as you see in the example , there are multiple tags , there is only one valid tag pair and i want to extract the text between the valid tags .

    text = '''
    some text here <document> additional text here <document> The universe really seems to be expanding fast. Too fast, even. A new measurement confirms what previous—and highly debated—results had shown: The universe is expanding faster than predicted by theoretical models, and faster than can be explained by our current understanding of physics.</document>
    '''


    i tried using regular expression to see , if i can extract the text from valid set of document tags , but i get the entire contents of the file , when i print it out.

    any thoughts?

    import re

    with open('data.txt', 'r') as f:
    text = f.read()

    input = re.findall(r"<document>.*?</document>", text, re.DOTALL)

    for i in input :
    print(i)

    Continue reading...

Compartilhe esta Página