使用正则表达式提取URL链接重新匹配字符串 - Python

Ete*_*ity 2 python string url extraction matching

我一直在尝试使用re api从文本文件中提取URL.任何以http://,https://和www开头的链接.

该文件包含文本以及html源代码,html部分很容易,因为我可以使用BeautifulSoup提取它们,但普通文本似乎更具挑战性.我在网上发现这似乎是URL提取的最佳实现,但它在某些标签上失败,特别是它无法处理标签并将它们包含在URL中.感谢任何帮助,因为我自己并不熟悉字符串匹配

这是签名

sp1=re.findall("http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+", str(STRING))
sp2=re.findall('www.(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', str(STRING))
Run Code Online (Sandbox Code Playgroud)

例子:

http://www.website.com/science/</span></a><o:p></o:p></span></div><div
www.website.com/library/</span></a></span></i><span
http://awebsite.com/Groups</a><div>
Run Code Online (Sandbox Code Playgroud)

Joe*_*ett 7

re.findall(r'https?://[^\s<>"]+|www\.[^\s<>"]+', str(STRING))
Run Code Online (Sandbox Code Playgroud)

[^\s<>"]+部分匹配任何非空格,非引号,非角度布局字符,以避免匹配字符串,如:

<a href="http://www.example.com/stuff">
http://www.example.com/stuff</br>
Run Code Online (Sandbox Code Playgroud)