parsing a url in python with changing part in it

前端 未结 1 2140
小鲜肉
小鲜肉 2021-01-23 21:35

I\'m parsing a url in Python, below you can find a sample url and the code, what i want to do is splitting the (74743) from the url and make a for loop which will be taking it

相关标签:
1条回答
  • 2021-01-23 22:15
    >>> import urlparse
    
    >>> url = 'http://example.com/wps/portal/lYuxDoIwGAYf6f9aqKSjMNQ/?PartNo=74743&IntNumberOf=&is='
    
    >>> split_url = urlparse.urlsplit(url)
    >>> split_url.path
    '/wps/portal/lYuxDoIwGAYf6f9aqKSjMNQ/'
    

    You can split the path into a list of strings using '/', slice the list, and re-join:

    >>> path = split_url.path
    >>> path.split('/')
    ['', 'wps', 'portal', 'lYuxDoIwGAYf6f9aqKSjMNQ', '']
    

    Slice off the last two:

    >>> path.split('/')[:-2]
    ['', 'wps', 'portal']
    

    And re-join:

    >>> '/'.join(path.split('/')[:-2])
    '/wps/portal'
    

    To parse the query, use parse_qs:

    >>> parsed_query = urlparse.parse_qs(split_url.query)
    {'PartNo': ['74743']}
    

    To keep the empty parameters use keep_blank_values=True:

    >>> query = urlparse.parse_qs(split_url.query, keep_blank_values=True)
    >>> query
    {'PartNo': ['74743'], 'is': [''], 'IntNumberOf': ['']}
    

    You can then modify the query dictionary:

    >>> query['PartNo'] = 85731
    

    And update the original split_url:

    >>> updated = split_url._replace(path='/'.join(base_path.split('/')[:-2] +
                                                  ['ASDFZXCVQWER', '']),
                                    query=urllib.urlencode(query, doseq=True))
    
    >>> urlparse.urlunsplit(updated)
    'http://example.com/wps/portal/ASDFZXCVQWER/?PartNo=85731&IntNumberOf=&is='
    
    0 讨论(0)
提交回复
热议问题