Lina
Lina

Reputation: 33

BeautifulSoup Issue: How to get the exact link by matching the exact tag content?

I want to get the link that is after "S-1", instead of the one after "S-1/A". I tried ".find_all(lambda tag: tag.name == 'td' and tag.get()==['S-1'])", tried ".select('td.s-1')", and failed to get the link. I appreciate any help on it.

Here is the relevant page source:

    <tr>
        <td>ADVANCE FINANCIAL BANCORP</td>
        <td>S-1/A</td>
        <td>10/31/1996</td>
        <td><a id="two_column_main_content_rpt_filings_fil_view_0" href="/markets/ipos/filing.ashx?filingid=1567309" target="_blank">Filing</a>
        </td>
    </tr>

    <tr>
        <td>ADVANCE FINANCIAL BANCORP</td>
        <td>S-1</td>
        <td>9/27/1996</td>
        <td><a id="two_column_main_content_rpt_filings_fil_view_1" href="/markets/ipos/filing.ashx?filingid=921318" target="_blank">Filing</a>
        </td>
    </tr>

Here is the screenshot of relevant page source:

Relevant Page Source

Here is the link of the full page source:

https://www.nasdaq.com/markets/ipos/company/advance-financial-bancorp-5492-13046?tab=financials

Upvotes: 3

Views: 60

Answers (1)

Ishara Dayarathna
Ishara Dayarathna

Reputation: 3601

Try this:

from bs4 import BeautifulSoup
import requests    

def getlink(url):
    response = requests.get(url)
    mainpage = BeautifulSoup(response.text, 'html5lib')
    table = mainpage.findAll('table', attrs={"class": "marginB10px"})
    links = table[1].findAll('a')
    return links[1].get('href')    

link = getlink('https://www.nasdaq.com/markets/ipos/company/advance-financial-bancorp-5492-13046?tab=financials')
mainlink = 'https://www.nasdaq.com'
link = mainlink + link
print(link)

output:

https://www.nasdaq.com/markets/ipos/filing.ashx?filingid=921318

Upvotes: 1

Related Questions