简体   繁体   English

如何在编码字符串中添加字符? 蟒蛇

[英]How do I add characters to an encoded string? Python

I need to send a request from the backend I'm doing in Python to an API via URL. 我需要通过URL从我在Python中进行的后端向API发送请求。

The method requests.get(URL) automatically makes the respective encoding to UTF-8. 方法requests.get(URL)自动将相应的编码转换为UTF-8。

The problem is that the API page, when they are special characters, such as in Russian, German, Spanish, etc.... in the address goes the parameter (query, for example) and it is already sent encoded. 问题在于,当API页面是特殊字符(例如俄语,德语,西班牙语等)时,地址中的参数(例如查询)带有参数,并且已经过编码发送。

For example, the Russian word "Бавария Мюнхен" in UTF-8 becomes: 例如,UTF-8中的俄语单词“БаварияМюнхен”变为:

"%D0%91%D0%B0%D0%B2%D0%B0%D1%80%D0%B8%D1%8F%20%D0%9C%D1%8E%D0%BD%D1%85%D0%B5%D0%BD"

but it turns out that the url when entering manually from the website, to each % adds a 25, that is, it remains as %25 plus the letter that follows in the query encoded. 但是事实证明,从网站手动输入时,该网址会为每个%添加25,也就是说,它仍为%25加上查询编码后的字母。

在此处输入图片说明在此处输入图片说明

The website is RSS Dog . 该网站是RSS Dog

Questions: 问题:

  • How do I manually add that "%25"? 如何手动添加“%25”?
  • Is a coding method that does that in Python. 是一种在Python中执行此操作的编码方法。

Translated with www.DeepL.com/Translator :v 翻译成www.DeepL.com/Translator:v

The method in python that does it is urllib.quote and urllib.unquote . python中执行此操作的方法是urllib.quoteurllib.unquote

%25 is equal to % when unquoted, so you could unquote the entire string, insert them, and then reinsert them if you wanted, but it seems like you're accidentally double quoting the querystring. %25在取消引用时等于% ,因此您可以取消引用整个字符串,将它们插入,然后根据需要重新插入它们,但是似乎您不小心在双引号中加上了查询字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用Python编码字符? - How are characters encoded in Python? 如何在Python中替换字符串中的字符? - How do I replace characters in a string in Python? 如何在Python字符串中设置字符? - How do I set characters in a Python string? 如何获得字符保持相同顺序的部分编码字符串的所有可能组合? - How do I get all possible combinations of a partially encoded string where characters remain in same order? json编码为UTF-8字符。 如何在Python请求中作为json处理 - json encoded as UTF-8 characters. How do I process as json in Python Requests 如何在Python的字符串中添加@,/,\\,*之类的字符,而不将其读取为转义序列 - How do I add characters like @, /, \, * in a string in Python, without it reading it as an escape sequence Python-格式化JSON字符串时如何添加反斜杠字符? - Python - How do I add the backslash characters when formatting a JSON string? 如何在 Javascript 中生成与在 Python 3 中相同的 SHA256 编码字符串? - How do I make the same SHA256 encoded string in Javascript that I do in Python 3? 如何转换混合编码文件的字符 - how do I convert the characters of a mixed encoded file (Python)如何将字符添加到集合中的某个字符串? - (Python) How can I add characters to a certain string in a set?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM