如何使用Python正式插入URL空间(%20)? [英] How to formally insert URL space (%20) using Python?
问题描述
在ebay中输入多词搜索词时,结果URL看起来像(例如"demarini cf5 cf12"):
When a mutli-word search term is entered in ebay, the resultant URL looks something like (for example "demarini cf5 cf12"):
http://www.ebay.com/sch/i.html?_from=R40&_sacat=0&_nkw=demarini%20cf5%20cfc12
我希望用Python构造此URL,以便可以直接访问它.因此,是串联基本URL的情况:
I wish to construct this URL in Python so it can be accessed directly. So it's case of concatenating the base URL:
http://www.ebay.com/sch/i.html?_from=R40&_sacat=0&_nkw=
...以及搜索词.现在.因此,我将%20
明确地添加到空格中:
... with the search term. Right now. I am adding the %20
for the spaces explicately thus:
baseUrl = 'http://www.ebay.com/sch/i.html?_from=R40&_sacat=0&_nkw='
searchTerm = 'demarini cf5 cf12'
searchTerm = ('%20').join(searchTerm.split(' '))
finalUrl = baseUrl + searchTerm
在Python中执行此操作的更正式的方法是什么?我相信这类任务的名称是URL编码吗?
What is a more formal way of doing this in Python? I believe the name for this sort of task is URL encoding?
推荐答案
简单易用的urllib库 导入urllib
It's simple use urllib library import urllib
finalurl = baseUrl + urllib.quote(searchterm)
您可以使用quote_plus()添加%20的+ insted 撤消此用途
you can use quote_plus() to add + insted of %20 to undo this use
urllib.unquote(str)
这篇关于如何使用Python正式插入URL空间(%20)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!