[Tutor] Tutor Digest, Vol 160, Issue 26

Jerry Hill malaclypse2 at gmail.com
Fri Jun 23 16:18:42 EDT 2017


On Tue, Jun 20, 2017 at 4:34 AM, angela ebirim <cebirim at gmail.com> wrote:

> ​​
> 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*
> *
> ​​
> resultUrl =
> ​​
> urljoin(baseUrl + result)*
>
>
​Your strings aren't what you think they are.

When you run
result ​
​
= search_criteria + outputs
That concatenates the two string together, so result is now "
​
House=Commons
​
Constituencies"

The same thing happens again on the next line.  You call
​
urljoin(baseUrl + result), but that isn't doing quite what you think
either.  Since you tell python to add those two stings together, it does
that first, then passes the result to urljoin.

In both cases, you're losing the '/' that ought to separate the pieces of
your URL.

Try this instead:

result
​​
= search_criteria + "/" + outputs
resultUrl =
​​urljoin(baseUrl, result)


That should get you what you're looking for.

As a more general piece of debugging advice, when things start to go wrong
it's usually worth printing out the variables you're working with.  That
way you can make sure that they match your expectations. If you'd done that
in this case, you would have immediately seen that your resultUrl was being
built incorrectly.

Also, it was super helpful to have comments saying what your expected
results were.  That makes it easier for someone else to come along and
immediately understand what you're trying to accomplish, and see where you
might have gone wrong.

-- 
Jerry


More information about the Tutor mailing list