要查找超链接,可以使用BeautifulSoup库中的find_all()方法,并指定标签名称为"a",如下所示:

from bs4 import BeautifulSoup

html = """
<html>
<head></head>
<body>
<a href="https://www.google.com">Google</a>
<a href="https://www.facebook.com">Facebook</a>
<a href="https://www.twitter.com">Twitter</a>
</body>
</html>
"""

soup = BeautifulSoup(html, 'html.parser')
links = soup.find_all('a')

for link in links:
    print(link.get('href'))

运行以上代码,将会输出超链接的地址:

https://www.google.com
https://www.facebook.com
https://www.twitter.com
``
py soup 查找超链接

原文地址: http://www.cveoy.top/t/topic/iQ8h 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录