【问题标题】:BeautifulSoup Issue: How to get the exact link by matching the exact tag content?BeautifulSoup 问题:如何通过匹配准确的标签内容得到准确的链接?
【发布时间】:2018-11-15 03:59:39
【问题描述】:

我想获取“S-1”之后的链接,而不是“S-1/A”之后的链接。我试过“.find_all(lambda tag: tag.name == 'td' and tag.get()==['S-1'])”,试过“.select('td.s-1')”,并未能获得链接。感谢您对此提供的任何帮助。

这里是相关的页面来源:

    <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>

以下是相关页面来源截图:

这里是整页源码的链接:

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

【问题讨论】:

    标签: python html hyperlink beautifulsoup


    【解决方案1】:

    试试这个:

    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)
    

    输出:

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

    【讨论】:

    • 非常感谢,伊莎拉!它适用于我与您分享的链接。
    • 但是,当我将相同的方法应用于其他链接时,我发现了一些问题。索引 [1] 随链接(公司)而变化。一些“S-1”表格被[1]索引,一些被[2]索引,一些被[3]索引,等等。我看到识别 S-1 表单的一种方法是完全匹配“S-1/ ”。你知道我是否可以通过匹配名称找到它们,比如“S-1”表格?或者,如果有帮助,“S-1”表格始终是最后一个链接。我找不到计算结果数量的方法。你有什么技巧吗?
    • 如果它始终是最后一个链接,那么请使用 links[1] 而不是 links[-1]。它返回列表链接的最后一个元素。
    • stackoverflow.com/questions/930397/… 在堆栈溢出中,您可能会找到许多问题的解决方案。这是我使用的索引规则的答案。
    • 知道了。很高兴我加入了堆栈溢出!
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签