zoukankan      html  css  js  c++  java
  • BeautifulSoup练习

    html1="""
    <!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta charset="utf-8" />
    <title>我的第一个网页</title>
    <meta name="generator" content="EverEdit" />
    <meta name="author" content="" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    </head>
    <body>
    <div class="rows">
    <a href="http://www.baidu.com/" target="_blank">
    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-2 vfsd-div vfsd-div-color1">
    <span class="vfsd_a_title">百度</span>
    </div>
    </a>
    <a href="http://www.google.com/" target="_blank">
    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-2 vfsd-div vfsd-div-color3">
    <span class="vfsd_a_title">Google</span>
    </div>
    </a>
    <a href="http://www.oschina.net/" target="_blank">
    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-2 vfsd-div vfsd-div-color2">
    <span class="vfsd_a_title">Stack Overflow</span>
    </div>
    </a>
    </div>
    <p class="col-xs-12 col-sm-6 col-md-4 col-lg-2 vfsd-div vfsd-div-color2">你好
    <span class="vfsd_a_title">CSDN</span>
    </p>
    <p class="col-xs-12 col-sm-6 col-md-4 col-lg-2 vfsd-div vfsd-div-color2">
    <span class="vfsd_a_title">FaceBook</span>
    </p>
    <p class="nmn" id="nmn1">
    <span class="vfsd_a_title">开源中国</span>
    </p>
    </body>
    </html>
    """

    from bs4 import BeautifulSoup
    soup = BeautifulSoup(html1,'lxml')

    print(soup.title)

    ####################输出:

    <title>我的第一个网页</title>

    print(soup.title.string)

    ####################输出:

    我的第一个网页

    print(soup.head)

    ####################输出:

    <head>
    <meta charset="utf-8"/>
    <title>我的第一个网页</title>
    <meta content="EverEdit" name="generator"/>
    <meta content="" name="author"/>
    <meta content="" name="keywords"/>
    <meta content="" name="description"/>
    </head>

    for i,child in enumerate(soup.div.children):
      print(i,child)

    ####################输出:

    ['
    ', <a href="http://www.baidu.com/" target="_blank">
    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-2 vfsd-div vfsd-div-color1">
    <span class="vfsd_a_title">百度</span>
    </div>
    </a>, '
    ', <a href="http://www.google.com/" target="_blank">
    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-2 vfsd-div vfsd-div-color3">
    <span class="vfsd_a_title">Google</span>
    </div>
    </a>, '
    ', <a href="http://www.oschina.net/" target="_blank">
    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-2 vfsd-div vfsd-div-color2">
    <span class="vfsd_a_title">Stack Overflow</span>
    </div>
    </a>, '
    ']

  • 相关阅读:
    一些算法思路整理
    (递归描述)根据上排给出十个数,在其下排填出对应的十个数
    在二元树中找出和为某一值的所有路径(树)--最容易理解的版本?
    动态规划求解连续子数组最大和问题(应该是新的描述方法?)
    ubuntu/linux 下 git 通过代理下载数据 (最简单的方式)
    3. Longest Substring Without Repeating Characters(c++) 15ms
    1.Two Sum(c++)(附6ms O(n) accepted 思路和代码)
    3篇NeuroImage文献分析
    PCA、ZCA白化
    mysql创建全文索引
  • 原文地址:https://www.cnblogs.com/herd/p/9570983.html
Copyright © 2011-2022 走看看