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

使用 beautifulsoup 解析网页非常的慢,有什么同类产品可以替代么?

  •  
  •   halfcrazy ·
    halfcrazy · 2014-04-28 13:37:03 +08:00 · 14877 次点击
    这是一个创建于 3640 天前的主题,其中的信息可能已经有所发展或是发生改变。
    另 如果单纯用正则匹配的话,效率如何?
    26 条回复    2014-05-06 16:11:27 +08:00
    for4
        1
    for4  
       2014-04-28 13:40:01 +08:00
    halfcrazy
        2
    halfcrazy  
    OP
       2014-04-28 13:49:25 +08:00
    @for4 如果性能能相差十倍,确实很诱人,这就试试去
    qonco
        3
    qonco  
       2014-04-28 13:51:22 +08:00
    jsoup
    qonco
        4
    qonco  
       2014-04-28 13:51:48 +08:00
    正则不是用来匹配html的
    Ever
        5
    Ever  
       2014-04-28 13:56:30 +08:00
    @halfcrazy 美丽汤指定第二个参数为lxml就能走lxml parser, 不用重写。
    halfcrazy
        6
    halfcrazy  
    OP
       2014-04-28 13:56:57 +08:00
    @qonco jsoup是java的啊,另我的意思是只用正则来解析网页提取内容。
    halfcrazy
        7
    halfcrazy  
    OP
       2014-04-28 14:02:15 +08:00
    @Ever 是这样么? soup = BeautifulSoup(page,"lxml")
    halfcrazy
        8
    halfcrazy  
    OP
       2014-04-28 14:09:41 +08:00
    @Ever 用了这个lxml’s HTML parser效果似乎不是很明显啊
    bilipan
        9
    bilipan  
       2014-04-28 15:48:20 +08:00
    pyquery可以试下,语法跟jquery类似
    binux
        10
    binux  
       2014-04-28 15:51:04 +08:00
    正则比xml建树快得多,直接用xpath,比soup,pyquery快。
    即便如此,lxml单进程每秒30个页面还是没问题的。加大并发就好了。
    flyer103
        11
    flyer103  
       2014-04-28 16:31:33 +08:00 via Android
    @binux 想问下 “lxml单进程每秒30个页面还是没问题的” 是如何测出来的,平均获取单个页面中的数据条目有多少?
    binux
        12
    binux  
       2014-04-28 16:35:01 +08:00
    @flyer103 timeit,每个页面80条xpath规则
    andyhu
        13
    andyhu  
       2014-04-28 16:44:22 +08:00
    可以不用python吗?nodejs+cheerio非常爽,完全jquery的语法解析,速度也很快
    kxxoling
        14
    kxxoling  
       2014-04-28 17:41:22 +08:00 via iPad
    bs有坑啊!lxml!
    187j3x1
        15
    187j3x1  
       2014-04-28 17:46:35 +08:00
    匹配一堆相同内容 正则舒服很多 能正则就正则
    dreasky
        16
    dreasky  
       2014-04-28 17:55:49 +08:00
    亲测正则的速度快最灵活
    a2z
        17
    a2z  
       2014-04-28 18:17:35 +08:00
    bs4
    tomnee
        18
    tomnee  
       2014-04-28 18:38:45 +08:00
    pyquery, 套的lxml, 性能比bs好,用起来比较简单。
    daiv
        19
    daiv  
       2014-04-28 18:51:54 +08:00
    pyquery,用起来还是很舒服的
    walleL
        20
    walleL  
       2014-04-28 19:38:57 +08:00   ❤️ 2


    不知道大家有没有注意过这个功能, 很赞啊
    okidogi
        21
    okidogi  
       2014-04-28 23:36:25 +08:00
    beautifulsoup4 使用的就是lxml的库,应该会快一些。

    pip install beautifulsoup4
    halfcrazy
        22
    halfcrazy  
    OP
       2014-04-28 23:54:33 +08:00
    @okidogi 默认用的是html.parser吧 ,lxml好像要手动指定
    chevalier
        23
    chevalier  
       2014-04-29 00:10:52 +08:00
    @walleL 一直在用,写爬虫XPath解析网页必备
    orancho
        24
    orancho  
       2014-04-29 08:36:57 +08:00 via Android
    nokigiri
    Ever
        25
    Ever  
       2014-04-29 11:47:12 +08:00
    @halfcrazy 是不是单个文档过大? 数量大试试开线程池解决, lxml会释放GIL, 能有效利用多核。
    remnet
        26
    remnet  
       2014-05-06 16:11:27 +08:00
    beautifulsoup 用过 感觉的确挺慢的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3801 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 05:05 · PVG 13:05 · LAX 22:05 · JFK 01:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.