Search code examples
pythonregexbeautifulsoupfinanceedgar

REGEX extract information from EDGAR SC-13 form


I am trying to extract information from the latest SEC EDGAR Schedule 13 forms filings.

Link of the filing as an example:

1) Saba Capital_27-Dec-2019_SC13

The information I am trying to extract (and the parts of the filing with the information)

1) Names of reporting persons: Saba Capital Management, L.P.

<p style="margin-bottom: 0pt;">NAME OF REPORTING PERSON</p>
<p style="margin-top: 0pt; margin-left: 18pt;">Saba Capital Management GP, LLC<br><br/>  

2) Name of issuer : WESTERN ASSET HIGH INCOME FUND II INC

<p style="text-align: center;"><b><font size="5"><u>WESTERN ASSET HIGH INCOME FUND II INC.</u></font><u><br/></u>(Name of Issuer)</b>

3) CUSIP Number: 95766J102 (managed to get)

<p style="text-align: center;"><b><u>95766J102<br/></u>(CUSIP Number)</b>   

4) Percentage of class represented by amount : 11.3% (managed to get)

<p style="margin-bottom: 0pt;">PERCENT OF CLASS REPRESENTED BY AMOUNT IN ROW (11)</p>
<p style="margin-top: 0pt; margin-left: 18pt;">11.3%<br><br/>

5) Date of Event Which requires filing of this statement: December 24, 2019

<p style="text-align: center;"><b><u>December 24, 2019<br/></u>(Date of Event Which Requires Filing of This Statement)</b> 

.

import requests 
import re
from bs4 import BeautifulSoup

page = requests.get('https://www.sec.gov/Archives/edgar/data/1058239/000106299319004848/formsc13da.htm')
soup = BeautifulSoup(page.text, 'xml')

## get CUSIP number
CUSIP = re.findall(r'[0-9]{3}[a-zA-Z0-9]{2}[a-zA-Z0-9*@#]{3}[0-9]', soup.text)

### get % 
regex = r"(?<=PERCENT OF CLASS|Percent of class)(.*)(?=%)"
percent = re.findall(r'\d+.\d+', re.search(regex, soup.text, re.DOTALL).group().split('%')[0])

How can I extract the 5 pieces of information from the filing? Thanks in advance


Solution

  • Try the following Code to get all the values.Using find() and css selector select_one()

    import requests
    import re
    from bs4 import BeautifulSoup
    
    page = requests.get('https://www.sec.gov/Archives/edgar/data/1058239/000106299319004848/formsc13da.htm')
    soup = BeautifulSoup(page.text, 'lxml')
    NameReportingperson=soup.find('p', text=re.compile('NAME OF REPORTING PERSON')).find_next('p').text.strip()
    print(NameReportingperson)
    NameOftheIssuer=soup.select_one('p:nth-child(7) > b u').text.strip()
    print(NameOftheIssuer)
    CUSIP=soup.select_one("p:nth-child(9) > b > u").text.strip()
    print(CUSIP)
    percentage=soup.find('p', text=re.compile('PERCENT OF CLASS REPRESENTED BY AMOUNT IN ROW')).find_next('p').text.strip()
    print(percentage)
    Dateof=soup.select_one("p:nth-child(11) > b > u").text.strip()
    print(Dateof)
    

    Output:

    Saba Capital Management, L.P.
    WESTERN ASSET HIGH INCOME FUND II INC.
    95766J102
    11.3%
    December 24, 2019
    

    UPDATED


    If you don't want to use position then try below one.

    import requests
    import re
    from bs4 import BeautifulSoup
    
    page = requests.get('https://www.sec.gov/Archives/edgar/data/1058239/000106299319004848/formsc13da.htm')
    soup = BeautifulSoup(page.text, 'lxml')
    NameReportingperson=soup.find('p', text=re.compile('NAME OF REPORTING PERSON')).find_next('p').text.strip()
    print(NameReportingperson)
    NameOftheIssuer=soup.select_one('p:contains(Issuer)').find_next('u').text.strip()
    print(NameOftheIssuer)
    CUSIP=soup.select_one('p:contains(CUSIP)').find_next('u').text.strip()
    print(CUSIP)
    percentage=soup.find('p', text=re.compile('PERCENT OF CLASS REPRESENTED BY AMOUNT IN ROW')).find_next('p').text.strip()
    print(percentage)
    Dateof=soup.select_one('p:contains(Event)').find_next('u').text.strip()
    print(Dateof)
    

    Output:

    Saba Capital Management, L.P.
    WESTERN ASSET HIGH INCOME FUND II INC.
    95766J102
    11.3%
    December 24, 2019
    

    Update 2:

    import requests
    import re
    from bs4 import BeautifulSoup
    page = requests.get('https://www.sec.gov/Archives/edgar/data/1058239/000106299319004848/formsc13da.htm')
    soup = BeautifulSoup(page.text, 'lxml')
    NameReportingperson=soup.find('p', text=re.compile('NAME OF REPORTING PERSON')).find_next('p').text.strip()
    print(NameReportingperson)
    NameOftheIssuer=soup.select_one('p:nth-of-type(7) > b u').text.strip()
    print(NameOftheIssuer)
    CUSIP=soup.select_one("p:nth-of-type(9) > b > u").text.strip()
    print(CUSIP)
    percentage=soup.find('p', text=re.compile('PERCENT OF CLASS REPRESENTED BY AMOUNT IN ROW')).find_next('p').text.strip()
    print(percentage)
    Dateof=soup.select_one("p:nth-of-type(11) > b > u").text.strip()
    print(Dateof)