如何在Python中使用标准库构建URL?[关闭]

shstlldc  于 2023-04-28  发布在  Python
关注(0)|答案(4)|浏览(75)

**已关闭。**此问题正在寻求书籍、工具、软件库等的建议。它不符合Stack Overflow guidelines。目前不接受答复。

我们不允许问题寻求书籍,工具,软件库等的建议。您可以编辑问题,以便可以用事实和引用来回答。
5年前关闭。
Improve this question
我需要知道如何在Python中构建URL,比如:

http://subdomain.domain.com?arg1=someargument&arg2=someotherargument

在python标准库中,如何构建URL?

hsgswve4

hsgswve41#

我会选择Python的urllib,它是一个内置库。
Python 2**

import urllib
url = 'https://example.com/somepage/?'
params = {'var1': 'some data', 'var2': 1337}
print(url + urllib.urlencode(params))

Python 3

import urllib.parse
url = 'https://example.com/somepage/?'
params = {'var1': 'some data', 'var2': 1337}
print(url + urllib.parse.urlencode(params))

输出:

https://example.com/somepage/?var1=some+data&var2=1337
b1payxdu

b1payxdu2#

python标准库中的urlparse是关于构建有效的url的。查看urlparse的文档
示例:

from collections import namedtuple
from urllib.parse import urljoin, urlencode, urlparse, urlunparse

# namedtuple to match the internal signature of urlunparse
Components = namedtuple(
    typename='Components', 
    field_names=['scheme', 'netloc', 'url', 'path', 'query', 'fragment']
)

query_params = {
    'param1': 'some data', 
    'param2': 42
}

url = urlunparse(
    Components(
        scheme='https',
        netloc='example.com',
        query=urlencode(query_params),
        path='',
        url='/',
        fragment='anchor'
    )
)

print(url)

输出:

https://example.com/?param1=some+data&param2=42#anchor
8cdiaqws

8cdiaqws3#

下面是一个使用urlparse生成URL的示例。这提供了向URL添加路径的便利,而不必担心检查斜杠。

import urllib

def build_url(base_url, path, args_dict):
    # Returns a list in the structure of urlparse.ParseResult
    url_parts = list(urllib.parse.urlparse(base_url))
    url_parts[2] = path
    url_parts[4] = urllib.parse.urlencode(args_dict)
    return urllib.parse.urlunparse(url_parts)

>>> args = {'arg1': 'value1', 'arg2': 'value2'}
>>> # works with double slash scenario
>>> build_url('http://www.example.com/', '/somepage/index.html', args)

http://www.example.com/somepage/index.html?arg1=value1&arg2=value2

# works without slash
>>> build_url('http://www.example.com', 'somepage/index.html', args)

http://www.example.com/somepage/index.html?arg1=value1&arg2=value2
ltskdhd1

ltskdhd14#

import urllib

def make_url(base_url , *res, **params):
    url = base_url
    for r in res:
        url = '{}/{}'.format(url, r)
    if params:
        url = '{}?{}'.format(url, urllib.urlencode(params))
    return url

print make_url('http://example.com', 'user', 'ivan', alcoholic='true', age=18)

输出:

http://example.com/user/ivan?age=18&alcoholic=true

相关问题