Reputation: 810
I have a source code of a webpage formatted like this:
<span class="l r positive-icon">
Turkish
</span>
<span>
The.Mist[2007]DvDrip[Eng]-aXXo
</span>
<span class="l r neutral-icon">
Vietnamese
</span>
<span>
The.Mist.2007.720p.Bluray.x264.YIFY
</span>
As you can see, there are either spans with the class of "l r positive-icon" or "l r neutral-icon". I want to get only the languages, so everything between the span with any class. I use this regexp but it gives me an empty list:
allLanguages = re.findall('<span class=".*">\s(.*)\s</span>', allLanguagesTags)
allLanguagesTags contains the source code shown above. Can anybody give me a hint?
Upvotes: 0
Views: 48
Reputation: 1121972
Don't use regular expressions. Use an actual HTML parser. I recommend you use BeautifulSoup instead:
from bs4 import BeautifulSoup
soup = BeautifulSoup(yourhtml)
languages = [s.get_text().strip() for s in soup.find_all('span', class_=True)]
Demo:
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('''\
... <span class="l r positive-icon">
... Turkish
... </span>
... <span>
... The.Mist[2007]DvDrip[Eng]-aXXo
... </span>
... <span class="l r neutral-icon">
... Vietnamese
... </span>
... <span>
... The.Mist.2007.720p.Bluray.x264.YIFY
... </span>
... ''')
>>> [s.get_text().strip() for s in soup.find_all('span', class_=True)]
[u'Turkish', u'Vietnamese']
Upvotes: 3