十年老IT知识分享 – 72 – 使用Beautiful Soup 的CSS选择器获取节点信息

如何使用Beautiful Soup 的CSS选择器获取节点信息

from bs4 import BeautifulSoup

html = '''
<html>
<head>
    <meta charset="UTF-8">
    <title>Beautiful Soup</title>
</head>
<body>
<div>
    <ul>
        <li class="item" value1="1234", value2="hello world"><a href="https://www.xxx.com">ruochen</a></li>
        <li class="item2"><a href="https://www.xxx.com">若尘</a></li>
    </ul>
    <button id="button1">确定</button>
    <ul>
        <li class="item"><a href="https://www.taobao.com">淘宝</a></li>
        <li id="myli" class="item4"><a href="https://www.microsoft">微软</a></li>
        <li class="item5"><a href="https://www.google.com">谷歌</a></li>
    </ul>
</div>
<body>
'''

soup = BeautifulSoup(html, 'lxml')
tags = soup.select('.item')
print(tags)
print(type(tags))
for tag in tags:
    print(tag)

[<li class="item" value1="1234" value2="hello world"><a href="https://www.xxx.com">ruochen</a></li>, <li class="item"><a href="https://www.taobao.com">淘宝</a></li>]

<class 'list'>

<li class="item" value1="1234" value2="hello world"><a href="https://www.xxx.com">ruochen</a></li>

<li class="item"><a href="https://www.taobao.com">淘宝</a></li>

tags= soup.select('#myli')
print(tags)

[<li class="item4" id="myli"><a href="https://www.microsoft">微软</a></li>]

tags = soup.select('a')
for tag in tags:
    print(tag)
    print(tag['href'])

<a href="https://www.xxx.com">ruochen</a>

https://www.xxx.com

<a href="https://www.xxx.com">若尘</a>

https://www.xxx.com

<a href="https://www.taobao.com">淘宝</a>

https://www.taobao.com

<a href="https://www.microsoft">微软</a>

https://www.microsoft

<a href="https://www.google.com">谷歌</a>

https://www.google.com

正文完