如何使用python将文本中的url提取出来?


使用python,比如有个字符串:

myString = "This is my tweet check it out http://tinyurl.com/blah"

请问,如何才能将 http://tinyurl.com/blah 提取出来?

python 开发 url提取 url

jmkkxx 9 years, 2 months ago

一般都是使用正则表达式来处理,这里提供两段代码供参考:

import re
myString = "This is my tweet check it out http://tinyurl.com/blah"
print re.search("(?P<url>https?://[^\s]+)", myString).group("url")

或者,查找所有的url

import re
myString = "This is my tweet check it out http://tinyurl.com/blah"
print re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', myString)
兄贵都要死 answered 9 years, 2 months ago

Your Answer