简体   繁体   English

使用Requests模块Python构建URL

[英]Build a URL using Requests module Python

Is it possible to build a URL using the Requests library for Python? 是否可以使用Python的Requests库构建URL?

Building a query string is supported but what about building the rest of the URL. 支持构建查询字符串但是如何构建URL的其余部分。 Specifically I'd be interested in adding on to the base URL with URL encoded strings: 具体来说,我有兴趣使用URL编码的字符串添加到基本URL:

http :// some address.com/api/[term]/ http:// some address.com/api/[term]/

term = 'This is a test' term ='这是一个测试'

http :// some address.com/api/This+is+a+test/ http:// some address.com/api/This+is+a+test/

This is presumably possible using urllib but it seems like it would be better in Requests. 这可能是使用urllib可能的,但似乎在请求中会更好。 Does this feature exist? 这个功能是否存在? If not is there a good reason that it shouldn't? 如果没有,是不是有充分的理由不应该?

requests is basically a convenient wrapper around urllib (and 2,3 and other related libraries). requests基本上是urllib (以及2,3和其他相关库)的方便包装器。

You can import urljoin() , quote() from requests.compat , but this is essentially the same as using them directly from urllib and urlparse modules: 您可以从requests.compat导入urljoin()quote() ,但这直接从urlliburlparse模块中使用它们基本相同

>>> from requests.compat import urljoin, quote_plus
>>> url = "http://some-address.com/api/"
>>> term = 'This is a test'
>>> urljoin(url, quote_plus(term))
'http://some-address.com/api/This+is+a+test'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM