zoukankan      html  css  js  c++  java
  • BeautifulSoup-find,findAll

    BeautifulSoup的主要函数使用

    from bs4 import BeautifulSoup
    html = """
    <html><head><title>The Dormouse's story</title></head>
    <body>
    <p class="title" id="hehe"><b>The Dormouse's story</b></p>
    <p class="story" id="firstpara">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,'html.parser')
    print soup.prettify()

    这里是一个读取html标签 然后通过prettify()函数输出标签的过程。这里输出soup对象的html标签有多种方法:
    1 soup.prettify()
    2 soup.html
    3 soup.contents
    4 soup
    另外使用soup+标签名称可以获取html标签中第一个匹配的标签内容,举例:
    print soup.p输出结果为:<p class="title"><b>The Dormouse's story</b></p>
    print soup.p.string 输出标签的内容 结果为:The Dormouse's story
    另外输出标签内容还可以使用get_text()函数:

    pid = soup.find(href=re.compile("^http:")) #使用re正则匹配 后面有讲
    p1=soup.p.get_text()
    The Dormouse's story

    通过get函数获得标签的属性:

    soup=BeautifulSoup(html,'html.parser')
    pid = soup.findAll('a',{'class':'sister'})
    for i in pid:
    print i.get('href') #对每项使用get函数取得tag属性值
    http://example.com/elsie
    http://example.com/lacie
    http://example.com/tillie

    对其他的标签也是同样可用的,并且输出的结果为文档中第一个匹配的对象,如果要搜索其他的标签需要使用find findAll函数。
    BeautifulSoup提供了强大的搜索函数find 和findall,这里的两个方法(findAll和 find)仅对Tag对象以及,顶层剖析对象有效。

    findAll(name, attrs, recursive, text, limit, **kwargs)

    for link in soup.find_all('a'): #soup.find_all返回的为列表
    print(link.get('href'))
    # http://example.com/elsie
    # http://example.com/lacie
    # http://example.com/tillie

    findAll也可以使用标签的属性搜索标签,寻找 id=”secondpara”的 p 标记,返回一个结果集:

    > pid=soup.findAll('p',id='hehe') #通过tag的id属性搜索标签
    > print pid
    [<p class="title" id="hehe"><b>The Dormouse's story</b></p>]
    >pid = soup.findAll('p',{'id':'hehe'}) #通过字典的形式搜索标签内容,返回的为一个列表[]
    >print pid
    [<p class="title" id="hehe"><b>The Dormouse's story</b></p>]

    利用正则表达式搜索tag标签内容:

    >pid=soup.findAll(id=re.compile("he$")) #正则表达式的使用
    >print pid
    [<p class="title" id="hehe"><b>The Dormouse's story</b></p>]

    利用标签的多个属性值进行搜索:

    pp=soup.findAll('a',attrs={'href':re.compile('^http'),'id':'link1'}) #标签多个属性值进行搜索 这里的attrs不可省略,便签'a'是可以省略的 相当于一个限定标签符
    print pp
    #[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>] #输出结果为list

    对搜索结果的个数进行限制: limit=n

    pid = soup.findAll('a',limit=2) #限制搜索前两个匹配的结果
    #[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
    <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]

    利用find_all搜索返回一个列表:

    soup.find_all(["a", "b"])
    # [<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>]

    这里的find_all函数参数中设置了一个列表的形式,包含了a和b两个标签,使结果以列表的形式返回。

    读取和修改属性:

    > p1 = soup.p
    > p1 #输出p1内容
    <p id="firstpara" align="center">This is paragraph<b>one</b>.</p>
    > p1['id'] #输出p1的id属性
    hehe
    >p1['id']='haha' #修改p1的id属性值
    >print p1['id']
    haha

    BeautifulSoup中的find和findAll用法相同,不同之处为find返回的是findAll搜索值的第一个值。举例:

    >soup=BeautifulSoup(html,'html.parser')
    >pid = soup.find(href=re.compile("^http:")) #这里也是使用re正则匹配
    >print pid
    <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>

  • 相关阅读:
    动态规划最大利润的问题
    【转】mysql基础汇总
    mac使用frida
    Mac 下python3 [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed 解决方法
    mac使用jadx逆向app
    python桶排序代码
    requests_html使用asyncio
    async for的使用
    [转载]微信企业号:企业客户的移动应用入口
    微信服务号、订阅号、企业号差别
  • 原文地址:https://www.cnblogs.com/dltts/p/6033863.html
Copyright © 2011-2022 走看看