开发者

Automatically create valid links

I'm new to python, so please bear with me :)

I was wondering if there's any built-in way in python to append variables to URL's regardless of it's structure.

I 开发者_运维技巧would like to have a URL variable (test=1) added to an URL which could have any of the following structures

http://www.aaa.com (would simply add "/?test=1") to the end

http://www.aaa.com/home (like the one above, would simply add "/?test=1") to the end

http://www.aaa.com/?location=home (would figure out there's already a ? being used, and would add &test=1 to the end)

http://www.aaa.com/?location=home&page=1 (like the one above, would figure out there's already a ? being used, and would add &test=1 to the end)

I'd be happy to write domething to do it myself, but if python can already do it somehow, I'd me more than happy to use any built-in functionality that would save me some time ;-)

Thanks in advance


Using both Lennart Regebro's and Adam Vendenberg's suggestions, you could do something like this:

import urllib
import urlparse

query=urllib.urlencode({"test":1})
urls=['http://www.aaa.com',
      'http://www.aaa.com/home',
      'http://www.aaa.com/?location=home',
      'http://www.aaa.com/?location=home&page=1']
for url in urls:
    x=urlparse.urlparse(url)
    new_query=(x.query+'&'+query) if x.query else query
    y=urlparse.urlunparse((x.scheme,x.netloc,x.path,x.params,new_query,x.fragment))
    print(y)

yields

http://www.aaa.com?test=1
http://www.aaa.com/home?test=1
http://www.aaa.com/?location=home&test=1
http://www.aaa.com/?location=home&page=1&test=1


You probably want something like urllib.urlencode: http://docs.python.org/library/urllib.html#urllib.urlencode


Yes, the urlparse library (called urllib.parse in Python 3) can help you do this:

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

Specifically the urlparse and urlunparse methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜