如何对URL中的阿拉伯语词汇编码及将截图URL编码为指定格式
Alright, let's tackle your two questions about Arabic URL encoding step by step—this is a common gotcha since URLs only speak ASCII, right?
URLs are restricted to ASCII characters, so any non-ASCII text (like Arabic) needs to be converted into percent-encoded format (each byte of the UTF-8 representation is prefixed with %). Here are reliable, practical ways to do it:
Using Python: Super straightforward. The
urllib.parse.quote()function handles UTF-8 encoding by default (perfect for Arabic script). Example:from urllib.parse import quote arabic_text = "ثورة زراعية باستخدام الاسم النانوية" encoded_text = quote(arabic_text) print(encoded_text) # Outputs the exact percent-encoded string from your target URLUsing JavaScript: In browsers or Node.js, use
encodeURIComponent()(avoidencodeURI()here—it skips characters needed for path segments). Example:const arabicText = "ثورة زراعية باستخدام الاسم النانوية"; const encodedText = encodeURIComponent(arabicText); console.log(encodedText); // Matches the encoded segment in your target URLManual Calculation (for understanding only): If you want to see the mechanics:
- Convert the Arabic text to UTF-8 bytes. For example, the character "ث" becomes the byte sequence
D8 AB. - Prefix each byte with
%, soD8 ABbecomes%D8%AB. - Concatenate all encoded bytes to form the full string.
Don't do this manually for real use cases—it's error-prone. Stick to the code methods above.
- Convert the Arabic text to UTF-8 bytes. For example, the character "ث" becomes the byte sequence
Assuming the original Arabic path segment in your screenshot is ثورة زراعية باستخدام الاسم النانوية, here's exactly how to build the target URL:
- Isolate the Arabic segment: Pull out the Arabic text from the screenshot's URL—this is the only part that needs encoding.
- Encode the Arabic text: Use either the Python or JavaScript method above. You'll get this encoded string:
%D8%AB%D9%88%D8%B1%D8%A9-%D8%B2%D8%B1%D8%A7%D8%B9%D9%8A%D8%A9-%D8%A8%D8%A7%D8%B3%D8%AA%D8%AE%D8%AF%D8%A7%D9%85-%D8%A7%D9%84%D8%A7%D9%94%D8%B3%D9%85%D8%AF%D8%A9-%D8%A7%D9%84%D9%86%D8%A7%D9%86%D9%88%D9%8A%D8%A9 - Assemble the full URL: Take the base URL
https://arabian-chemistry.com/wp-content/uploads/2018/01/, append the encoded Arabic string, then add the query parameter?x15001. That's your exact target URL.
Pro tip: If you're working with a full URL from the screenshot, make sure to only encode the path/query parts containing Arabic—never encode the scheme (https://) or domain (arabian-chemistry.com), as those must stay in plain ASCII.
内容的提问来源于stack exchange,提问作者Génius Chemist

