推荐学习书目
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 · Aug 5, 2018 · 3638 views
    This topic created in 2849 days ago, the information mentioned may be changed or developed.

    同一个页面,在 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 replies    2018-08-06 11:31:27 +08:00
    MikePerfect
        1
    MikePerfect  
       Aug 5, 2018
    你确定你的第一行加入了#utf-8 的标志吗
    zyqf
        2
    zyqf  
       Aug 5, 2018 via Android
    Python 3
    ClutchBear
        3
    ClutchBear  
       Aug 5, 2018
    page_req.encoding = page_req.apparent_encoding
    在 soup 那一行之前加一行这个.
    ipwx
        4
    ipwx  
       Aug 5, 2018
    print 一个 list,会被 repr,不是很自然嘛?

    你就不能把 select 出来的结果遍历一下嘛?
    Sylv
        5
    Sylv  
       Aug 5, 2018   ❤️ 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
    MilkShake
        6
    MilkShake  
       Aug 6, 2018 via iPhone
    为什么不使用 Python3
    cxxcoding
        7
    cxxcoding  
       Aug 6, 2018
    为什么不使用 Python3

    看我主页
    talen666
        8
    talen666  
       Aug 6, 2018
    新手还拿旧版本练手。。。
    jamwong9
        9
    jamwong9  
       Aug 6, 2018
    python3
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   861 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 21:42 · PVG 05:42 · LAX 14:42 · JFK 17:42
    ♥ Do have faith in what you're doing.