Razeel
Razeel

Reputation: 47

Need to extract all links from script tag HTML Python

Basically i need to parse all src="" links from all <script> tags in HTML.

<script src="path/to/example.js" type="text/javascript"></script>

Unfortunately, bs4 cannot do that. Any ideas how can i achieve this?

Upvotes: 2

Views: 1861

Answers (2)

QHarr
QHarr

Reputation: 84465

I would condense and use script[src] to ensure script has src attribute

import requests
from bs4 import BeautifulSoup as bs
r = requests.get('http://example.com').content
soup = bs(r, 'lxml') # 'html.parser' if lxml not installed
srcs = [item['src'] for item in soup.select('script[src]')]

Upvotes: 1

Alex Hall
Alex Hall

Reputation: 36043

import requests
import bs4
text = requests.get('http://example.com').text
soup = bs4.BeautifulSoup(text, features='html.parser')
scripts = soup.find_all('script')
srcs = [link['src'] for link in scripts if 'src' in link.attrs]
print(srcs)

Upvotes: 4

Related Questions