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

bs4(beautiful soup 4)简单抓取示例

  •  
  •   tanteng ·
    tanteng · 2015-05-12 11:46:16 +08:00 · 4115 次点击
    这是一个创建于 3280 天前的主题,其中的信息可能已经有所发展或是发生改变。

    参照bs4官方中文文档 http://beautifulsoup.readthedocs.org/en/latest/ 照着写了个简单的例子,感觉它非常强大!!

    import urllib.request
    from bs4 import BeautifulSoup
    import sys
    
    url = 'http://www.tantengvip.com'
    html = urllib.request.urlopen(url).read()
    
    soup = BeautifulSoup(html)
    h1s = soup.find_all('h1',class_='entry-title')
    
    '''打印所有H1标题和链接'''
    for h1 in h1s:
        #print(h1)
        print(h1.get_text())
        print(h1.a.attrs['href'])
    

    结果:

    D:\learn-python>python test.py
    beautiful soul 4 for Python3中文文档
    http://www.tantengvip.com/2015/05/beautiful-soul-4-for-pytho
    Python文件名不能和已有模块名重名
    http://www.tantengvip.com/2015/05/python-filename/
    Python3 requests包抓取并保存网页源码
    http://www.tantengvip.com/2015/05/requests-html/
    Python作用域代码演示
    http://www.tantengvip.com/2015/05/python-scope/
    深入理解django url原理
    http://www.tantengvip.com/2015/05/django-url/
    使用Mac终端svn命令从SAE检出代码
    http://www.tantengvip.com/2015/05/mac-svn-sae/
    使用pip安装Django for Python3
    http://www.tantengvip.com/2015/05/django-python3-install/
    Python字典get方法小例子
    http://www.tantengvip.com/2015/05/python-dict-get/
    Python减少循环层次和缩进技巧
    http://www.tantengvip.com/2015/05/python-for-level/
    Python中newinit的区别
    http://www.tantengvip.com/2015/05/python-new-init/
    Python中requirements.pip文件的作用
    http://www.tantengvip.com/2015/05/python-requirements-pip/
    Python中name的含义
    http://www.tantengvip.com/2015/05/python-name/
    用python列举牌类游戏斗牛中所有牛牛的情况
    http://www.tantengvip.com/2015/05/python-niu-niu/
    ThinkPHP标签if condition嵌套bug
    http://www.tantengvip.com/2015/05/thinkphp-if-condition-bug/
    在SAE上部署django
    http://www.tantengvip.com/2015/05/sae-django/
    python中文基础教程
    http://www.tantengvip.com/2015/05/python-guide-chinese/

    9 条回复    2015-05-12 23:03:45 +08:00
    latyas
        1
    latyas  
       2015-05-12 13:51:12 +08:00
    已从bs4跳到pyquery了
    wensonsmith
        2
    wensonsmith  
       2015-05-12 14:25:48 +08:00
    看起来很不错啊!
    tanteng
        3
    tanteng  
    OP
       2015-05-12 15:18:07 +08:00
    @latyas 我只知道bs4。。。所以就用了,到底哪个更主流
    zztt168
        4
    zztt168  
       2015-05-12 16:18:18 +08:00 via Android
    @latyas 请教pyquery与bs4比较有什么优势,和楼主一样用bs比较多,其他的不懂,谢谢。
    ahxxm
        5
    ahxxm  
       2015-05-12 17:25:37 +08:00
    曾经用过一阵子bs4,发现在vps上会用满cpu,之后就回到regex了
    kxxoling
        6
    kxxoling  
       2015-05-12 18:03:44 +08:00
    @latyas pyquery 挺有意思的,不过还是得思考,lxml + Chrome 自动获取 xpath 更方便!
    octopus_new
        7
    octopus_new  
       2015-05-12 18:07:56 +08:00
    用lxml,没用过bs系列....
    mingyun
        8
    mingyun  
       2015-05-12 22:34:56 +08:00
    urllib2呢
    beibeijia
        9
    beibeijia  
       2015-05-12 23:03:45 +08:00
    刚开始听说bs好就用bs 后来用了xpath后就几乎不太用bs了 随后知道lxml性能也比bs强后就彻底不用bs了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2483 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:19 · PVG 20:19 · LAX 05:19 · JFK 08:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.