2024-04-25 23:19:45 发布
网友
关于正则表达式有点棘手。我有这样一个模式的网址:
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
如何提取imgurl值?你知道吗
imgurl
看看urlparse
urlparse
http://docs.python.org/2/library/urlparse.html
你可以很容易地把你的URL分成参数,然后把你需要的任何东西都删掉。你知道吗
示例:
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
此解决方案假定imgurl参数值后面始终跟有大小参数,例如:&w=...:
&w=...
import re re.findall('imgurl=([^&]+)&', url)
看看
urlparse
http://docs.python.org/2/library/urlparse.html
你可以很容易地把你的URL分成参数,然后把你需要的任何东西都删掉。你知道吗
示例:
此解决方案假定
imgurl
参数值后面始终跟有大小参数,例如:&w=...
:相关问题 更多 >
编程相关推荐