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
lsj8924
V2EX  ›  Python

一个 py3 的坑,自己想了几天结果。获取的网页字符串是\xb6\xd4\xb6\xcc\xd0\xc5\格式的, py3 没有 str.decode()

  •  
  •   lsj8924 · 2018-10-05 21:51:53 +08:00 · 3577 次点击
    这是一个创建于 2023 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如图,我爬虫获取到的是一个 dict。现在我要把字典的中文解析出来。我用 py2 是这么干的: 主要是用了字符串的 decode。但是 py3 没有这个方法啊。 而且这个 dict 是用 requests 获取出来的 response.headers 获取出来就是字符串 dict,不能是 bytes。我自己试了用 bytes(s,"gbk"),str(s,"gbk")都没用。自己也想了几天,没啥结果。别跟我说在字符串前面加个 b 变为 bytes,问题是变量不能加 b 啊。

    13 条回复    2018-10-07 12:01:22 +08:00
    jianzhao123
        1
    jianzhao123  
       2018-10-05 21:54:07 +08:00 via Android
    哈哈哈,我前段时间也遇到了,放弃了后来……
    leoleoasd
        2
    leoleoasd  
       2018-10-05 21:57:21 +08:00
    eval("ans = b'{}'".format(str))
    lhx2008
        3
    lhx2008  
       2018-10-05 21:58:08 +08:00   ❤️ 2
    data.encode("raw_unicode_escape")
    XIVN1987
        4
    XIVN1987  
       2018-10-05 21:59:53 +08:00
    先 encode 再 decode 即可:

    In [3]: s.encode('latin')
    Out[3]: b'\xb6\xd4\xb6\xcc\xd0\xc5'

    In [4]: s.encode('latin').decode('gbk')
    Out[4]: '对短信'
    lhx2008
        5
    lhx2008  
       2018-10-05 22:00:34 +08:00
    "\xd3\xf1\xc1\xfa".encode("raw_unicode_escape").decode("gbk")
    Out[5]: '玉龙'
    fhefh
        6
    fhefh  
       2018-10-05 22:07:01 +08:00
    ![]( )


    先检查 text 是什么类型如果 type(text) is bytes,那么 text.decode('unicode_escape')

    如果 type(text) is str,那么 text.encode('latin-1').decode('unicode_escape')

    链接: https://www.zhihu.com/question/26921730/answer/49625649
    fhefh
        7
    fhefh  
       2018-10-05 22:13:16 +08:00
    sobigfish
        8
    sobigfish  
       2018-10-05 22:37:31 +08:00
    呃,我不是很懂 python
    更像是 lz 那个是 dict 不知道怎么转 str 的问题

    搜一下还是能找到解决方案。 没测试过中文文件名 lz 自己测试一下
    r = requests.get('http://httpbin.org/image/jpeg')
    headers = str.encode(json.dumps(dict(r.headers)))
    print(headers.decode('gbk'))
    lsj8924
        9
    lsj8924  
    OP
       2018-10-05 22:43:13 +08:00
    @fhefh @lhx2008 @XIVN1987 @leoleoasd 感谢各位大佬为我解惑。
    ClutchBear
        10
    ClutchBear  
       2018-10-06 08:25:37 +08:00
    既然是爬虫,
    response = requests.get(url)
    response.encoding="utf-8"
    就直接是能看懂的汉字了,
    那用这么麻烦...
    vipfts
        11
    vipfts  
       2018-10-06 10:20:43 +08:00
    print(str(b'\xb6\xd4\xb6\xcc\xd0\xc5', encoding="GBK"))
    v2014
        12
    v2014  
       2018-10-07 09:25:29 +08:00
    自己实现 http.client.parse_headers 方法

    https://www.dust8.com/2017/12/04/header-parsing-error/
    evilhexxx
        13
    evilhexxx  
       2018-10-07 12:01:22 +08:00
    网络编程中常见的编码问题
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   970 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 19:40 · PVG 03:40 · LAX 12:40 · JFK 15:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.