V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
laoyuan
V2EX  ›  Python

怎样将 url 中非 ASCII 字符的部分 urlencode, UTF-8 环境

  •  
  •   laoyuan ·
    laoyuan · Jul 19, 2015 · 2939 views
    This topic created in 3944 days ago, the information mentioned may be changed or developed.
    url = 'http://doman.com/中文' #有些不太正规的网站url 就是这样任性的

    得到 http://doman.com/%E4%B8%AD%E6%96%87

    urllib.quote( )不行,误伤冒号了
    Supplement 1  ·  Jul 19, 2015
    误伤url 中所有需要转义的非字母ASCII字符
    5 replies    2015-07-23 14:22:40 +08:00
    lixia625
        1
    lixia625  
       Jul 19, 2015
    先把 http:// 拿走
    laoyuan
        2
    laoyuan  
    OP
       Jul 19, 2015
    @lixia625 误伤太多,比如 url = 'http://domain.com/?中文&+,' 见 rfc3986
    kaneg
        3
    kaneg  
       Jul 22, 2015
    reserved = ';/:@&=+$,'
    print urllib.quote(url, reserved)

    不过奇怪的是,唯独?不能加入reserved,否则中文都变成?了
    laoyuan
        4
    laoyuan  
    OP
       Jul 22, 2015   ❤️ 1
    @kaneg 不止这些字符,~ 在url 里也很常见。我感觉应该有什么包可以,反正粘到浏览器地址栏里,浏览器可以正确的转义
    jack139
        5
    jack139  
       Jul 23, 2015
    好像有个urllib.quote_plus,或是urllib2,记不清了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5690 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 64ms · UTC 07:40 · PVG 15:40 · LAX 00:40 · JFK 03:40
    ♥ Do have faith in what you're doing.