有几种方法可以做到这一点。随着.find_all()
:
soup.find_all("a", href=re.compile(r"^/markets/NZSX/securities/"))
soup.find_all("a", href=lambda href: href and href.startswith("/markets/NZSX/securities/"))
或者,用CSS selector:
soup.select('a[href^="/markets/NZSX/securities/"]')
以上将检查该href
到下手/markets/NZSX/securities/
。如果你想申请“包含”检查,而不是:
soup.find_all("a", href=re.compile(r"/markets/NZSX/securities/"))
soup.find_all("a", href=lambda href: href and "/markets/NZSX/securities/" in href)
soup.select('a[href*="/markets/NZSX/securities/"]')
很好的答案!请注意,这假定希望href用*字符串开始*,而不是*包含*字符串作为OP提到的。 – taleinat
@taleinat谢谢,好点。改进了答案。 – alecxe
谢谢alexce。还有一件事;这返回完整的元素,即“ datavoredan