Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to get data between tags of xml or html files in python?

Tags:

python

html

xml

I am using Python and need to find and retrieve all character data between tags:

<tag>I need this stuff</tag>

I then want to output the found data to another file. I am just looking for a very easy and efficient way to do this.

If you can post a quick code snippet to portray the ease of use. Because I am having a bit of trouble understanding the parsers.

like image 681
Recursion Avatar asked Dec 14 '22 00:12

Recursion


1 Answers

without external modules, eg

>>> myhtml = """ <tag>I need this stuff</tag>
... blah blah
... <tag>I need this stuff too
... </tag>
... blah blah """
>>> for item in myhtml.split("</tag>"):
...   if "<tag>" in item:
...       print item [ item.find("<tag>")+len("<tag>") : ]
...
I need this stuff
I need this stuff too
like image 167
ghostdog74 Avatar answered May 03 '23 12:05

ghostdog74