Get current URL in Python

前端 未结 7 1985
独厮守ぢ
独厮守ぢ 2020-12-05 18:49

How would i get the current URL with Python,

I need to grab the current URL so i can check it for query strings e.g

requested_url = \"URL_HERE\"

ur         


        
相关标签:
7条回答
  • 2020-12-05 19:02

    If your python script is server side:

    You can use os

        import os
        url = os.environ
        print(url)
    

    with that, you will see all the data os.environ gives you. It looks like your need the 'QUERY_STRING'. Like any JSON object, you can obtain the data like this.

        import os
        url = os.environ['QUERY_STRING']
        print(url)
    

    And if you want a really elegant scalable solution you can use anywhere and always, you can save the variables into a dictionary (named vars here) like so:

        vars={}
        splits=os.environ['QUERY_STRING'].split('&')
        for x in splits:
            name,value=x.split('=')
            vars[name]=value
    
        print(vars)
    

    If you are client side, then any of the other responses involving the get request will work

    0 讨论(0)
提交回复
热议问题