擅长:python、mysql、java
<p>尝试使用<a href="https://docs.python.org/2/library/urlparse.html" rel="nofollow">urlparse</a>失败后,获得所需信息的最佳方法似乎是使用正则表达式:</p>
<pre><code>import urlparse
import re
urls = [
"http://hostname.com/as/ck$st=fa+gw+hw+ek+ei/",
"http://hostname.com/wqs/ck$st=fasd+/",
"http://hostname.com/as/ck$st=fa+gq+hf+kg+is&sadfnlslkdfn&gl+jh+ke+oj+kp sfav"
]
for myurl in urls:
parsed = urlparse.urlparse(myurl)
print 'scheme :', parsed.scheme
print 'netloc :', parsed.netloc
print 'path :', parsed.path
print 'params :', parsed.params
print 'query :', parsed.query
print 'fragment:', parsed.fragment
print 'username:', parsed.username
print 'password:', parsed.password
print 'hostname:', parsed.hostname, '(netloc in lower case)'
print 'port :', parsed.port
print urlparse.parse_qs(parsed.query)
print re.findall(r'([\w\+]+\+[\w\+]*)(?:[^\w\+]|$)', parsed.path)
print '-' * 80
</code></pre>