简体   繁体   中英

Build a URL using Python requests library

I have a base URL.

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

I want to create a new url based on a search term(Ex - mit) appended to the base URL.

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

I am using requests library to build and call the URLs as shown below.

from requests.compat import urljoin

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

But when I print the new_url, it messes up the URL.

https://api.github.com/mit

I am not sure how to fix this issue.

Add a / at the end of the base url.

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

Otherwise it's probably treating licences as filename.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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