1 回答
TA贡献1786条经验 获得超13个赞
如果您查看 HTML 代码,您会看到有</p>before </blockquote></blockquote>。这意味着您的变量rounds不包含您想要的链接。<a>在此标签后搜索下一个<p>:
from bs4 import BeautifulSoup
txt = '''
<p align="left"><strong><tt>
some text:</tt></strong><tt> (8/4)</tt><a href="some link"><tt>some other text</tt></a><tt>, (9/4)</tt><a href="some other link"><tt><br/>
some text:</tt></strong><tt>, (19/6)</tt><!--a href="some link in comment"--><tt>text after comment</tt></p></blockquote></blockquote><tt>, </tt><a href="link i want"><tt>text i want</tt></a><strong><tt><br/>
...
</p>
'''
soup = BeautifulSoup(txt, 'html.parser')
matched_link = soup.select_one('p[align="left"] ~ a')
print(matched_link)
印刷:
<a href="link i want"><tt>text i want</tt></a>
添加回答
举报