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

新手做爬虫, list 结构如何 encode 输出能看的中文字? 如何破?

  •  
  •   pppguest3962 · 2018-08-05 12:45:59 +08:00 · 2920 次点击
    这是一个创建于 2090 天前的主题,其中的信息可能已经有所发展或是发生改变。

    同一个页面,在 pycharm 的输出区,soup.title 是能看的中文字。。。 soup.select 检索到的内容,是\u661f\u671f\u65e5\xa0\xa0 这样的乱码。。。 在网上找这个问题,得到到原因(这个也是网文作者臆测吧?我自己没有能力核实)是因为 BeautifulSoup 在处理问题的时候,都是 UTF-8 视之,

    第一问题: 既然 BeautifulSoup 全是 UTF8 一刀切,那么我又搞不懂为什么 title()可以正常输出中文??

    按照网上攻略套路,那么我必须得把 select()方法的内容 encode 成 GBK 或者 GB2312, 按目前我的理解,BeautifulSoup 处理的方法,返回的是 list 结构而不是 str

    这里问题来了,我见到网上的例子,结构居然可以用 encode(),为啥我的 pycharm + py 2.7 不行,没 encode 方法? 传送门: https://www.jianshu.com/p/69401b84419e https://www.jb51.net/article/49220.htm 这两个文章是不是在害人?

    page_req = requests.get(url,headers=headers)
    soup = BeautifulSoup(page_req.text,'html.parser')
    print(soup.title)
    #title 输出正常
    print(soup.select('.fl.ps'))
    #select()输出的是\u661f\u671f\u65e5\xa0\xa0,UTF8 编码
    
    9 条回复    2018-08-06 11:31:27 +08:00
    MikePerfect
        1
    MikePerfect  
       2018-08-05 13:28:16 +08:00
    你确定你的第一行加入了#utf-8 的标志吗
    zyqf
        2
    zyqf  
       2018-08-05 13:48:11 +08:00 via Android
    Python 3
    ClutchBear
        3
    ClutchBear  
       2018-08-05 16:21:07 +08:00
    page_req.encoding = page_req.apparent_encoding
    在 soup 那一行之前加一行这个.
    ipwx
        4
    ipwx  
       2018-08-05 16:49:59 +08:00
    print 一个 list,会被 repr,不是很自然嘛?

    你就不能把 select 出来的结果遍历一下嘛?
    Sylv
        5
    Sylv  
       2018-08-05 16:52:31 +08:00   ❤️ 3
    不不不,这个问题和编码什么的没有关系。这个问题的实质是:Python 2 在 print list 等容器数据时,输出的是内部元素的 __repr__ 值,而不是 __str__ 值。

    >>> print('你好'.__str__())
    你好
    >>> print('你好'.__repr__())
    '\xe4\xbd\xa0\xe5\xa5\xbd'
    >>> print(['你好'])
    ['\xe4\xbd\xa0\xe5\xa5\xbd']
    >>> print(['你好'][0])
    你好
    >>> '你好' == '\xe4\xbd\xa0\xe5\xa5\xbd'
    True
    >>> ['你好'] == ['\xe4\xbd\xa0\xe5\xa5\xbd']
    True

    所以 '\xe4\xbd\xa0\xe5\xa5\xbd' 并不是乱码,它和 '你好' 是等价的,只是表示形式的不同,前者是给解释器看的,后者是给人看的。至于 print(['你好']) 为什么要显示成 ['\xe4\xbd\xa0\xe5\xa5\xbd'] 而不是 ['你好'],其实没啥为什么,Python 2 就是这样设计的。

    那我想显示 list 里的中文怎么办?

    方法一:
    改用 Python 3。
    >>> print(['你', '好'])
    ['你', '好']

    方法二:
    >>> for i in ['你', '好']:
    ... print(i)



    方法三:
    >>> import json
    >>> print(json.dumps(['你', '好'], ensure_ascii=False))
    ["你", "好"]

    方法四:
    https://stackoverflow.com/a/45841899
    aaa5838769
        6
    aaa5838769  
       2018-08-06 08:49:03 +08:00 via iPhone
    为什么不使用 Python3
    cxxcoding
        7
    cxxcoding  
       2018-08-06 09:49:03 +08:00
    为什么不使用 Python3

    看我主页
    talen666
        8
    talen666  
       2018-08-06 09:51:48 +08:00
    新手还拿旧版本练手。。。
    jamwong9
        9
    jamwong9  
       2018-08-06 11:31:27 +08:00
    python3
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5611 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 02:32 · PVG 10:32 · LAX 19:32 · JFK 22:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.