【问题标题】:Build a URL using Python requests library使用 Python 请求库构建 URL
【发布时间】:2016-04-22 04:51:02
【问题描述】:

我有一个基本 URL。

BASE_URL = 'https://api.github.com/licenses'

我想根据附加到基本 URL 的搜索词(Ex - mit)创建一个新 url。

NEW_URL = 'https://api.github.com/licenses/mit'

我正在使用 requests 库来构建和调用 URL,如下所示。

from requests.compat import urljoin

base_url = 'https://api.github.com/licenses'
new_url = urljoin(base_url, 'mit')
print new_url

但是当我打印 new_url 时,它会弄乱 URL。

https://api.github.com/mit

我不确定如何解决此问题。

【问题讨论】:

    标签: python url python-requests


    【解决方案1】:

    在基本网址的末尾添加/

    BASE_URL = 'https://api.github.com/licenses/'
    

    否则它可能会将 licences 视为文件名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-02
      • 2021-03-28
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-05
      • 1970-01-01
      相关资源
      最近更新 更多