Python3的urllib.parse經常使用函數小結

一、獲取url參數

from urllib import parse

url = 'https://docs.python.org/3.5/search.html?q=parse&check_keywords=yes&area=default'
parseResult = parse.urlparse(url)
print(parseResult)
# ParseResult(scheme='https', netloc='docs.python.org', path='/3.5/search.html', params='', query='q=parse&check_keywords=yes&area=default', fragment='')
param_dict = parse.parse_qs(parseResult.query)
print(param_dict)
# {'q': ['parse'], 'check_keywords': ['yes'], 'area': ['default']}
q = param_dict['q'][0]
print(q)
'parse'
# # 注意:加號會被解碼,可能有時並非咱們想要的
d = parse.parse_qs('proxy=183.222.102.178:8080&task=XXXXX|5-3+2')
print(d)

二、 urlencode

from urllib import parse

query = {"name": "walker", "age": 99}
d = parse.urlencode(query)
print(d)
# name=walker&age=99

三、quote/quote_plus

from urllib import parse
d = parse.quote('a&b/c')  #未編碼斜線
print(d)
# a%26b/c
d1 = parse.quote_plus('a&b/c')  #編碼了斜線
print(d1)
# a%26b%2Fc

四、unquote/unquote_plus

from urllib import parse

d = parse.unquote('1+2')  # 不解碼加號
print(d)
# 1+2
d1 = parse.unquote_plus('1+2')  # 把加號解碼爲空格
print(d1)
# 1 2

五、獲取url後面的參數爲dict

from urllib import parse


def qs(url):
    query = parse.urlparse(url).query
    return dict([(k, v[0]) for k, v in parse.parse_qs(query).items()])


print(qs('http://url/api?param=2&param2=4'))
# {'param': '2', 'param2': '4'}

六、URL參數拼接

from urllib import parse


def url_add_params(url, **params):
    pr = parse.urlparse(url)
    query = dict(parse.parse_qsl(pr.query))
    query.update(params)
    pr_list = list(pr)
    pr_list[4] = parse.urlencode(query)
    return parse.ParseResult(*pr_list).geturl()


if __name__ == "__main__":
    url = 'http://bbs.163.com/viewthread.php'
    data = {"name": "hero", "111": "222"}
    print(url_add_params(url, **data))

# result : http://bbs.163.com/viewthread.php?111=222&name=hero

七、其餘功能

urljoin

from urllib import parse

d = parse.urljoin('http://www.oschina.com/tieba', 'index.php')
print(d)
# http://www.oschina.com/index.php
d1 = parse.urljoin('http://www.oschina.com/tieba/', 'index.php')
print(d1)
# http://www.oschina.com/tieba/index.php

urlsplit

urlsplit和urlparse差很少,不過它不切分URL的參數。適用於遵循RFC2396的URL,每一個路徑段都支持參數。這樣返回的元組就只有5個元素.php

from urllib import parse

url = parse.urlsplit('http://www.baidu.com/index.php?username=guol')
print(url)
# SplitResult(scheme='http', netloc='www.baidu.com', path='/index.php', query='username=guol', fragment='')

urlunsplit

使用urlsplit的格式組合成一個url,傳遞的元素必須是5個,或者直接將分解的元組從新組合html

from urllib import parse

d = parse.urlunsplit(("https", "i.cnblogs.com", "EditPosts.aspx", "a=a", "b=b"))
print(d)
# https://i.cnblogs.com/EditPosts.aspx?a=a#b=b

urlparse

urlparse(將url解析爲組件,url必須以http://開頭)python

from urllib import parse

d = parse.urlparse("https://i.cnblogs.com/EditPosts.aspx?opt=1")
print(d)
# ParseResult(scheme='https', netloc='i.cnblogs.com', path='/EditPosts.aspx', params='', query='opt=1', fragment='')

urlunparse

使用urlparse的格式組合成一個url,能夠直接將urlparse的返回傳遞組合api

from urllib import parse

data = parse.urlparse("https://i.cnblogs.com:80/EditPosts.aspx?opt=1")
print(parse.urlunparse(data))
# https://i.cnblogs.com:80/EditPosts.aspx?opt=1
相關文章
相關標籤/搜索