推荐学习书目
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
liaipeng
V2EX  ›  Python

用 BeautifulSoup 分析百度贴吧的页面,为什么只能提取前 60 多行的数据呢?

  •  
  •   liaipeng · Jul 27, 2015 · 3744 views
    This topic created in 3953 days ago, the information mentioned may be changed or developed.
    f = urllib.urlopen(url).read()
    soup = BeautifulSoup(f, 'html.parser')

    如上面的代码,f打印出来看了是完整的页面,有几百行,但是把soup打印出来只有60多行。爬取其他网页的数据整成,就是爬百度贴吧的帖子会出现这种情况,是什么原因呢?
    12 replies    2015-07-28 15:54:02 +08:00
    WhiteLament
        1
    WhiteLament  
       Jul 27, 2015   ❤️ 1
    'html.parser' 换成 'lxml' 试试?
    lingo233
        2
    lingo233  
       Jul 27, 2015
    我记得贴吧未登录只能看一页的内容。
    iyaozhen
        3
    iyaozhen  
       Jul 27, 2015
    2 楼应该是真相。
    liaipeng
        4
    liaipeng  
    OP
       Jul 27, 2015
    @WhiteLament
    提示这个,对BeautifulSoup模块还不熟悉,第一次接触
    Couldn't find a tree builder with the features you requested: lxml.parser. Do you need to install a parser library?
    liaipeng
        5
    liaipeng  
    OP
       Jul 27, 2015
    @lingo233 不是的,现在是soup连主楼的内容都没有抓取完整
    yappa
        6
    yappa  
       Jul 27, 2015   ❤️ 1
    html.parser改成lxml,或者html5lib,这两个模块都要先安装
    liaipeng
        7
    liaipeng  
    OP
       Jul 27, 2015
    @yappa
    好的,我试试
    liaipeng
        8
    liaipeng  
    OP
       Jul 27, 2015
    @yappa 可以了!太感谢了。想知道为什么会有这种情况呢?是因为其他网页跟贴吧帖子的什么不同?
    WhiteLament
        9
    WhiteLament  
       Jul 27, 2015
    你没安装
    pip install lxml
    yappa
        10
    yappa  
       Jul 27, 2015
    估计你是从文档里面复制出来的代码,“html.parser”是“html解析器”的意思,你要找到适合的解析器,lxml,html5lib就是所谓的“html.parser"。
    WhiteLament
        11
    WhiteLament  
       Jul 27, 2015
    有些页面不够规范,不同解析器兼容不一样,造成结果不同。
    我也遇到过,换一个解析器就好了
    liaipeng
        12
    liaipeng  
    OP
       Jul 28, 2015
    @WhiteLament
    @yappa
    感谢两位!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2745 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 02:15 · PVG 10:15 · LAX 19:15 · JFK 22:15
    ♥ Do have faith in what you're doing.