John
John

Reputation: 1594

Extract All urls from a string in python

Given a string of text which could possibly contain multiple urls all starting with http://

for example:

someString = "Text amongst words and links http://www.text.com more text more text another http http://www.word.com"

How can I extract all the urls from a string like the one above?

Leaving just

http://www.text.com

http://www.word.com

Upvotes: 0

Views: 699

Answers (2)

linuxfan
linuxfan

Reputation: 1160

This should work:

>>> for url in re.findall('(http://\S+)', someString): print url
... 
http://www.text.com
http://www.word.com

Upvotes: 1

cdvv7788
cdvv7788

Reputation: 2089

You want regular expressions.

In python: https://docs.python.org/2/library/re.html

Regular expression to evaluate: http://daringfireball.net/2010/07/improved_regex_for_matching_urls

Shouldn't take you long from there

Upvotes: 1

Related Questions