How to perform regex using Python but with a slight twist?

Viewed 62

Let's say I have text file that contains (acquired from a page source):

    Manager:
<a href="/name/nm0001392/?ref_=adv_li_dr_0"
>Mark Smith</a>
                 <span class="ghost">|</span> 

    Managers:
<a href="/name/nm0905154/?ref_=adv_li_dr_0"
>Herb Dean</a>, 
<a href="/name/nm0905152/?ref_=adv_li_dr_1"
>Jon Anik</a>
                 <span class="ghost">|</span>

    Manager:
<a href="/name/nm0001392/?ref_=adv_li_dr_0"
>Dominick Cruz</a>
                 <span class="ghost">|</span>

I open the file with:

with open('managers.txt', 'r') as file:
    data = file.read().replace('\n', '')

Using regex (or another way if easier, but regex is mainly what I've been working with), how can I get the end result to be:

result = ["Mark Smith", "Herb Dean,Jon Anik", "Dominick Cruz"]

so that when it's "Managers", they are placed in the same string in the list, as shown in the example above.

I've tried the following without succeeding with what I want:

manager_list= re.findall(r'Manager:<a href=(.*?)</a>', data)

Thank you for any kind of help!

1 Answers

You can use BeautifulSoup with itertools.groupby to group the name blocks on the span.ghost spacers:

import itertools as it
from bs4 import BeautifulSoup as soup
html = '''    Manager:
<a href="/name/nm0001392/?ref_=adv_li_dr_0"
>Mark Smith</a>
                 <span class="ghost">|</span> 

    Managers:
<a href="/name/nm0905154/?ref_=adv_li_dr_0"
>Herb Dean</a>, 
<a href="/name/nm0905152/?ref_=adv_li_dr_1"
>Jon Anik</a>
                 <span class="ghost">|</span>

    Manager:
<a href="/name/nm0001392/?ref_=adv_li_dr_0"
>Dominick Cruz</a>
                 <span class="ghost">|</span>
'''
vals = [i.text for i in soup(html, 'html.parser').select(':is(a, span.ghost)')]
r = [','.join(b) for a, b in it.groupby(vals, key=lambda x:x == '|') if not a]

Output:

['Mark Smith', 'Herb Dean,Jon Anik', 'Dominick Cruz']
Related