V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
fyooo
V2EX  ›  Python

Python requests 库怎么对 get 参数的汉字做编码?

  •  
  •   fyooo · 2017-03-29 12:07:59 +08:00 · 3006 次点击
    这是一个创建于 2578 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如我想把 苹果 作为 get 参数发送到后台,在浏览器上是可以的,因为浏览器会帮我编码成 %E8%8B%B9%E6%9E%9C,但是 python 的 requests 库就不知道怎么搞了

    url_pre =  "http://127.0.0.1/seq?kw="
    
    kws = [
        "苹果"
    ]
    
    for key in kws:
        r = requests.get(url_pre + key)
        print r
    

    因为运行会提示出错 SyntaxError: Non-ASCII character '\xe6' in file

    第 1 条附言  ·  2017-03-29 14:33:24 +08:00
    加了 coding 就搞定了,我是用 python2 的
    # -*- coding:utf-8 -*-
    # coding:utf-8
    13 条回复    2017-03-29 14:33:06 +08:00
    Mitt
        1
    Mitt  
       2017-03-29 12:19:14 +08:00 via iPhone
    怎么感觉是 python 或者定义字符串的问题 并不是库的问题
    jingniao
        2
    jingniao  
       2017-03-29 12:22:47 +08:00 via Android
    应该不是这么拼的吧,另外报错是文件编码问题
    bxb100
        3
    bxb100  
       2017-03-29 12:27:00 +08:00 via Android
    不是 requests 库的问题
    Kilerd
        4
    Kilerd  
       2017-03-29 12:40:13 +08:00
    > 因为浏览器会帮我编码成 %E8%8B%B9%E6%9E%9C

    所以你的问题不就是把文字转换成 URL 编码咯 ?

    自己都提出问题了,随手百度都是找得到的吧
    mgna17
        5
    mgna17  
       2017-03-29 12:40:34 +08:00   ❤️ 1
    #coding: utf-8

    您可能旧版软件的受害者
    Kilerd
        6
    Kilerd  
       2017-03-29 12:41:15 +08:00
    善用关键字搜索 “ python 汉字 URL ”

    祝你好运。
    Deluxe
        7
    Deluxe  
       2017-03-29 12:42:13 +08:00 via Android
    urllib.parse.quote(key)
    xxwar
        8
    xxwar  
       2017-03-29 12:56:29 +08:00
    response = requests.get(url_pre + key)
    response.encoding = "gbk"

    requests 有时候不能正确识别网页编码
    shuax
        9
    shuax  
       2017-03-29 13:26:09 +08:00
    你可能是 python2 的受害者
    ic2y
        10
    ic2y  
       2017-03-29 13:27:09 +08:00   ❤️ 1
    代码头部 加上这两行

    # -*- coding:utf-8 -*-
    # coding:utf-8

    默认 python 源代码文件不能输入汉字,加上这两行就行了
    VicYu
        11
    VicYu  
       2017-03-29 14:02:41 +08:00
    In [1]: import requests

    In [2]: requests.utils.quote("中文")
    Out[2]: '%E4%B8%AD%E6%96%87'


    urllib.urlencode
    urllib.quote
    urllib.unquote
    Kallen
        12
    Kallen  
       2017-03-29 14:05:30 +08:00
    urllib.parse.quote()

    反之,还原请求参数用: urllib.parse.unquote()
    fyooo
        13
    fyooo  
    OP
       2017-03-29 14:33:06 +08:00
    谢谢 #5 @mgna17 #10 @ic2y

    加了 coding 就搞定了,我是用 python2 的
    # -*- coding:utf-8 -*-
    # coding:utf-8
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5216 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 08:23 · PVG 16:23 · LAX 01:23 · JFK 04:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.