zoukankan      html  css  js  c++  java
  • python中对url编码解码处理

    解码

    import urllib.parse
    
    url = "https://market.m.taobao.com/apps/market/content/index.html?contentId=210495552952&spm=a2141.7631544.t1.p602&scm=1007.11419.126969.0&wh_weex=true&wx_navbar_transparent=true&business_spm=a2141.12288619&source=youhh_tuji&params=%7B%22yhhListContentId%22%3A%22210495552952%22%7D&utparam=%7B%22x_object_type%22%3A%22content_item%22%2C%22x_item_ids%22%3A569376571773%2C%22x_object_id%22%3A%22210495552952%22%7D"
    
    decode_url = urllib.parse.unquote(url)
    
    print(decode_url)
    
    """
    转换后
    https://market.m.taobao.com/apps/market/content/index.html?contentId=210495552952&spm=a2141.7631544.t1.p602&scm=1007.11419.126969.0&wh_weex=true&wx_navbar_transparent=true&business_spm=a2141.12288619&source=youhh_tuji&params={"yhhListContentId":"210495552952"}&utparam={"x_object_type":"content_item","x_item_ids":569376571773,"x_object_id":"210495552952"}
    """

    编码

    urllib库里面有个urlencode函数,可以把key-value这样的键值对转换成我们想要的格式,返回的是a=1&b=2这样的字符串,比如:

    import urllib.parse
    
    url = 'https://market.m.taobao.com/apps/market/content/index.html?contentId=210495552952&spm=a2141.7631544.t1.p602&scm=1007.11419.126969.0&wh_weex=true&wx_navbar_transparent=true&business_spm=a2141.12288619&source=youhh_tuji&params={"yhhListContentId":"210495552952"}&utparam={"x_object_type":"content_item","x_item_ids":569376571773,"x_object_id":"210495552952"}'
    
    
    params = {
        "yhhListContentId":"210495552952",
    
    }
    utparam = {
        "x_object_type":"content_item",
        "x_item_ids":569376571773,
        "x_object_id":"210495552952"
    }
    
    params = urllib.parse.urlencode(params)
    utparam = urllib.parse.urlencode(utparam)
    
    url1 = 'https://market.m.taobao.com/apps/market/content/index.html?contentId=210495552952&spm=a2141.7631544.t1.p602&scm=1007.11419.126969.0&wh_weex=true&wx_navbar_transparent=true&business_spm=a2141.12288619&source=youhh_tuji' + "&" + params + '&' + utparam
    
    print(url1)
    
    """
    转换后
    https://market.m.taobao.com/apps/market/content/index.html?contentId=210495552952&spm=a2141.7631544.t1.p602&scm=1007.11419.126969.0&wh_weex=true&wx_navbar_transparent=true&business_spm=a2141.12288619&source=youhh_tuji&yhhListContentId=210495552952&x_object_type=content_item&x_item_ids=569376571773&x_object_id=210495552952
    """
  • 相关阅读:
    SQVI和SAP查询QUERY的区别和使用注意事项
    hdu 4119 Isabella's Message
    HaProxy+Keepalived+Mycat高可用群集配置
    Android之RecyclerView简单使用(三)
    2.CCGridAction(3D效果),3D反转特效,凸透镜特效,液体特效,3D翻页特效,水波纹特效,3D晃动的特效,扭曲旋转特效,波动特效,3D波动特效
    怎样cp文件夹时忽略指定的文件夹和文件
    使用boost::property_tree生成带attribute的xml
    数据结构:中序线索二叉树
    像音乐播放App一样移动背景
    【EasyUi】页面设计必学之Layout
  • 原文地址:https://www.cnblogs.com/songzhixue/p/12936289.html
Copyright © 2011-2022 走看看