• 爬虫-beautifulsoup的使用(5)


    资料准备:

    from bs4 import BeautifulSoup
    
    html = """
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>bobby基本信息</title>
        <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    </head>
    <body>
        <div id="info">
            <p style="color: blue">讲师信息</p>
            <div class="teacher_info info">
                python全栈工程师,7年工作经验,喜欢钻研python技术,对爬虫、
                web开发以及机器学习有浓厚的兴趣,关注前沿技术以及发展趋势。
                <p class="age">年龄: 29</p>
                <p class="name bobbyname" data-bind="bobby bobby2">姓名: bobby</p>
                <p class="work_years">工作年限: 7年</p>
                <p class="position">职位: python开发工程师</p>
            </div>
            <p style="color: aquamarine">课程信息</p>
            <table class="courses">
              <tr>
                <th>课程名</th>
                <th>讲师</th>
                <th>地址</th>
              </tr>
              <tr>
                <td>django打造在线教育</td>
                <td>bobby</td>
                <td><a href="https://coding.imooc.com/class/78.html">访问</a></td>
              </tr>
              <tr>
                <td>python高级编程</td>
                <td>bobby</td>
                <td><a href="https://coding.imooc.com/class/200.html">访问</a></td>
              </tr>
              <tr>
                <td>scrapy分布式爬虫</td>
                <td>bobby</td>
                <td><a href="https://coding.imooc.com/class/92.html">访问</a></td>
              </tr>
              <tr>
                <td>django rest framework打造生鲜电商</td>
                <td>bobby</td>
                <td><a href="https://coding.imooc.com/class/131.html">访问</a></td>
              </tr>
              <tr>
                <td>tornado从入门到精通</td>
                <td>bobby</td>
                <td><a href="https://coding.imooc.com/class/290.html">访问</a></td>
              </tr>
            </table>
        </div>
    
    </body>
    </html>
    
    """

    数据处理

    获取标签
    # bs = BeautifulSoup(html, "html.parser")
    # title_tag = bs.title
    # print(title_tag.string)
    # 以字符串的形式输出
    # div_tag = bs.div
    # 仅仅获取到第一个div
    # print(div_tag.string)

    #find的查找实现方式,查找单个, # div_tag = bs.find("div") # div_tag = bs.find(id="info-955") # div_tag = bs.find("div", id="info-955") #如果匹配更多可以使用findall # div_tags = bs.find_all("div") # for tag in div_tags: # print(tag.string)

    import re #使用正则表达式的实现 # div_tag = bs.find("div", id=re.compile("info-d+")) # 也可以使用内容匹配 # div_tag = bs.find(string="bobby") #

    根据html的dom形式获取父类,子类的数据的处理方式

    # 
    # 获取子元素的子元素
    # childrens = div_tag.descendants
    # for child in childrens:
    #     if child.name:
    #         print(child.name)
    # 
    # 父元素的所有父元素
    # parents = bs.find("p",{"class":"name"}).parents
    # for parent in parents:
    #     print(parent.name)
    
    #获取兄弟节点
    # previous_sibling = bs.find("p",{"class":"name"}).previous_sibling
    # print(previous_sibling.string)
    # for sibling in previous_siblings:
    #     print(sibling.string)

    获取属性的方式:

    # 获取属性的方式
    # name_tag = bs.find("p",{"class":"name"})
    # class支持多值属性,返回的是一个列表
    # print(name_tag["class"])
    # print(name_tag.get("class"))
    # 自定义的属性,返回的值是字符串
    # print(name_tag["data-bind"])

    缺点:学习成本高,一旦换库将意味着重新学习

    优点:安装简单,学习难度较低

  • 相关阅读:
    jquery css 主菜单样式的跳转
    JS学习笔记-事件绑定
    CAS—认证原理
    uva 1203
    简化delegate写法
    使用apache htpasswd生成加密的password文件,并使用.htaccess控制文件夹訪问
    菜鸟学Java(二十)——你知道long和Long有什么差别吗?
    【spring cloud】【spring boot】网管服务-->配置文件添加endpoints.enabled = false,SpringBoot应用监控Actuator使用的安全隐患
    【Devops】【docker】【CI/CD】Jenkins源码管理,设置gitlab上项目的clone地址 + jenkins构建报错:Please make sure you have the correct access rights and the repository exists.
    【Devops】【docker】【CI/CD】jenkins源码管理,添加SSH地址后报错+Jenkins构建报错:Please make sure you have the correct access rights and the repository exists.
  • 原文地址:https://www.cnblogs.com/topass123/p/13329677.html
Copyright © 2020-2023  润新知