Python3 replace string with another string

i have this python3 code

urls = ['https://example.com/']
for url in urls:
    response = requests.get(url)
    soup = BeautifulSoup(response.content, "html.parser")
    pattern = re.compile(r'https(.*?)m3u8')
    script = soup.find("script", text=pattern)
    print(pattern.search(script.text).group(0))

when run it prints this url

https://example.com/live\u002Dhls/amlst:bla\u002Dsd\u002D6359b748af746aa988a1536f82a19924578bd4b37e51197389a70775558f3cbf_trns_h264/playlist.m3u8

i need to replace \u002D with -

how to do that so url prints like this

https://example.com/live-hls/amlst:bla-sd-6359b748af746aa988a1536f82a19924578bd4b37e51197389a70775558f3cbf_trns_h264/playlist.m3u8

thanks

Hi bob123...

I don't use BeautifulSoup but you could try putting it into a variable, untested:

MYURL=str(pattern.search(script.text).group(0))
print(MYURL)

hi thanks for reply

that just prints the same url with no change

i need to replace \u002D with -

Not sure I understand your request. Do you want to enhance your python3 script to do the replacement / interpretation within, or do you want to process its output afterwards with a standard *nix text tool?

yes i want to

enhance my python3 script to do the replacement

from this

https://example.com/live\u002Dhls/amlst:bla\u002Dsd\u002D6359b748af746aa988a1536f82a19924578bd4b37e51197389a70775558f3cbf_trns_h264/playlist.m3u8

replacing \u002D with -

so url prints like this

https://example.com/live-hls/amlst:bla-sd-6359b748af746aa988a1536f82a19924578bd4b37e51197389a70775558f3cbf_trns_h264/playlist.m3u8

Hi bob123...
Which version of python 3.x.x are you using, OS, and machine...

--- Post updated at 11:41 PM ---

I hope this does not attach itself to the previous...
It looks like you are using an early version of Python 3.x.x, so untested try this...

MYURL=str(pattern.search(script.text).group(0))
MYURL=str.replace(MYURL, '\u002D', '-')
print(MYURL)
1 Like

hi thanks for reply

im using

python --version

Python 3.7.6 in ubuntu

and the same in windows Python 3.7.6

this did not replace
\u002D -

MYURL=str(pattern.search(script.text).group(0)) MYURL=str.replace(MYURL, '\u002D', '-') print(MYURL)

If you are using one line it HAS to be:

MYURL=str(pattern.search(script.text).group(0)); MYURL=str.replace(MYURL, '\u002D', '-'); print(MYURL)
1 Like

0k got it this works just added a r after MYURL

    MYURL=str(pattern.search(script.text).group(0))
    MYURL=str.replace(MYURL, r'\u002D', '-')
    print(MYURL)

thank you

1 Like