zoukankan      html  css  js  c++  java
  • css选择器

    css选择器

    """
    # css选择器
    #######
    #1 css选择器
    #######
    # 重点
    
    # Tag对象.select("css选择器")
    #  #ID号
    #  .类名
    #   div>p:儿子 和div p:子子孙孙
    #   找div下最后一个a标签 div a:last-child
    
    
    # css选择器,xpath选择器会用了,它就是个通行证(所有的都可以不会,会粘贴就行)
    
    # bs4:自己的选择器,css选择器
    # lxml:css选择器,xpath选择器
    # selenium:自己的选择器,css选择器,xpath选择器
    # scrapy框架:自己的选择器,css选择器,xpath选择器
    # #select('.article')
    
    
    
    #该模块提供了select方法来支持css,详见官网:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id37
    html_doc = """
    <html><head><title>The Dormouse's story</title></head>
    <body>
    <p class="title">
        <b>The Dormouse's story</b>
        Once upon a time there were three little sisters; and their names were
        <a href="http://example.com/elsie" class="sister" id="link1">
            <span>Elsie</span>
        </a>
        <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
        <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
        <div class='panel-1'>
            <ul class='list' id='list-1'>
                <li class='element'>Foo</li>
                <li class='element'>Bar</li>
                <li class='element'>Jay</li>
            </ul>
            <ul class='list list-small' id='list-2'>
                <li class='element'><h1 class='yyyy'>Foo</h1></li>
                <li class='element xxx'>Bar</li>
                <li class='element'>Jay</li>
            </ul>
        </div>
        and they lived at the bottom of a well.
    </p>
    <p class="story">...</p>
    """
    from bs4 import BeautifulSoup
    soup=BeautifulSoup(html_doc,'lxml')
    
    
    #1、CSS选择器(前端学的css选择)
    print(soup.p.select('.sister'))
    print(soup.select('.sister span'))
    
    print(soup.select('#link1'))
    print(soup.select('#link1 span'))
    
    print(soup.select('#list-2 .element.xxx'))
    
    print(soup.select('#list-2')[0].select('.element')) #可以一直select,但其实没必要,一条select就可以了
    
    # 2、获取属性
    print(soup.select('#list-2 h1')[0].attrs)
    
    # 3、获取内容
    print(soup.select('#list-2 h1')[0].get_text())
    """
    
  • 相关阅读:
    Linux常用命令大全
    C# 多线程、控制线程数提高循环输出效率
    【Redis笔记(四)】 Redis数据结构
    Redis 详解 (一) StackExchange.Redis Client
    C#中的线程(一)入门
    C#多线程
    StackExchange.Redis帮助类解决方案RedisRepository封装(散列Hash类型数据操作)
    【转】C#中使用Redis学习二 在.NET4.5中使用redis hash操作
    使用MongoDB.NET 2.2.4驱动版本对 Mongodb3.3数据库中GridFS增删改查
    .Net-Mongodb学习大全网址
  • 原文地址:https://www.cnblogs.com/yafeng666/p/12669370.html
Copyright © 2011-2022 走看看