Python - BeautifulSoup,在标签内获取标签

时间:2017-07-06 20:47:53

标签: python beautifulsoup

如何在代码中获取代码?

这里的td标签:

<td scope="row"><a href="/Archives/edgar/data/1085621/000108562117000004/actuacorp12312016.htm">actuacorp12312016.htm</a></td>

我想要href标签的值,主要是htm链接:

<a href="/Archives/edgar/data/1085621/000108562117000004/actuacorp12312016.htm">actuacorp12312016.htm</a>

我有这样的标签:

<tr>
<td scope="row">1</td>
<td scope="row">10-K</td>
<td scope="row"><a href="/Archives/edgar/data/1085621/000108562117000004/actuacorp12312016.htm">actuacorp12312016.htm</a></td>
<td scope="row">10-K</td>
<td scope="row">2724989</td>
</tr>
<tr class="blueRow">
<td scope="row">2</td>
<td scope="row">EXHIBIT 21.1</td>
<td scope="row"><a href="/Archives/edgar/data/1085621/000108562117000004/exhibit211q42016.htm">exhibit211q42016.htm</a></td>
<td scope="row">EX-21.1</td>
<td scope="row">21455</td>
</tr>
<tr>
<td scope="row">3</td>
<td scope="row">EXHIBIT 23.1</td>
<td scope="row"><a href="/Archives/edgar/data/1085621/000108562117000004/exhibit231q42016.htm">exhibit231q42016.htm</a></td>
<td scope="row">EX-23.1</td>
<td scope="row">4354</td>
</tr>

查看所有代码的代码:

base_url = "https://www.sec.gov/Archives/edgar/data/1085621/000108562117000004/" \
               "0001085621-17-000004-index.htm"
    response = requests.get(base_url)
    base_data = response.content
    base_soup = BeautifulSoup(base_data, "html.parser")

1 个答案:

答案 0 :(得分:1)

您可以使用find_all首先获取所有td代码,然后在这些代码中搜索锚点:

links = []
for tag in base_soup.find_all('td', {'scope' : 'row'}):
    for anchor in tag.find_all('a'):
        links.append(anchor['href'])

print(links)

输出:

['/Archives/edgar/data/1085621/000108562117000004/actuacorp12312016.htm',
 '/Archives/edgar/data/1085621/000108562117000004/exhibit211q42016.htm',
...
 '/Archives/edgar/data/1085621/000108562117000004/acta-20161231_lab.xml',
 '/Archives/edgar/data/1085621/000108562117000004/acta-20161231_pre.xml']

如果需要,您可以编写一个小过滤器来删除这些非htm链接:

filtered_links = list(filter(lambda x: x.endswith('.htm'), links))

要获得第一个链接,这里的版本略有不同,适合您的用例。

link = None
for tag in base_soup.find_all('td', {'scope' : 'row'}):
    children = tag.findChildren()
    if len(children) > 0:
        try:
            link = children[0]['href']
            break
        except:
            continue

print(link)

打印出'/Archives/edgar/data/1085621/000108562117000004/acta-20161231_pre.xml'