最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • Python爬虫如何搜索文档树

    正文概述    2020-07-22   239

    Python爬虫如何搜索文档树

    搜索文档树

    1.find_all(name, attrs, recursive, text, **kwargs)

    1)name参数

    name参数可以查找所有名字为name的Tag,字符串对象会被自动忽略掉。

    a.传字符串

    最简单的过滤器就是字符串,在搜索方法中传入一个字符串参数,Beautiful Soup会查找与字符串完整匹配所有的内容,返回一个列表。

    #!/usr/bin/python3
    # -*- coding:utf-8 -*-
     
    from bs4 import BeautifulSoup
     
    html = """
    <html><head><title>The Dormouse's story</title></head>
    <body>
    <p class="title" name="dromouse"><b>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>
    """
     
    # 创建 Beautiful Soup 对象,指定lxml解析器
    soup = BeautifulSoup(html, "lxml")
     
    print(soup.find_all("b")) 
    print(soup.find_all("a"))

    运行结果

    [<b>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>]

    相关推荐:《Python视频教程》

    B.传正则表达式

    如果传入正则表达式作为参数,Beautiful Soup会通过正则表达式match()来匹配内容。

    #!/usr/bin/python3
    # -*- coding:utf-8 -*-
     
    from bs4 import BeautifulSoup
    import re
     
    html = """
    <html><head><title>The Dormouse's story</title></head>
    <body>
    <p class="title" name="dromouse"><b>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>
    """
     
    # 创建 Beautiful Soup 对象,指定lxml解析器
    soup = BeautifulSoup(html, "lxml")
     
    for tag in soup.find_all(re.compile("^b")):
        print(tag.name)

    运行结果

    body
    b

    C.传列表

    如果传入列表参数,Beautiful Soup会将与列表中任一元素匹配的内容以列表方式返回。

    #!/usr/bin/python3
    # -*- coding:utf-8 -*-
     
    from bs4 import BeautifulSoup
     
    html = """
    <html><head><title>The Dormouse's story</title></head>
    <body>
    <p class="title" name="dromouse"><b>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>
    """
     
    # 创建 Beautiful Soup 对象,指定lxml解析器
    soup = BeautifulSoup(html, "lxml")
     
    print(soup.find_all(['a', 'b']))

    2)keyword参数

    #!/usr/bin/python3
    # -*- coding:utf-8 -*-
     
    from bs4 import BeautifulSoup
     
    html = """
    <html><head><title>The Dormouse's story</title></head>
    <body>
    <p class="title" name="dromouse"><b>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>
    """
     
    # 创建 Beautiful Soup 对象,指定lxml解析器
    soup = BeautifulSoup(html, "lxml")
     
    print(soup.find_all(id="link1"))

    运行结果

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

    3)text参数

    通过text参数可以搜索文档中的字符串内容,与name参数的可选值一样,text参数接受字符串,正则表达式,列表。

    #!/usr/bin/python3
    # -*- coding:utf-8 -*-
     
    from bs4 import BeautifulSoup
    import re
     
    html = """
    <html><head><title>The Dormouse's story</title></head>
    <body>
    <p class="title" name="dromouse"><b>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>
    """
     
    # 创建 Beautiful Soup 对象,指定lxml解析器
    soup = BeautifulSoup(html, "lxml")
     
    # 字符串
    print(soup.find_all(text = " Elsie "))
     
    # 列表
    print(soup.find_all(text = ["Tillie", " Elsie ", "Lacie"]))
     
    # 正则表达式
    print(soup.find_all(text = re.compile("Dormouse")))

    运行结果

    [' Elsie ']
    [' Elsie ', 'Lacie', 'Tillie']
    ["The Dormouse's story", "The Dormouse's story"]

    起源地下载网 » Python爬虫如何搜索文档树

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元