在另一个 URL 中发送 URL

Sending URL inside another URL

所以我正在用 python 构建一个电报机器人,我需要向用户发送一个 URL。我正在使用电报 send_text URL:

https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={chat_id}&parse_mode=Markdown&text={message}

但是我正在使用的URL:

https://www.amazon.es/RASPBERRY-Placa-Modelo-SDRAM-1822096/dp/B07TC2BK1X/ref=sr_1_3?__mk_es_ES=%C3%85M%C3%85%C5%BD%C3%95%C3%91&crid=YJ6X8FN3V801&keywords=raspberry+pi+4&qid=1577853490&sprefix=raspberr%2Caps%2C195&sr=8-3

有一个像 & 这样的特殊字符,可以防止消息以完整的 URL 发送。在这个 URL 的情况下,我只收到这个:

https://www.amazon.es/RASPBERRY-Placa-Modelo-SDRAM-1822096/dp/B07TC2BK1X/ref=sr13?mkesES=ÅMÅŽÕÑ

我尝试使用 utf-8 来替换像 & 这样的字符,但是 python 将它们转换回 "real character" 所以我不得不放弃这个想法。 如果您想查看我在这里尝试的代码片段:

url = url.replace('&', u"\x26")

有什么办法可以解决这个问题吗?

urlencode()

编码URL
import requests
import urllib.parse

link = "https://www.amazon.es/RASPBERRY-Placa-Modelo-SDRAM-1822096/dp/B07TC2BK1X/ref=sr_1_3?__mk_es_ES=%C3%85M%C3%85%C5%BD%C3%95%C3%91&crid=YJ6X8FN3V801&keywords=raspberry+pi+4&qid=1577853490&sprefix=raspberr%2Caps%2C195&sr=8-3"
markdownMsg = "[Click me!](" + urllib.parse.quote(link) + ")"
url = "https://api.telegram.org/bot<TOKEN>/sendMessage?chat_id=<ID>&text=" + markdownMsg + "&parse_mode=MarkDown"

response = requests.request("GET", url, headers={}, data ={})
print(response.text.encode('utf8'))

这也适用于 &parse_mode=HTML

htmlMsg = "<a href=\"" + urllib.parse.quote(link) + "\">Click me!</a>"