python - 使用 Python 请求库构建 URL

标签 python url python-requests

我有一个基本 URL。

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

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

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

我正在使用请求库来构建和调用 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

我不确定如何解决这个问题。

最佳答案

在基本 url 的末尾添加一个 /

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

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

关于python - 使用 Python 请求库构建 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34830638/

相关文章:

python - 使用 python asyncio 正确使用 sqlalchemy scoped_session

python - 尝试在 pytest 屏幕上打印异常错误

python - 如何使用tensorflow在全连接层中恢复

Python 数据操作 : Splitting Data from one column to make more rows in the same dataframe

android - 在android中制作超链接textview

c# - 最好用的 URL 重写器是什么?

python - 使用 requests 诊断挂起的请求

html - 如何使用相对 URI 转到子目录

python - beautifulsoup select 方法返回回溯

python - 在没有硬编码有效负载的情况下无法从一个部分中抓取所有书籍