• Python-parse模块url转换


    1. 拆分url

     1 from urllib.parse import urlparse
     2 
     3 result = urlparse("http://www.baidu.com/index.html;user?id=5#comment")
     4 print(result)
     5 print(list(result))
     6 print(result[0])
     7 
     8 # 结果:
     9 # ParseResult(scheme='http', netloc='www.baidu.com', path='/index.html', params='user', query='id=5', fragment='comment')
    10 # ['http', 'www.baidu.com', '/index.html', 'user', 'id=5', 'comment']
    11 # http
    View Code
    
    

    2. 拼接url

     1 from urllib.parse import urlunparse
     2 
     3 data = ['http','www.baidu.com','index.html','user','a=123','commit']
     4 print(urlunparse(data))
     5 
     6 # 结果:
     7 # http://www.baidu.com/index.html;user?a=123#commit
     8 
     9 from urllib.parse import urljoin
    10 
    11 # 另一个拼贴url
    12 
    13 print(urljoin('http://www.baidu.com', 'FAQ.html'))
    14 print(urljoin('http://www.baidu.com', 'https://pythonsite.com/FAQ.html'))
    15 print(urljoin('http://www.baidu.com/about.html', 'https://pythonsite.com/FAQ.html'))
    16 print(urljoin('http://www.baidu.com/about.html', 'https://pythonsite.com/FAQ.html?question=2'))
    17 print(urljoin('http://www.baidu.com?wd=abc', 'https://pythonsite.com/index.php'))
    18 print(urljoin('http://www.baidu.com', '?category=2#comment'))
    19 print(urljoin('www.baidu.com', '?category=2#comment'))
    20 print(urljoin('www.baidu.com#comment', '?category=2'))
    21 
    22 # 结果:
    23 # http://www.baidu.com/FAQ.html
    24 # https://pythonsite.com/FAQ.html
    25 # https://pythonsite.com/FAQ.html
    26 # https://pythonsite.com/FAQ.html?question=2
    27 # https://pythonsite.com/index.php
    28 # http://www.baidu.com?category=2#comment
    29 # www.baidu.com?category=2#comment
    30 # www.baidu.com?category=2
    View Code

    3. 另一个url拼接

     1 print(urljoin('http://www.baidu.com', 'FAQ.html'))
     2 print(urljoin('http://www.baidu.com', 'https://pythonsite.com/FAQ.html'))
     3 print(urljoin('http://www.baidu.com/about.html', 'https://pythonsite.com/FAQ.html'))
     4 print(urljoin('http://www.baidu.com/about.html', 'https://pythonsite.com/FAQ.html?question=2'))
     5 print(urljoin('http://www.baidu.com?wd=abc', 'https://pythonsite.com/index.php'))
     6 print(urljoin('http://www.baidu.com', '?category=2#comment'))
     7 print(urljoin('www.baidu.com', '?category=2#comment'))
     8 print(urljoin('www.baidu.com#comment', '?category=2'))
     9 
    10 # 结果:
    11 # http://www.baidu.com/FAQ.html
    12 # https://pythonsite.com/FAQ.html
    13 # https://pythonsite.com/FAQ.html
    14 # https://pythonsite.com/FAQ.html?question=2
    15 # https://pythonsite.com/index.php
    16 # http://www.baidu.com?category=2#comment
    17 # www.baidu.com?category=2#comment
    18 # www.baidu.com?category=2
    View Code

    4. 字典转为参数

     1 from urllib.parse import urlencode
     2 
     3 params = {
     4     "name":"zhaofan",
     5     "age":23,
     6 }
     7 base_url = "http://www.baidu.com?"
     8 
     9 url = base_url+urlencode(params)
    10 print(url)
    11 
    12 # 结果
    13 # http://www.baidu.com?name=zhaofan&age=23
    View Code

    5. 参数转为字典

     1 url = '?id=1&name=xiaocai&'
     2 
     3 from urllib.parse import parse_qs
     4 
     5 url = url.split('?')
     6 
     7 aa = parse_qs(url[1])
     8 print(aa)
     9 
    10 # 结果:
    11 {'id': ['1'], 'name': ['xiaocai']}
    View Code
  • 相关阅读:
    最大子数组和问题的解
    【剑指Offer】46孩子们的游戏(圆圈中最后剩下的数)
    【剑指Offer】45扑克牌顺子
    【剑指Offer】44翻转单词顺序列
    【剑指Offer】43左旋转字符串
    【剑指Offer】42和为S的两个数字
    【剑指Offer】41和为S的连续正数序列
    【剑指Offer】40数组中只出现一次的数字
    【剑指Offer】39平衡二叉树
    【剑指Offer】38二叉树的深度
  • 原文地址:https://www.cnblogs.com/yunweixiaoxuesheng/p/8406047.html
Copyright © 2020-2023  润新知