Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find URL in another URL?

Tags:

python

regex

Kinda tricky question about regexes. I have url of such a pattern:

http://www.domain.com/img?res=high&refurl=http://www.ahother_domain.com/page/&imgurl=http://www.one_more.com/static/images/mercedes.jpg&w=640&h=480

how can I extract imgurl value?

like image 769
oleg.foreigner Avatar asked Oct 15 '13 11:10

oleg.foreigner


2 Answers

Take a look at urlparse

http://docs.python.org/2/library/urlparse.html

You can easily split your URL into parameters and then exctract whatever you need.

Example:

import urlparse
url = "http://www.domain.com/img?res=high&refurl=http://www.ahother_domain.com/page/&imgurl=http://www.one_more.com/static/images/mercedes.jpg&w=640&h=480"
urlParams = urlparse.parse_qs(urlparse.urlparse(url).query)
urlInUrl = urlParams['imgurl']
print urlInUrl
like image 172
Dropout Avatar answered Nov 13 '22 10:11

Dropout


This solution asssumes that the imgurl param value is always followed by size params such as: &w=...:

import re
re.findall('imgurl=([^&]+)&', url)
like image 40
user278064 Avatar answered Nov 13 '22 08:11

user278064