BeautifulSoup的用法

BeautifulSoup是一個模塊,該模塊用於接收一個HTML或XML字符串,而後將其進行格式化,以後遍能夠使用他提供的方法進行快速查找指定元素,從而使得在HTML或XML中查找指定元素變得簡單。html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from bs4 import BeautifulSoup
 
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
asdf
     <div class="title">
         <b>The Dormouse's story總共</b>
         <h1>f</h1>
     </div>
<div class="story">Once upon a time there were three little sisters; and their names were
     <a  class="sister0" id="link1">Els<span>f</span>ie</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.</div>
ad<br/>sf
<p class="story">...</p>
</body>
</html>
"""
 
soup = BeautifulSoup(html_doc, features = "lxml" )
# 找到第一個a標籤
tag1 = soup.find(name = 'a' )
# 找到全部的a標籤
tag2 = soup.find_all(name = 'a' )
# 找到id=link2的標籤
tag3 = soup.select( '#link2' )

再給你們舉個常常用的例子:python

一,這個參數須要一個字符串,response是一個對象,response.text是字符串,url

二,’lxml‘是解析器,指定用lxmL作解析器spa

三,四,這兩個合在一塊兒,是找到類選擇器爲btnlinks的p標籤code

五,找出標籤元素裏href對應的urlorm

注意:find獲得的是第一個標籤元素,find_all是獲得的是一個列表,元素是全部的符合條件的標籤,能夠經過下標獲取元素。xml

相關文章
相關標籤/搜索