推荐学习书目
› 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 · May 12, 2015 · 5002 views
    This topic created in 4199 days ago, the information mentioned may be changed or developed.

    参照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中new和init的区别
    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 replies  •  2015-05-12 23:03:45 +08:00
    latyas
    latyas
    May 12, 2015
    1
    已从bs4跳到pyquery了
    wensonsmith
    wensonsmith
    May 12, 2015
    2
    看起来很不错啊!
    tanteng
    tanteng
    OP
    May 12, 2015
    3
    @latyas 我只知道bs4。。。所以就用了,到底哪个更主流
    zztt168
    zztt168
    May 12, 2015 via Android
    4
    @latyas 请教pyquery与bs4比较有什么优势,和楼主一样用bs比较多,其他的不懂,谢谢。
    ahxxm
    ahxxm
    May 12, 2015
    5
    曾经用过一阵子bs4,发现在vps上会用满cpu,之后就回到regex了
    kxxoling
    kxxoling
    May 12, 2015
    6
    @latyas pyquery 挺有意思的,不过还是得思考,lxml + Chrome 自动获取 xpath 更方便!
    octopus_new
    octopus_new
    May 12, 2015
    7
    用lxml,没用过bs系列....
    mingyun
    mingyun
    May 12, 2015
    8
    urllib2呢
    beibeijia
    beibeijia
    May 12, 2015
    9
    刚开始听说bs好就用bs 后来用了xpath后就几乎不太用bs了 随后知道lxml性能也比bs强后就彻底不用bs了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4728 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 51ms · UTC 03:12 · PVG 11:12 · LAX 19:12 · JFK 22:12
    ♥ Do have faith in what you're doing.