-2

How do I remove %20 from url in my code?

elif 'launch' and 'open' in voice_note:
    print('opening...')
    start_url = "https://www."
    end_url = ".com"
    urllib.parse.unquote(voice_note)
    play_sound_from_polly('Displaying the result, sir')
    webbrowser.open(start_url + voice_note.replace('open', '').replace('%20', '') + end_url)
    exit()

Here is a image of what it opens image

2 Answers2

2

%20 is the url encoding for the space : ' ' character, so you want to strip or replace the ' ' in your url string.

Maxime
  • 818
  • 6
  • 24
0

Instead of doing this:

webbrowser.open(start_url + voice_note.replace('open', '').replace('%20', '') + end_url)

Use strip() function:

webbrowser.open(start_url + voice_note.replace('open', '').strip() + end_url)

Or simply put:

webbrowser.open(start_url + voice_note.replace('open', '').replace(' ', '') + end_url)

The reason is you are trying to replace '%20' and not a space. %20 is an encoded value of space in a url.

jose_bacoy
  • 12,227
  • 1
  • 20
  • 38