Giter Site home page Giter Site logo

中文url的问题 about bark HOT 4 CLOSED

finb avatar finb commented on May 14, 2024
中文url的问题

from bark.

Comments (4)

wnma3mz avatar wnma3mz commented on May 14, 2024 2

@Finb 十分感谢!我是用Python3实现的,实现代码如下

import requests
import urllib

def encode_text(text):
    return urllib.parse.quote(text.encode('utf-8'))

# 对中文文字编码
chinese_text = '2019年诺贝尔经济学奖'
url = 'https://wiki.mbalib.com/wiki/' + encode_text(chinese_text)

# 再对整个url编码
url = encode_text(url)

from bark.

Finb avatar Finb commented on May 14, 2024

你好
因为你这整个URL是当做url字段的参数,所以需要对整个URL进行编码(encodeURIComponent)
像这样

https%3a%2f%2fwiki.mbalib.com%2fwiki%2f2019%e5%b9%b4%e8%af%ba%e8%b4%9d%e5%b0%94%e7%bb%8f%e6%b5%8e%e5%ad%a6%e5%a5%96

然后将编码后的结果当成推送url的参数

from bark.

wnma3mz avatar wnma3mz commented on May 14, 2024

很感谢作者的及时回复,但我进行尝试后,发现依旧不行

from bark.

Finb avatar Finb commented on May 14, 2024

@wnma3mz 确实不行,忘了还有一步,问题出在URL中有中文
实际上url中是不能有中文的,中文会导致Swift初始化URL对象时出错

//现在还不是一个合法的url,因为url中有中文
var url:String = "https://wiki.mbalib.com/wiki/2019年诺贝尔经济学奖"; 

//将URL路径或参数部分的特殊字符或中文编码,使url合法。编码后url的值变为:
//https://wiki.mbalib.com/wiki/2019%E5%B9%B4%E8%AF%BA%E8%B4%9D%E5%B0%94%E7%BB%8F%E6%B5%8E%E5%AD%A6%E5%A5%96
url = url.encodeURI();

//将整个url编码,当做推送URL的参数部分
url = url.encodeURIComponent(); 
//最后的Url是
//https%3A%2F%2Fwiki.mbalib.com%2Fwiki%2F2019%25E5%25B9%25B4%25E8%25AF%25BA%25E8%25B4%259D%25E5%25B0%2594%25E7%25BB%258F%25E6%25B5%258E%25E5%25AD%25A6%25E5%25A5%2596

//把这个Url当做url的参数推送出去就可以了

建议所有url都经过 url.encodeURI().encodeURIComponent() 处理后,再当做推送链接的url参数值,基本就没问题了
encodeURI() 方法是对一个url进行编码,使url合法
encodeURIComponent()方法是对 一个字符串,整体都url编码
这里是js里的方法,不过其他语言都能找到对应实现

from bark.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.