如何使用BeautifulSoup查找指向特定域的页面中的所有链接?

Jua*_*nti 5 python beautifulsoup

如何使用BeautifulSoup查找指向特定域的页面中的所有链接?

vik*_*sit 8

使用SoupStrainer,

from BeautifulSoup import BeautifulSoup, SoupStrainer
import re

# Find all links
links = SoupStrainer('a')
[tag for tag in BeautifulSoup(doc, parseOnlyThese=links)]

linkstodomain = SoupStrainer('a', href=re.compile('example.com/'))
Run Code Online (Sandbox Code Playgroud)

编辑:官方文档中的修改示例.