2020国产成人精品视频,性做久久久久久久久,亚洲国产成人久久综合一区,亚洲影院天堂中文av色

分享

Python之Html解析方法

 看見就非常 2020-05-08

一、強(qiáng)大的BeautifulSoupBeautifulSoup是一個(gè)可以從htmlxml文件中提取數(shù)據(jù)的Python庫(kù)。它能夠通過你喜歡的轉(zhuǎn)換器實(shí)現(xiàn)慣用的文檔導(dǎo)航、查找、修改文檔的方式。在Python開發(fā)中,主要用的是BeautifulSoup的查找提取功能,修改功能很少使用

1、安裝BeautifulSoup

pip3 install beautifulsoup4

2、安裝第三方html解析器lxml

pip3 install lxml

3、安裝純Python實(shí)現(xiàn)的html5lib解析器

pip3 install html5lib


二、BeautifulSoup的使用:

1、導(dǎo)入bs4庫(kù)

from bs4 import BeautifulSoup #導(dǎo)入bs4庫(kù)

2、創(chuàng)建包含html代碼的字符串

html_str = """

<html><head><title>The Dormouse's story</title></head>

<body>

<p class="title"><b>The Dormouse's stopy</b></p>

<p class="story">Once upon a time there were three little sisters;and their names where

<a href="http:///elsie" class="sister" id="link1"><!--Elsie--></a>

"""

3、創(chuàng)建BeautifulSoup對(duì)象

1)直接通過字符串方式創(chuàng)建

soup = BeautifulSoup(html_str,'lxml') #html.parser是解析器,也可是lxml

print(soup.prettify()) ------>輸出soup對(duì)象的內(nèi)容


2)通過已有的文件來創(chuàng)建

soup = BeautifulSoup(open('/home/index.html'),features='html.parser')#html.parser是解析器,也可是lxml

4、BeautifulSoup對(duì)象的種類:BeautifulSoup將復(fù)雜HTML文檔轉(zhuǎn)換成一個(gè)復(fù)雜的樹形結(jié)構(gòu),每個(gè)節(jié)點(diǎn)都是Python對(duì)象

1BeautifulSoup表示的是一個(gè)文檔的全部?jī)?nèi)容。大部分時(shí)候,可以把它當(dāng)作Tag對(duì)象,是一個(gè)特殊的Tag,因?yàn)?/span>BeautifulSoup對(duì)象并不是真正的HTMLXML,所以沒有nameattribute屬性

2Tag:與XMLHTML原生文檔中的Tag相同,通俗講就是標(biāo)記

如:

抽取titleprintsoup.title

抽取a printsoup.a

抽取pprintsoup.p

Tag中有兩個(gè)重要的屬性:nameattributes。每個(gè)Tag都有自己的名字,通過.name來獲取

printsoup.title.name

操作Tag屬性的方法和操作字典相同

如:<p class=’p1’>Hello World</p>

printsoup.p[‘class’]

也可以直接“點(diǎn)”取屬性,如 .attrs 獲取Tag中所有屬性

printsoup.p.attrs

3NavigableString:獲取標(biāo)記內(nèi)部的文字.string

BeautifulSoupNavigableString類來封裝Tag中的字符串,一個(gè) NavigableString字符串與Python中的Unicode字符串相同,通過unicode()方法可以直接將 NavigableString對(duì)象轉(zhuǎn)換成Unicode字符串

如:u_string = unicode(soup.p.string)

4Comment:對(duì)于一些特殊對(duì)象,如果不清楚這個(gè)標(biāo)記.string的情況下,可能造成數(shù)據(jù)提取混亂。因此在提取字符串時(shí),可以判斷下類型:

if type(soup.a.string) == bs4.element.Comment:

print(soup.a.string)

5、遍歷文檔

1子節(jié)點(diǎn):

A、對(duì)于直接子節(jié)點(diǎn)可以通過 .contents .children來訪問

.contents ---->Tag子節(jié)點(diǎn)以列表的方式輸出

printsoup.head.contents

.children ----->返回一個(gè)生成器,對(duì)Tag子節(jié)點(diǎn)進(jìn)行循環(huán)

for child in soup.head.children:

printchild

B、獲取子節(jié)點(diǎn)的內(nèi)容

.string ---> 如果標(biāo)記里沒有標(biāo)記了,則返回內(nèi)容;如果標(biāo)記里只有一個(gè)唯一的標(biāo)記,則返回最里面的內(nèi)容;如果包含多個(gè)子節(jié)點(diǎn),Tag無法確定.string方法應(yīng)該返回哪個(gè)時(shí),則返回None

.strings ---->主要應(yīng)用于Tag中包含多個(gè)字符串的情況,可以進(jìn)行循環(huán)遍歷

for str in soup.strings:

printrepr(str)


.stripped_string ----->可以去掉字符串中包含的空格或空行

for str in soup.stripped_strings:

print(repr(str))

2父節(jié)點(diǎn)

A、通過.parent屬性來獲取某個(gè)元素的父節(jié)點(diǎn),如:

printsoup.title.parent

B、通過.parents屬性可以遞歸得到元素的所有父輩節(jié)點(diǎn)

for parent in soup.a.parents:

if parent is None:

print(parent)

else:

print(parent.name)

3兄弟節(jié)點(diǎn)

. next_sibling ----->獲取該節(jié)點(diǎn)的下一個(gè)兄弟節(jié)點(diǎn)

. previous_sibling ----->獲取該節(jié)點(diǎn)的上一個(gè)兄弟節(jié)點(diǎn)

4前后節(jié)點(diǎn)

. next_elements ----->獲得該節(jié)點(diǎn)前面的所有節(jié)點(diǎn)

. previous_elements ----->獲得該節(jié)點(diǎn)后面的所有節(jié)點(diǎn)

 

6、搜索文檔樹

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

A、name參數(shù):查找名字為name的標(biāo)記

printsoup.find_all(‘‘’’b)

Btext參數(shù):查找文檔中字符串的內(nèi)容

C、 recursive參數(shù):檢索當(dāng)前Tag的所有子孫節(jié)點(diǎn)時(shí),若只想找直接子節(jié)點(diǎn), 該參數(shù)設(shè)置為False


7CSS選擇器:使用soup.select()函數(shù)

1通過標(biāo)記名查找

printsoup.select("title")

2通過Tagclass屬性值查找

printsoup.select(".sister")

3通過Tagid屬性值查找

printsoup.select("#sister")

4通過是否存在某個(gè)屬性查找

printsoup.select("a[href]")

5通過屬性值查找

printsoup.select('a[href="http://"]')

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多