【问题标题】:URL query parameters to dict pythondict python的URL查询参数
【发布时间】:2014-03-02 07:33:07
【问题描述】:

有没有办法解析一个 URL(使用一些 python 库)并返回一个 python 字典,其中包含 URL 的查询参数部分的键和值?

例如:

url = "http://www.example.org/default.html?ct=32&op=92&item=98"

预期回报:

{'ct':32, 'op':92, 'item':98}

【问题讨论】:

    标签: python parsing url query-parameters


    【解决方案1】:

    使用urllib.parse library

    >>> from urllib import parse
    >>> url = "http://www.example.org/default.html?ct=32&op=92&item=98"
    >>> parse.urlsplit(url)
    SplitResult(scheme='http', netloc='www.example.org', path='/default.html', query='ct=32&op=92&item=98', fragment='')
    >>> parse.parse_qs(parse.urlsplit(url).query)
    {'item': ['98'], 'op': ['92'], 'ct': ['32']}
    >>> dict(parse.parse_qsl(parse.urlsplit(url).query))
    {'item': '98', 'op': '92', 'ct': '32'}
    

    urllib.parse.parse_qs()urllib.parse.parse_qsl() 方法解析查询字符串,考虑到键可以出现多次并且顺序可能很重要。

    如果您仍在使用 Python 2,则 urllib.parse 被称为 urlparse

    【讨论】:

      【解决方案2】:

      对于 Python 3,来自 parse_qs 的 dict 的值在一个列表中,因为可能有多个值。如果你只想要第一个:

      >>> from urllib.parse import urlsplit, parse_qs
      >>>
      >>> url = "http://www.example.org/default.html?ct=32&op=92&item=98"
      >>> query = urlsplit(url).query
      >>> params = parse_qs(query)
      >>> params
      {'item': ['98'], 'op': ['92'], 'ct': ['32']}
      >>> dict(params)
      {'item': ['98'], 'op': ['92'], 'ct': ['32']}
      >>> {k: v[0] for k, v in params.items()}
      {'item': '98', 'op': '92', 'ct': '32'}
      

      【讨论】:

      • 这不是 Python 3 独有的,Python 2 urllib.parse_qs 也返回值的列表。我在回答中特别提到了这一点,顺便说一下,如果您只需要单个值,您可能希望使用 urllib.parse_qsl() 并将结果列表传递到 dict()
      • 似乎与parse_qls 的区别在于,由于它返回一个元组列表,因此将 that 转换为 dict 将保留 last 值而不是 first。这当然假设有多个值开始。
      【解决方案3】:

      如果您不想使用解析器:

      url = "http://www.example.org/default.html?ct=32&op=92&item=98"
      url = url.split("?")[1]
      dict = {x[0] : x[1] for x in [x.split("=") for x in url[1:].split("&") ]}
      

      所以我不会删除上面的内容,但绝对不是你应该使用的。

      我想我看了一些答案,它们看起来有点复杂,以防你像我一样,不要使用我的解决方案。

      使用这个:

      from urllib import parse
      params = dict(parse.parse_qsl(parse.urlsplit(url).query))
      

      对于 Python 2.X

      import urlparse as parse
      params = dict(parse.parse_qsl(parse.urlsplit(url).query))
      

      我知道这与接受的答案相同,只是在一个可以复制的衬里。

      【讨论】:

      • 解析不仅仅是拆分字符串。您还需要处理 URL 编码(包括+),urllib.parse 也会根据要求为您引发或忽略错误。当它是标准库的一部分时,我不确定你为什么要重新发明这个轮子。
      【解决方案4】:

      对于 python 2.7

      In [14]: url = "http://www.example.org/default.html?ct=32&op=92&item=98"
      
      In [15]: from urlparse import urlparse, parse_qsl
      
      In [16]: parse_url = urlparse(url)
      
      In [17]: query_dict = dict(parse_qsl(parse_url.query))
      
      In [18]: query_dict
      Out[18]: {'ct': '32', 'item': '98', 'op': '92'}
      

      【讨论】:

        【解决方案5】:

        我同意不要重新发明轮子,但有时(在您学习的时候)构建一个轮子有助于理解一个轮子。 :) 所以,从纯粹的学术角度来看,我提供这个的警告是,使用字典假定名称值对是唯一的(即查询字符串不包含多个记录)。

        url = 'http:/mypage.html?one=1&two=2&three=3'
        
        page, query = url.split('?')
        
        names_values_dict = dict(pair.split('=') for pair in query.split('&'))
        
        names_values_list = [pair.split('=') for pair in query.split('&')]
        

        我在 Idle IDE 中使用的是 3.6.5 版。

        【讨论】:

          【解决方案6】:

          对于python2.7,我正在使用urlparse 模块将url 查询解析为dict。

          import urlparse
          
          url = "http://www.example.org/default.html?ct=32&op=92&item=98"
          
          print urlparse.parse_qs( urlparse.urlparse(url).query )
          # result: {'item': ['98'], 'op': ['92'], 'ct': ['32']} 
          

          【讨论】:

          • 有没有办法不通过删除项目来重建原始网址。
          【解决方案7】:
          from urllib.parse import splitquery, parse_qs, parse_qsl
          
          url = "http://www.example.org/default.html?ct=32&op=92&item=98&item=99"
          
          splitquery(url)
          # ('http://www.example.org/default.html', 'ct=32&op=92&item=98&item=99')
          
          parse_qs(splitquery(url)[1])
          # {'ct': ['32'], 'op': ['92'], 'item': ['98', '99']}
          
          dict(parse_qsl(splitquery(url)[1]))
          # {'ct': '32', 'op': '92', 'item': '99'}
          
          # also works with url w/o query
          parse_qs(splitquery("http://example.org")[1])
          # {}
          
          dict(parse_qsl(splitquery("http://example.org")[1]))
          # {}
          

          老问题,虽然在我遇到这个splitquery 的东西之后我会参与进来。不确定 Python 2,因为我不使用 Python 2。splitqueryre.split(r"\?", url, 1) 多一点。

          【讨论】:

            【解决方案8】:

            您可以使用特定库轻松解析 URL。

            这是我在没有任何专用库的情况下解析它的简单代码。

            (输入的url必须包含域名、协议和路径。

            def parseURL(url):

            seg2 = url.split('/')[2]    # Separating domain name
            seg1 = url.split(seg2)[-2]  # Deriving protocol
            print('Protocol:', seg1, '\n')
            print('Domain name:', seg2, '\n')
            seg3 = url.split(seg2)[1]   #Getting the path; if output is empty,the there is no path in URL
            print('Path:', seg3, '\n')
            
            if '#' in url:  # Extracting fragment id, else None
                seg4 = url.split('#')[1]
                print('Fragment ID:', seg4, '\n')
            else:
                seg4 = 'None'
            if '@' in url:              # Extracting user name, else None
                seg5 = url.split('/')[-1]
                print('Scheme with User Name:', seg5, '\n')
            else:
                seg5 = 'None'
            if '?' in url:              # Extracting query string, else None
                seg6 = url.split('?')[-1]
                print('Query string:', seg6, '\n')
            else:
                seg6 = 'None'
            
            print('**The dictionary is in the sequence: 0.URL 1.Protocol 2.Domain name 3.Path 4.Fragment id 5.User name 6.Query string** \n')
            
            dictionary = {'0.URL': url, '1.Protocol': seg1, '2.Domain name': seg2, '3.Path': seg3, '4.Fragment id': seg4,
                          '5.User name': seg5, '6.Query string': seg6}  # Printing required dictionary
            print(dictionary, '\n')
            
            print('The TLD in the given URL is following: ')
            if '.com' in url:           # Extracting most famous TLDs maintained by ICAAN
                print('.com\n')
            elif '.de' in url:
                print('.de\n')
            elif '.uk' in url:
                print('.uk\n')
            elif 'gov' in url:
                print('gov\n')
            elif '.org' in url:
                print('.org\n')
            elif '.ru' in url:
                print('.ru\n')
            elif '.net' in url:
                print('.net\n')
            elif '.info' in url:
                print('.info\n')
            elif '.biz' in url:
                print('.biz\n')
            elif '.online' in url:
                print('.online\n')
            elif '.in' in url:
                print('.in\n')
            elif '.edu' in url:
                print('.edu\n')
            else:
                print('Other low level domain!\n')
            
            return dictionary
            

            如果 name == 'ma​​in': url = input("请输入您的网址:") 解析URL(url)

            #Sample URLS to copy
            # url='https://www.facebook.com/photo.php?fbid=2068026323275211&set=a.269104153167446&type=3&theater'   
            # url='http://www.blog.google.uk:1000/path/to/myfile.html?key1=value1&key2=value2#InTheDocument'      
            # url='https://www.overleaf.com/9565720ckjijuhzpbccsd#/347876331/' 
            

            【讨论】:

            • TLD 检测部分可能不适用于以下 URL:https://www.info.ca/
            猜你喜欢
            • 1970-01-01
            • 2012-05-30
            • 2015-02-19
            • 2016-03-14
            • 2011-03-24
            • 2021-05-26
            • 2011-09-06
            • 2011-01-04
            相关资源
            最近更新 更多