python python的url构建器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1590219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:38:21  来源:igfitidea点击:

url builder for python

pythonurl

提问by Tom

I know about urlliband urlparse, but I want to make sure I wouldn't be reinventing the wheel.

我知道urlliband urlparse,但我想确保我不会重新发明轮子。

My problem is that I am going to be fetching a bunch of urls from the same domain via the urlliblibrary. I basically want to be able to generate urls to use (as strings) with different paths and query params. I was hoping that something might have a syntax like:

我的问题是我将通过urllib库从同一个域中获取一堆 url 。我基本上希望能够生成 urls 以使用(作为字符串)具有不同的路径和查询参数。我希望某些东西可能有这样的语法:

url_builder = UrlBuilder("some.domain.com")
# should give me "http://some.domain.com/blah?foo=bar
url_i_need_to_hit = url_builder.withPath("blah").withParams("foo=bar")  # maybe a ".build()" after this

Basically I want to be able to store defaults that get passed to urlparse.urlunsplitinstead of constantly clouding up the code by passing in the whole tuple every time.

基本上,我希望能够存储传递给的默认值,urlparse.urlunsplit而不是通过每次传入整个元组来不断地使代码变得模糊。

Does something like this exist? Do people agree it's worth throwing together?

这样的东西存在吗?人们是否同意将其放在一起?

采纳答案by ewall

Still not quite sure what you're looking for... But I'll give it a shot. If you're just looking to make a class that will keep your default values and such, it's simple enough to make your own class and use Python magic like str. Here's a scratched-out example (suboptimal):

仍然不太确定你在寻找什么......但我会试一试。如果您只是想创建一个保留默认值等的类,那么创建自己的类并使用像str这样的 Python 魔法就足够简单了。这是一个划掉的例子(次优):

class UrlBuilder:
    def __init__(self,domain,path="blah",params="foo=bar"):
        self.domain = domain
        self.path = path
        self.params = params

    def withPath(self,path):
        self.path = path
        return self

    def withParams(self,params):
        self.params = params
        return self

    def __str__(self):
        return 'http://' + self.domain + '/' + self.path + '?' + self.params
        # or return urlparse.urlunparse( ( "http", self.domain, self.path, self.params, "", "" )

    def build(self):
        return self.__str__()

if __name__ == '__main__':
    u = UrlBuilder('www.example.com')
    print u.withPath('bobloblaw')
    print u.withParams('lawyer=yes')
    print u.withPath('elvis').withParams('theking=true')

If you're looking for more of the Builder Design Pattern, the Wikipedia article has a reasonable Python example(as well as Java).

如果您正在寻找更多的 Builder Design Pattern,维基百科文章有一个合理的 Python 示例(以及Java)。

回答by S.Lott

Are you proposing an extension to http://docs.python.org/library/urlparse.html#urlparse.urlunparsethat would substitute into the 6-item tuple?

您是否提议对http://docs.python.org/library/urlparse.html#urlparse.urlunparse进行扩展以替代 6 项元组?

Are you talking about something like this?

你在谈论这样的事情吗?

def myUnparse( someTuple, scheme=None, netloc=None, path=None, etc. ):
    parts = list( someTuple )
    if scheme is not None: parts[0] = scheme
    if netloc is not None: parts[1]= netloc
    if path is not None: parts[2]= path
    etc.
    return urlunparse( parts )

Is that what you're proposing?

那是你提议的吗?

This?

这?

class URLBuilder( object ):
    def __init__( self, base ):
        self.parts = list( urlparse(base) )
    def __call__( self, scheme=None, netloc=None, path=None, etc. ):
        if scheme is not None: self.parts[0] = scheme
        if netloc is not None: self.parts[1]= netloc
        if path is not None: self.parts[2]= path
        etc.
        return urlunparse( self.parts )

bldr= URLBuilder( someURL )
print bldr( scheme="ftp" )

Something like that?

类似的东西?

回答by neutrinus

You might want consider having a look at furlbecause it might be an answer to your needs.

您可能需要考虑查看furl,因为它可能是您需要的答案。

回答by Scott Smith

I think you want http://pythonhosted.org/uritools/.

我想你想要http://pythonhosted.org/uritools/

Example from the docs:

文档中的示例:

parts = urisplit('foo://[email protected]:8042/over/there?name=ferret#nose')

orig_uri = uriunsplit(parts)

The split value is a named tuple, not a regular list. It is accessible by name or index:

拆分值是一个命名元组,而不是常规列表。它可以通过名称或索引访问:

assert(parts[0] == parts.schema)
assert(parts[1] == parts.authority)
assert(parts[2] == parts.path)
assert(parts[3] == parts.query)
assert(parts[4] == parts.fragment)

Make a copy to make changes:

复制以进行更改:

new_parts = [part for part in parts]
new_parts[2] = "/some/other/path"
new_uri = uriunsplit(new_parts)