将字符串转换为UTF-8编码的URL的正确方法是什么?


Recieverphno = input("Please enter the phone number of the person you want to send the 
whatsapp message to: ")
Text = input('Please enter the text you want this person to recieve: ')
n = input('Please enter the number of times you want to send the person: ')
Text.replace(" ", "%20")
Text.replace("?", "%3F")
Text.replace("!", "%21")
Text.replace(",", "%2C")
WhatsappAPIURL = str('https://wa.me/'+Recieverphno+'?text='+Text)
print(WhatsappAPIURL)

我想替换字符串并以UTF-8格式编码

为此,您需要urlencoder

在python 3中可以使用urllib.parse.quote…

import urllib.parse
WhatsappAPIURL = urllib.parse.quote(str('https://wa.me/'+Recieverphno+'?text='+Text))

可选,如果您已经在项目中使用请求,

from requests.utils import requote_uri
requote_uri(str('https://wa.me/'+Recieverphno+'?text='+Text))

相关内容

最新更新