0

Im writing this program that searches YouTube for a part of a bigger program

when the user inputs the search phrase it might have spaces but i don't want those spaces i want "+" instead so that the URL works

Here is my Code:

import urllib2
x=raw_input("Search: ")
site1 = urllib2.urlopen('http://www.youtube.com/results?search_query=%s'%x)
y = site1.read()
print y

when i input anything that's more than one word it doesn't work because there shouldn't be any spaces in a URL so how can i turn the spaces between words into plus signs

Thank You!

Serial
  • 7,925
  • 13
  • 52
  • 71

3 Answers3

4

Fortunately, urllib has urllib.quote_plus for that purpose. It will also help to escape everything else that needs escaping:

>>> urllib.quote_plus('hello how\'re you?')
'hello+how%27re+you%3F'
Dan Lecocq
  • 3,383
  • 25
  • 22
2

I don't quite see the problem:

>>> "This is a string".replace(" ", "+")
'This+is+a+string'
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
0

That should work.

x.replace(" ", "+")
James
  • 2,635
  • 5
  • 23
  • 30