How to join a string to a URL in Python?
Question: I am trying to join a string in a URL, but the problem is that since it's
spaced the other part does not get recognized as part of the URL.

Here would be an example:

    
    
    import urllib
    import urllib2
    website = "http://example.php?id=1 order by 1--"
    request = urllib2.Request(website)
    response = urllib2.urlopen(request)
    html = response.read()
    

The "`order by 1--`" part is not recognized as part of the URL.


Answer: You should better use
[`urllib.urlencode`](http://docs.python.org/2/library/urllib.html#urllib.urlencode)
or
[`urllib.quote`](http://docs.python.org/2/library/urllib.html#urllib.quote):

    
    
    website = "http://example.com/?" + urllib.quote("?id=1 order by 1--")
    

or

    
    
    website = "http://example.com/?" + urllib.urlencode({"id": "1 order by 1 --"})
    

and about the query you're trying to achieve:

![](http://imgs.xkcd.com/comics/exploits_of_a_mom.png)

I think you're forgetting a `;` to end the first query.

