简体   繁体   English

如何从 python 中的 html 脚本中提取链接?

[英]How to extract link from html script in python?

How can I extract the URL from a script of HTML with Python?如何从带有 Python 的 HTML 的脚本中提取 URL?
The HTML provided: HTML 提供:


function download() {
                window.open('https:somelink.com');
        }
        const text = `<div style=\'position: relative;padding-bottom: 56.25%;height: 0;overflow: hidden;\'>
<iframe allowfullscreen=\'allowfullscreen\' src=\'URL\' style=\'border: 0;height: 100%;left: 0;position: absolute;top: 0;width: 100%;\' ></iframe>
</div>`;

function embed() {
                var element = document.getElementById('embed-text');
                console.log(element);
                element.innerHTML = text

        }

Desired output will be:所需的 output 将是:

https://somelink.com

Any help will do.任何帮助都可以。 Thanks!谢谢!

You should use regex like this:你应该像这样使用正则表达式:

 var urlRegex = /(https?:\/\/[^\s]+)/; // the regex // your string var input = "<div style=\'position: relative;padding-bottom: 56.25%;height: 0;overflow: hidden;\'><iframe allowfullscreen=\'allowfullscreen\' src=\" https://my-url.com/test \" style=\'border: 0;height: 100%;left: 0;position: absolute;top: 0;width: 100%;\' ></iframe></div>"; console.log(input.match(urlRegex)[1]); // use regex and lot result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM