[英]Need to extract all links from script tag HTML Python
Basically i need to parse all src="" links from all <script>
tags in HTML. 基本上,我需要解析HTML中所有<script>
标记中的所有src =“”链接。
<script src="path/to/example.js" type="text/javascript"></script>
Unfortunately, bs4 cannot do that. 不幸的是,bs4无法做到这一点。 Any ideas how can i achieve this? 任何想法我怎么能做到这一点?
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)
I would condense and use script[src]
to ensure script has src attribute 我会压缩并使用script[src]
以确保脚本具有src属性
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]')]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.