zoukankan      html  css  js  c++  java
  • 【Python3 爬虫】U12_BeautifulSoup4之select和CCS选择器提取元素

    1.常用CSS选择器介绍

    以下是一个包含常用类选择器的案例,在案例后有具体的选择器使用介绍

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style type="text/css">
            /*标签选择器*/
            p{
                background-color: pink;
            }
            /*类选择器*/
            .h1Line{
                background-color: greenyellow;
            }
            /*id选择器*/
            #h2{
                background-color: rosybrown;
            }
            /*定位直接子元素*/
            #info > li{
                background-color: orangered;
            }
    
            /*根据属性定位*/
            input[name="username"]{
                background-color: dodgerblue;
            }
        </style>
    </head>
    <body>
        <div>
            <p>我是第1行</p>
            <p>我是第2行</p>
            <p>我是第3行</p>
            <p>我是第4行</p>
        </div>
        <div>
            <h1 class="h1Line">我是标题1</h1>
        </div>
        <div>
            <h2 id="h2">我是标题2</h2>
            <h2 id="h2s">我是标题2的参照例子</h2>
        </div>
        <div id="info">
            <li>姓名</li>
            <li>年龄</li>
            <li>身高</li>
            <div>
                <li>班级</li>
                <li>教师</li>
                <li>人数</li>
                <em>asbas</em>
            </div>
        </div>
    
        <form>
            <input type="text" name="username"/>
            <input type="password" name="password"/>
        </form>
    </body>
    </html>
    

    1.1 标签选择器

    根据标签的名字选择。示例代码如下:

    p{
        background-color: pink;
    }
    

    1.2 类名选择器

    根据类名选择,那么需要在类名前加一个点。示例代码如下:

    .h1Line{
        background-color: greenyellow;
    }
    

    1.3 id选择器

    根据id选择,那么需要在id的前面加一个#号。示例代码如下:

    #h2{
        background-color: rosybrown;
    }
    

    1.4 查找子孙元素

    查找子孙元素,那么在子孙元素直接有一个空格。示例代码如下:

    #info  li{
        background-color: orangered;
    }
    

    1.5 查找直接子元素

    查找直接子元素,那么要在父子元素中间有一个>,示例代码如下:

    #info > li{
        background-color: orangered;
    }
    

    1.6 根据属性查找

    根据属性的名字进行查找,那么应该先写标签名字,然后在括号中写属性的值。示例代码如下:

    input[name="username"]{
        background-color: dodgerblue;
    }
    

    2.实战演练:select和css选择器提取元素

    下面使用到的"前程无忧"对应的代码来自于【Python3 爬虫】U11_BeautifulSoup4库提取数据详解

    2.1 获取所有的p标签

    from bs4 import BeautifulSoup
    html = "前程无忧"
    soup = BeautifulSoup(html,'lxml')
    ps = soup.select('p')
    for p in ps:
        print(p)
        print('=' * 40)
    

    2.2 获取第2个p标签

    from bs4 import BeautifulSoup
    html = "前程无忧"
    soup = BeautifulSoup(html,'lxml')
    p = soup.select('p')[1]
    print(p)
    

    2.3 获取所有class等于t3的span标签

    from bs4 import BeautifulSoup
    html = "前程无忧"
    soup = BeautifulSoup(html,'lxml')
    spans = soup.select(".t3") #也可以直接写spans = soup.select(".t3")
    # 也可以写为:spans = soup.select("span[class='t3']")
    for span in spans:
        print(span)
    
    

    2.4 获取class为t1的p标签下的所有a标签的href属性

    from bs4 import BeautifulSoup
    html = "前程无忧"
    soup = BeautifulSoup(html,'lxml')
    aList = soup.select("p.t1 a")
    for a in aList:
        href = a['href']
        print(href)
    
    

    2.5 获取所有的职位信息(文本)

    from bs4 import BeautifulSoup
    html = "前程无忧"
    soup = BeautifulSoup(html,'lxml')
    
    divs = soup.select('div')
    infoSet = list()
    for div in divs:
        info = {}
        infos = list(div.stripped_strings) # div.stripped_strings返回的是一个生成器
        info['job'] = infos[0]
        info['company'] = infos[1]
        info['address'] = infos[2]
        info['salary'] = infos[3]
        info['ReleaseDate'] = infos[4]
        infoSet.append(info)
    print(infoSet)
    
    
  • 相关阅读:
    并发的简单介绍1
    UITableView (4): 在TableView中移动cell和Section 从TableView中删除cell和section 添加系统默认刷新控件
    UITableView (3):显示cell上的菜单
    TableView(2)
    UITableView (1)
    在 Interface Builder 中配置自动布局的约束
    Velocity 的工作原理
    ORA-01858: a non-numeric character was found where a numeric was expected
    Java 8:不要再用循环了
    Lambda表达式
  • 原文地址:https://www.cnblogs.com/OliverQin/p/12597211.html
Copyright © 2011-2022 走看看