Best way to get query string from a URL in python?

后端 未结 3 889
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-09 02:22

I need to get the query string from this URL https://stackoverflow.com/questions/ask?next=1&value=3 and I don\'t want to use request.META. I have figured out that there

3条回答
  •  情书的邮戳
    2020-12-09 03:20

    Third option:

    >>> from urlparse import urlparse, parse_qs
    >>> url = 'http://something.com?blah=1&x=2'
    >>> urlparse(url).query
    'blah=1&x=2'
    >>> parse_qs(urlparse(url).query)
    {'blah': ['1'], 'x': ['2']}
    

    In Python 3+ this is available as:

    from urllib.parse import parse_qs
    

    Documentation for urllib.parse

提交回复
热议问题