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

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

  •  
  •   liaipeng · 2015-07-27 11:33:31 +08:00 · 2982 次点击
    这是一个创建于 3198 天前的主题,其中的信息可能已经有所发展或是发生改变。
    f = urllib.urlopen(url).read()
    soup = BeautifulSoup(f, 'html.parser')

    如上面的代码,f打印出来看了是完整的页面,有几百行,但是把soup打印出来只有60多行。爬取其他网页的数据整成,就是爬百度贴吧的帖子会出现这种情况,是什么原因呢?
    12 条回复    2015-07-28 15:54:02 +08:00
    WhiteLament
        1
    WhiteLament  
       2015-07-27 11:59:08 +08:00   ❤️ 1
    'html.parser' 换成 'lxml' 试试?
    lingo233
        2
    lingo233  
       2015-07-27 12:01:47 +08:00
    我记得贴吧未登录只能看一页的内容。
    iyaozhen
        3
    iyaozhen  
       2015-07-27 12:02:53 +08:00
    2 楼应该是真相。
    liaipeng
        4
    liaipeng  
    OP
       2015-07-27 12:12:08 +08:00
    @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
       2015-07-27 12:13:20 +08:00
    @lingo233 不是的,现在是soup连主楼的内容都没有抓取完整
    yappa
        6
    yappa  
       2015-07-27 12:16:03 +08:00   ❤️ 1
    html.parser改成lxml,或者html5lib,这两个模块都要先安装
    liaipeng
        7
    liaipeng  
    OP
       2015-07-27 12:20:29 +08:00
    @yappa
    好的,我试试
    liaipeng
        8
    liaipeng  
    OP
       2015-07-27 12:31:18 +08:00
    @yappa 可以了!太感谢了。想知道为什么会有这种情况呢?是因为其他网页跟贴吧帖子的什么不同?
    WhiteLament
        9
    WhiteLament  
       2015-07-27 12:36:55 +08:00
    你没安装
    pip install lxml
    yappa
        10
    yappa  
       2015-07-27 12:38:54 +08:00
    估计你是从文档里面复制出来的代码,“html.parser”是“html解析器”的意思,你要找到适合的解析器,lxml,html5lib就是所谓的“html.parser"。
    WhiteLament
        11
    WhiteLament  
       2015-07-27 12:39:11 +08:00
    有些页面不够规范,不同解析器兼容不一样,造成结果不同。
    我也遇到过,换一个解析器就好了
    liaipeng
        12
    liaipeng  
    OP
       2015-07-28 15:54:02 +08:00
    @WhiteLament
    @yappa
    感谢两位!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3664 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 04:26 · PVG 12:26 · LAX 21:26 · JFK 00:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.