查看以下HTML代码:
<html>
<body>
<p class="fixedfonts">
<a href="A.pdf">LINK1</a>
</p>
<h2>Results</h2>
<p class="fixedfonts">
<a href="B.pdf">LINK2</a>
</p>
<p class="fixedfonts">
<a href="C.pdf">LINK3</a>
</p>
</body>
</html>
它包含3个链接。但是,我只需要检索标题Results
之后的链接
我正在将python与BeautifulSoup结合使用:
from bs4 import BeautifulSoup, SoupStrainer
# at this point html contains the code as string
# parse the HTML file
soup = BeautifulSoup(html.replace('\n', ''), parse_only=SoupStrainer('a'))
# kill all script and style elements
for script in soup(["script", "style"]):
script.extract() # rip it out
links = list()
for link in soup:
if link.has_attr('href'):
links.append(link['href'].replace('%20', ' '))
print(links)
通过提供的代码,我获得了文档中的所有链接,但正如我所说的,我只需要那些在Results
标记/标题之后的链接。你知道吗
感谢您的指导
将html数据分成两部分,在“结果”之前和之后,然后使用后面的一部分来处理它:
所以只要实现这一点:
您可以使用^{} method 来解决这个问题:
演示:
测试和似乎工作。你知道吗
相关问题 更多 >
编程相关推荐