urlparse

  1. import urllib
  2. urllib.parse.urlparse('http://www.examples.com/search/?page=1&pageSize=10')
  3. # ParseResult(scheme='http', netloc='www.examples.com', path='/search/', params='', query='page=1&pageSize=10', fragment='')

query string: urlencode, parse_qs

  1. urllib.parse.urlencode({'page': 1, 'pageSize': 10})
  2. # 'page=1&pageSize=10'
  3. urllib.parse.parse_qs('page=1&pageSize=10')
  4. # {'page': ['1'], 'pageSize': ['10']}
  5. urllib.parse.parse_qs('page=1&pageSize=')
  6. # {'page': ['1']}
  7. urllib.parse.parse_qs('page=1&pageSize=', keep_blank_values=True)
  8. # {'page': ['1'], 'pageSize': ['']}

url quote/unquote

  1. urllib.parse.quote('page=1&pageSize=10')
  2. # 'page%3D1%26pageSize%3D10'
  3. urllib.parse.unquote('page%3D1%26pageSize%3D10')
  4. # 'page=1&pageSize=10'
  5. urllib.parse.quote('中文.pdf')
  6. # '%E4%B8%AD%E6%96%87.pdf'
  7. urllib.parse.unquote('%E4%B8%AD%E6%96%87.pdf')
  8. # '中文.pdf'