从 BS4 获取网址链接.美丽的汤



我只想得到以https开头的href

 (some texts(type='bs4.BeautifulSoup')).find_all("a",href="https") can not get url links.

我正在制作爬行工具。

使用 css 属性 = 值选择器,并以 ^ 开头运算符。很确定是一个骗子,但不能很快找到一个好的例子。

 links = [link['href'] for link in soup.select('[href^='https'])]
您还可以

find_all 中使用正则表达式过滤 a 标签的 href 属性

soup.find_all('a',href=re.compile('^https'))

演示

from bs4 import BeautifulSoup
import re
html="""
<a href="https://www.google.com">Secure</a>
<a href="http://www.google.com">Not Secure</a>
"""
soup=BeautifulSoup(html,'html.parser')
print(soup.find_all('a',href=re.compile('^https')))

输出:

[<a href="https://www.google.com">Secure</a>]

文档:

关键字参数

作为过滤器的正则表达式

最新更新