zoukankan      html  css  js  c++  java
  • bs4解析库

    beautifulsoup4

    bs4解析库是灵活又方便的网页解析库,处理高效,支持多种解析器。利用它不用编写正则表达式即可方便地实现网页的提取

    要解析的html标签

    from bs4 import BeautifulSoup
    
    # 要解析的html标签
    html_str = """
    <li data_group="server" class="content"> 
        <a href="/commands.html" class="index" name="a1">第一个a标签
        <a href="/commands.html" class="index2" name="a2">第二个a标签
        <a href="/commands/flushdb.html">
            <span class="first">
                这是第一个span标签
                <span class="second">
                这是第二个span标签,第一个下的子span标签
                </span>
            </span>
            <span class="third">这是第三个span标签</span>
            <h3>这是一个h3</h3>
        </a>
    </li>
    """

     

    1. 找标签:

    # 1. find_all 找到所有的li标签 结果为一个结果集
    li_find_all = BeautifulSoup(html_str, "lxml").find_all("li")
    print(type(li_find_all))  # <class 'bs4.element.ResultSet'>
    # 2. find  找到第一个li标签 结果为一个标签对象
    li_find = BeautifulSoup(html_str, "lxml").find("li")
    print(type(li_find))    # <class 'bs4.element.Tag'>
    # 添加限制条件 class id
    li = BeautifulSoup(html_str, "lxml").find_all("li", class_="content", data_group="server")
    li1 = BeautifulSoup(html_str, "lxml").find_all("li", attrs={"class":"content", "data_group":"server"})

     

    2. 找标签属性和name:

    # 找到a标签的属性和name
    a = BeautifulSoup(html_str, "lxml").find("a")
    print(a.get("href"), a.name, type(a.get("href")))    # /commands.html  a <class 'str'>
    print(a.attrs, type(a.attrs), a.text, a.string,a.get_text(), type(a.string))
    # {'href': '/commands.html', 'class': ['index'], 'name': 'a1'} <class 'dict'> 第一个a标签  <class 'bs4.element.NavigableString'>

     

    3. 处理子标签和后代标签:

    # 找到li下的后代标签
    li_find = BeautifulSoup(html_str, "lxml").find("li")
    print(li_find.children)    # <list_iterator object at 0x00000132C0915320>
    """
    for i in li_find.children:
        print(type(i),i)
    """
    # 找到li下的子标签 返回第一个找到的标签
    print(li_find.a, type(li_find.a))
    # <a class="index" href="/commands.html" name="a1">第一个a标签</a> <class 'bs4.element.Tag'>

     

    4. 处理兄弟标签:

    # 处理a标签的兄弟
    a = BeautifulSoup(html_str, "lxml").find("a", class_="index2")
    print(a.next_siblings, type(a.next_siblings))  # <generator object next_siblings at 0x000001B14AA712B0> <class 'generator'>
    """
    for i in a.next_siblings:
        print(i, type(i), "
    ")
    1. <a class="index" href="/commands.html" name="a1">第一个a标签
        </a> <class 'bs4.element.Tag'> 
    2. <a href="/commands/flushdb.html">
    <span class="first">
                这是第一个span标签
                <span class="second">
                这是第二个span标签,第一个下的子span标签
                </span>
    </span>
    <span class="third">这是第三个span标签</span>
    <h3>这是一个h3</h3>
    </a> <class 'bs4.element.Tag'>
    """
    # print("next--", a.last ,type(a.next))
    # 一组兄弟标签中的下一个标签next_sibling()  下的所有标签next_siblings()
    # 一组兄弟标签中的上一个标签previous_sibling() 上的所有标签previous_siblings()
    # 找到一组兄弟标签下的最后一个标签:
    a = [x for x in a.next_siblings][-1]
    print("aaaaaa", a, type(a))

     

    5. 处理父标签:

    # 1.parent # 返回的父标签及其子标签
    span = BeautifulSoup(html_str, "lxml").find("span", class_="second")
    print(span.parent, type(span.parent))
    # 2. parents 一层一层返回
    """
    span = BeautifulSoup(html_str, "lxml").find("span", class_="second")
    for i in span.parents:
        print(i)
    """

     

    6. 标签的其它一些处理方法

    # 1. prettify方法
    # 这个方法就是在每个标签后加入一个
     打印出来是十分规范的h5代码 一目了然
    # 也可以对某个标签做格式化处理
    a = BeautifulSoup(html_str, "lxml").find("a")
    print(a.prettify())
    
    # 2.contents方法
    li = BeautifulSoup(html_str, "lxml")
    print(li.contents, type(li.contents))
    print(li.childrent, type(li.children))
    """
    li_find.contents 返回的是一个列表 查找的标签下的子标签 包括'
    '
    li_find.children 返回的是一个迭代器, 迭代器的内容与li_find.contents一样
    """
  • 相关阅读:
    javascript线性渐变2
    javascript无缝滚动2
    javascript Object对象
    javascript无缝滚动
    javascript图片轮换2
    javascript图片轮换
    用C/C++写CGI程序
    linux shell 的 for 循环
    重磅分享:微软等数据结构+算法面试100题全部答案完整亮相
    查看linux服务器硬盘IO读写负载
  • 原文地址:https://www.cnblogs.com/KIV-Y/p/10758212.html
Copyright © 2011-2022 走看看