python - 对字符串进行 URL 编码

标签 python

我可以执行以下操作来构建 url:

base_url = 'http://google.com/'
qs = urllib.urlencode({'q': string})
url = base_url + '?' + qs

有没有办法对字符串进行 url 编码?例如,我希望能够做到:

url = 'http://google.com/?q=' + urlencode('this is my search'))

最佳答案

使用urllib.quoteurllib.quote_plus,例如:

>>> urllib.quote('this is my string')
'this%20is%20my%20string'

>>> urllib.quote_plus('this is my string')
'this+is+my+string'

关于python - 对字符串进行 URL 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28890909/

相关文章:

python - 分配给多维数组

python - 是否可以删除一个类的实例,该类的实例会自动将其从作为元素的所有列表中删除?

c++ - 实用观点: Why would I want to use Python with C++?

python - NLP 检测英语条件语句

python - 加入 Python 中的多个线程之一

python - 使用以前保存的模型获得测试数据的分类准确性

python - Pandas groupby : fill missing values from other group members

python - 游戏错误 : mixer system not initialized

python - 我的神经网络实现有什么问题?

Python xarray.concat 然后 xarray.to_netcdf 生成巨大的新文件大小