从python中的URL获取查询字符串的最佳方法? [英] Best way to get query string from a URL in python?

查看:446
本文介绍了从python中的URL获取查询字符串的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个url = https://stackoverflow.com/questions/ask?next=1& value = 3
现在我需要从这个url获取查询字符串。
我不想使用request.META获取查询字符串。我如何想出有两种方法来获取查询字符串

I have this url = https://stackoverflow.com/questions/ask?next=1&value=3 Now I need to get the query string from this url. I don;t wanna use request.META for getting query string. How ever I figured it out that there are two more ways to get the query string


  1. 使用urlparse
    使用urlparse.urlparse(url).query dats它

  1. Using urlparse Use urlparse.urlparse(url).query dats it

使用url编码
使用urlencode并将request.GET参数字典传递给它,以获取它的字符串表示....

Using url encode Use urlencode and pass the request.GET params dictionary into it,to get the string representation of it....

所以哪种方式更好地获取查询字符串...?
我的一些同事们说,urlencode好多了,但是我不太满意,所以到目前为止还没有答案。就像他们在内部提到urlparse调用urlencode一样。就这一点我有疑问,我认为urlparse和urlencode是完全独立的。urlencode是在urllib模块....请澄清这个东西。

So which way is better to get the query string...? Some of my colleagues are saying that urlencode is much better , however I am not satisfied wid there answers so far..like they mentioned urlparse calls urlencode internally too..Even on this point I have doubt as I think urlparse and urlencode are entirely independent.as urlencode is in urllib module....please clarify this thing.

推荐答案

你可以使查询字符串使用这样的GET参数

You can make Query string using GET parameters like this

request.GET.urlencode()

这不包含前缀,它可能不会按照与原始请求。

This does not include the ? prefix, and it may not return the keys in the same order as in the original request.

这篇关于从python中的URL获取查询字符串的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆