1
Filter1=re.findall(r'<span (.*?)</span>',PageSource) 
Filter2=re.findall(r'<a href=.*title="(.*?)" >',PageSource) 
Filter3=re.findall(r'<span class=.*?<b>(.*?)</b>.*?',PageSource)

how to do it in 1 line code ...like this:

Filter=re.findall(r'  ',PageSource)

I tried this way:

Filter=re.findall(r'<span (.*?)</span>'+
                  r'<a href=.*title="(.*?)" >'+
                  r'<span class=.*?<b>(.*?)</b>.*?',PageSource)

But it is not working.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Nurul Akter Towhid
  • 3,046
  • 2
  • 33
  • 35

1 Answers1

2

How about using an HTML Parser instead?

Example, using BeautifulSoup:

from bs4 import BeautifulSoup

data = "your HTML here"
soup = BeautifulSoup(data)

span_texts = [span.text for span in soup.find_all('span')]
a_titles = [a['title'] for a in soup.find_all('a', title=True)]
b_texts = [b.text for b in soup.select('span[class] > b')]

result = span_texts + a_titles + b_texts

Demo:

>>> from bs4 import BeautifulSoup
>>> 
>>> data = """
... <div>
...     <span>Span's text</span>
...     <a title="A title">link</a>
...     <span class="test"><b>B's text</b></span>
... </div>
... """
>>> soup = BeautifulSoup(data)
>>> 
>>> span_texts = [span.text for span in soup.find_all('span')]
>>> a_titles = [a['title'] for a in soup.find_all('a', title=True)]
>>> b_texts = [b.text for b in soup.select('span[class] > b')]
>>> 
>>> result = span_texts + a_titles + b_texts
>>> print result
[u"Span's text", u"B's text", 'A title', u"B's text"]

Aside from that, your regular expressions are pretty different and serve different purposes - I would not try to squeeze unsqueezable, keep them separate and combine the results into a single list.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195