[Tutor] urljoin (was: Re: Tutor Digest, Vol 160, Issue 26)

Mats Wichmann mats at wichmann.us
Tue Jun 20 11:25:59 EDT 2017


On 06/20/2017 02:34 AM, angela ebirim wrote:
> Hi,
> 
> I'm trying to dynamically build a url in python and have tried using:

can see a couple of things...

> 
> #file.py
> 
> import json
> import requests
> from urllib.parse import urljoin
> 
> baseUrl = "
> http://data.parliament.uk/membersdataplatform/services/mnis/members/query"
> search_criteria = "House=Commons"
> outputs = "Constituencies"
> 
> headers = {"content-type": "application/json"}
> 
> 
> *""" so the resulting url: resultUrl should be *
> http://data.parliament.uk/membersdataplatform/services/mnis/members/query/House=Commons/Constituencies
>  *"""*
> 
> *result = search_criteria + outputs*

if you expected a '/' between the two bits here you're going to have to
add it yourself.

> *resultUrl = urljoin(baseUrl + result)*

you would need a comma here instead of a plus, since you're trying to
provide two things to join (I think you'd get a syntax error as written,
so maybe that's a transcription error, not something really wrong in
your code)

plus urljoin often doesn't work the way people expect. In your baseUrl
above you end with /query, and the query piece is going to be stripped
off when urljoin'ing... if the "base" is the path to some page, then
what you join to it will end relative to that location, not to the page
- a trailing slash would change the sense to what I think you're expecting.

Consider something like this:

baseUrl =
"http://data.parliament.uk/membersdataplatform/services/mnis/members/query/"
# house is the one you want to search on
query = "House=%s/Constituencies" % house
resultUrl = urljoin(baseUrl, query)

But you don't really need urljoin for this... you can just format up the
url string yourself, since you don't need any of the special behavior.



More information about the Tutor mailing list