zoukankan      html  css  js  c++  java
  • from urllib import parse模块的使用

    一、介绍

    定义了url的标准接口,实现url的各种抽取
    parse模块的作用:url的解析,合并,编码,解码

    二、代码

    实现url的识别和分段

    方法1.urlparse

    url:待解析的url
    scheme='':假如解析的url没有协议,可以设置默认的协议,如果url有协议,设置此参数无效
    allow_fragments=True:是否忽略锚点,默认为True表示不忽略,为False表示忽略

    from urllib import parse
    url = 'https://www.cnblogs.com/angelyan/'
    result = parse.urlparse(url=url,scheme='http',allow_fragments=True)
    print(result)
    print(result.scheme)
    

    (scheme='https', netloc='www.cnblogs.com', path='/angelyan/', params='', query='', fragment='')
    scheme:表示协议
    netloc:域名
    path:路径
    params:参数
    query:查询条件,一般都是get请求的url
    fragment:锚点,用于直接定位页
    面的下拉位置,跳转到网页的指定位置

    方法2.urlunparse

    可以实现url的构造

    url_parmas = ('https', 'www.cnblogs.com', '/angelyan/', '', 'name=maple', 'log')
    #components:是一个可迭代对象,长度必须为6
    result = parse.urlunparse(url_parmas)
    print(result)
    

    返回结果:
    https://www.cnblogs.com/angelyan/?name=maple#log

    方法3.urljoin

    传递一个基础连接,根据基础连接可以将某一个不完整的链接拼接为一个完整链接

    base_url = 'https://www.cnblogs.com'
    sub_url = '/angelyan/?name=maple#log'
    full_url = parse.urljoin(base_url,sub_url)
    print(full_url)
    
    

    返回结果:
    https://www.cnblogs.com/angelyan/?name=maple#log

    方法4.urlencode

    将字典形式的参数序列化为url编码后的字符串,常用来构造get请求和post请求的参数

    
    parmas = {
        'name':'maple',
        'age':18
    }
    parmas_str = parse.urlencode(parmas)
    print(parmas_str)
    

    返回结果:
    name=maple&age=18

    parmas_str = 'name=maple&age=18'
    # 将url编码格式的参数反序列化为字典类型
    parmas = parse.parse_qs(parmas_str)
    print(parmas)
    

    返回结果:
    {'name': ['maple'], 'age': ['18']}

    方法5.quote编码

    可以将中文转换为url编码格式

    tt = time.strftime("%a %b %d %Y %H:%M:%S", time.localtime())
    send_time = tt +  ' GMT+0800 (中国标准时间)
    print(parse.quote(send_time))
    url = 'https://www.xinxindai.com/findPassword/sendSMS.html?'+ parse.quote(send_time)+'&mobileNo='+phone+'&imageCode='+code`
    print(url)
    

    返回结果:
    Sun%20Feb%2007%202021%2016%3A55%3A14%20GMT%2B0800%20%28%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4%29
    https://www.xinxindai.com/findPassword/sendSMS.html?Sun Feb 07 2021 16%3A55%3A14 GMT%2B0800 (中国标准时间)&mobileNo=13811352043&imageCode=9389

    方法6.unquote解码

    from urllib import parse
    url = 'https://www.xinxindai.com/findPassword/sendSMS.html?Sun%20Feb%2007%202021%2016%3A55%3A14%20GMT%2B0800%20%28%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4%29&mobileNo=13811352043&imageCode=9389
    '
    print(parse.unquote(url)
    

    返回结果:
    https://www.xinxindai.com/findPassword/sendSMS.html?Sun Feb 07 2021 15:42:10 GMT+0800 (中国标准时间)&mobileNo=13811352043&imageCode=9389

  • 相关阅读:
    穷举 迭代 while
    for 循环
    switch case
    if else 语句
    数据类型
    语句的输入、输出
    控件——DataGridview
    mysql-bin.000001文件的来源及处理方法
    /var/log目录下的20个Linux日志文件功能详解
    CountDownLatch
  • 原文地址:https://www.cnblogs.com/gqv2009/p/14385876.html
Copyright © 2011-2022 走看看