Beautifulsoup: Find all stylesheet links

Syntax

soup.find_all('link', rel="stylesheet")

Find all stylesheet links example

from bs4 import BeautifulSoup

html = """
<html lang="">
<head>
     
      <link rel="icon" type="image/png" href="/static/img/logo.png">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
      <link rel="preload" as="style" href="/static/css/prism.css">
      <link rel="stylesheet" type="text/css" href="/static/css/prism.css" />
      <link rel="stylesheet" type="text/css" href="/static/css/bootstrap.min.css" />
      <link rel="stylesheet" type="text/css" href="/static/css/main.css" />
      <link rel="preload" as="style" href="https://fonts.googleapis.com/css2?family=Coda+Caption:wght@800&display=swap">
      <link href="https://fonts.googleapis.com/css2?family=Coda+Caption:wght@800&display=swap" rel="stylesheet">
         
<body>
"""

soup = BeautifulSoup(html, 'html.parser')
#Find all stylesheet links
stylesheets = soup.find_all('link', rel="stylesheet")
#print
print(stylesheets)

Output:

[<link href="/static/css/prism.css" rel="stylesheet" type="text/css">
<link href="/static/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<link href="/static/css/main.css" rel="stylesheet" type="text/css"/>
<link as="style" href="https://fonts.googleapis.com/css2?family=Coda+Caption:wght@800&amp;display=swap" rel="preload"/>
<link href="https://fonts.googleapis.com/css2?family=Coda+Caption:wght@800&amp;display=swap" rel="stylesheet"/>
<body>
</body></link></link>, <link href="/static/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<link href="/static/css/main.css" rel="stylesheet" type="text/css"/>
<link as="style" href="https://fonts.googleapis.com/css2?family=Coda+Caption:wght@800&amp;display=swap" rel="preload"/>
<link href="https://fonts.googleapis.com/css2?family=Coda+Caption:wght@800&amp;display=swap" rel="stylesheet"/>
<body>
</body></link>, <link href="/static/css/main.css" rel="stylesheet" type="text/css"/>, <link href="https://fonts.googleapis.com/css2?family=Coda+Caption:wght@800&amp;display=swap" rel="stylesheet"/>]

 

Print href of the link:

for style in stylesheets:
    print(style['href'])

 

Output:

//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/styles/default.min.css
/static/css/bootstrap.min.css
/static/css/main.css
https://fonts.googleapis.com/css2?family=Coda+Caption:wght@800&display=swap