美文网首页
爬虫基础系列BeautifulSoup——css选择器(4)

爬虫基础系列BeautifulSoup——css选择器(4)

作者: 猛犸象和剑齿虎 | 来源:发表于2019-05-21 06:05 被阅读0次
    8586231_192932724000_2.jpg

    css选择器简介

    • CSS选择器类型:标签选择器、类选择器、id选择器

    方法:select()

    from bs4 import BeautifulSoup
    html = """
    <html><head><title>The Dormouse's story</title><title>The Dormouse's story2</title></head>
    <body>
    <p class="title" name="dromouse"><b  class="sister" >The Dormouse's story</b></p>
    <p class="story">Once upon a time there were three little sisters; and their names were
    <a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></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>;
    and they lived at the bottom of a well.</p>
    <p class="story">...</p>
    """
    

    #通过标签名查找
    soup=BeautifulSoup(html,'lxml')
    data1=soup.select('a')
    print(type(data1))
    

    结果:注意是一个列表,而不是bs4结果集。

    <class 'list'>
    

    #通过类名查找
    data2=soup.select('.sister')
    print(data2)
    

    结果:将整个class="sister"的标签拿出来,放入列表中。

    [<b class="sister">The Dormouse's story</b>, <a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
    

    #通过id查找
    data3=soup.select('#link2')
    print(data3)
    

    结果:

    [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
    

    #组合查找
    data4=soup.select('p #link1')
    print(data4)
    

    结果:

    [<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
    

    #通过其他属性查找
    data5=soup.select('a[href="http://example.com/tillie"]')
    print(data5)
    

    结果:

    [<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
    

    BeautifulSoup模块总结:

    • 相比于xpath,beautifulsoup的方法十分的复杂和琐碎,除了基础性的知识点,还有分类的搜索文档树find_all,以及css选择器select,所以记忆起来还是比较困难的。
    • 我们在实际过程中不必刻意记忆,毕竟不用考试,而是有个大致的印象,当用到时,记得从哪里搜素就行。

    相关文章

      网友评论

          本文标题:爬虫基础系列BeautifulSoup——css选择器(4)

          本文链接:https://www.haomeiwen.com/subject/wtisaqtx.html