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

2019-05-21  本文已影响0人  猛犸象和剑齿虎
8586231_192932724000_2.jpg

css选择器简介

方法: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模块总结:

上一篇下一篇

猜你喜欢

热点阅读