zoukankan      html  css  js  c++  java
  • BeautifulSoup学习笔记

    from BeautifulSoup import BeautifulSoup
    import re
    
    doc = ['<html><head><title>Page title</title></head>',
           '<body><p id="firstpara" align="center">This is paragraph <b>one</b>.',
           '<p id="secondpara" align="blah">This is paragraph <b>two</b>.',
           '</html>']
    soup = BeautifulSoup(''.join(doc))
    print soup.prettify()
    

     运行结果为:

    print soup.contents[0].name
    #
    print soup.contents[0].contents[0].name
    
    for i in range(len(soup.contents[0])):
        print soup.contents[0].contents[i].name
    

     

    titleTag = soup.html.head.title
    titleTag
    # <title>Page title</title>
    
    titleTag.string
    # u'Page title'
    
    len(soup('p'))
    # 2
    
    soup.findAll('p', align="center")
    # [<p id="firstpara" align="center">This is paragraph <b>one</b>. </p>]
    
    soup.find('p', align="center")
    # <p id="firstpara" align="center">This is paragraph <b>one</b>. </p>
    
    soup('p', align="center")[0]['id']
    # u'firstpara'
    
    soup.find('p', align=re.compile('^b.*'))['id']
    # u'secondpara'
    
    soup.find('p').b.string
    # u'one'
    
    soup('p')[1].b.string
    # u'two'
    
  • 相关阅读:
    小程序全局生命周期( 仅供了解 )
    iview表格render小案例2
    iview中表格根据条件渲染
    如何实现页面同时在移动端和pc端的兼容问题
    小程序页面中的生命周期( 仅供了解 )
    弹性盒基本属性
    elementUI实现分页效果+模糊搜索效果
    事件流 ---- 事件冒泡与事件捕获
    React生命周期
    数据库索引数据结构btree,b-tree和b+tree树
  • 原文地址:https://www.cnblogs.com/rollenholt/p/2271298.html
Copyright © 2011-2022 走看看