开发者

Is there a better way to write this URL Manipulation in Python?

开发者 https://www.devze.com 2022-12-30 19:41 出处:网络
I\'m curious if there\'s a simpler way to remove a particular parameter from a url. What I came up with is the following. This seems a bit verbose. Libraries to use or a more pythonic version apprecia

I'm curious if there's a simpler way to remove a particular parameter from a url. What I came up with is the following. This seems a bit verbose. Libraries to use or a more pythonic version appreciated.

parsed = urlparse(url)
if parsed.query != "":
    params = dict([s.split("=") for s in par开发者_JAVA百科sed.query.split("&")])
    if params.get("page"):
        del params["page"]
    url = urlunparse((parsed.scheme,
                      None,
                      parsed.path,
                      None,
                      urlencode(params.items()),
                      parsed.fragment,))
    parsed = urlparse(url)


Use urlparse.parse_qsl() to crack the query string. You can filter this in one go:

params = [(k,v) for (k,v) in parse_qsl(parsed.query) if k != 'page']


I've created a small helper class to represent a url in a structured way:

import cgi, urllib, urlparse

class Url(object):
    def __init__(self, url):
        """Construct from a string."""
        self.scheme, self.netloc, self.path, self.params, self.query, self.fragment = urlparse.urlparse(url)
        self.args = dict(cgi.parse_qsl(self.query))

    def __str__(self):
        """Turn back into a URL."""
        self.query = urllib.urlencode(self.args)
        return urlparse.urlunparse((self.scheme, self.netloc, self.path, self.params, self.query, self.fragment))

Then you can do:

u = Url(url)
del u.args['page']
url = str(u)

More about this: Web development peeve.

0

精彩评论

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