欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python BeautifulSoup使用方法詳解

 更新時(shí)間:2013年11月21日 15:11:50   作者:  
Beautiful Soup 是用Python寫的一個(gè)HTML/XML的解析器,它可以很好的處理不規(guī)范標(biāo)記并生成剖析樹(shù)(parse tree)。 它提供簡(jiǎn)單又常用的導(dǎo)航(navigating),搜索以及修改剖析樹(shù)的操作。它可以大大節(jié)省你的編程時(shí)間,下面我們就看看他是如何使用

直接看例子:

復(fù)制代碼 代碼如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>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://www.dbjr.com.cn" class="sister" id="link1">Elsie</a>,
<a href="http://www.dbjr.com.cn" class="sister" id="link2">Lacie</a> and
<a href="http://www.dbjr.com.cn" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
soup = BeautifulSoup(html_doc)
print soup.title
print soup.title.name
print soup.title.string
print soup.p
print soup.a
print soup.find_all('a')
print soup.find(id='link3')
print soup.get_text()

結(jié)果為:

復(fù)制代碼 代碼如下:

<title>The Dormouse's story</title>
title
The Dormouse's story
<p class="title"><b>The Dormouse's story</b></p>
<a class="sister" href="http://www.dbjr.com.cn" id="link1">Elsie</a>
[<a class="sister" href="http://www.dbjr.com.cn" id="link1">Elsie</a>, <a class="sister" href="http://www.dbjr.com.cn" id="link2">Lacie</a>, <a class="sister" href="http://www.dbjr.com.cn" id="link3">Tillie</a>]
<a class="sister" href="http://www.dbjr.com.cn" id="link3">Tillie</a>
The Dormouse's story
The Dormouse's story
Once upon a time there were three little sisters; and their names were
Elsie,
Lacie and
Tillie;
and they lived at the bottom of a well.
...

可以看出:soup 就是BeautifulSoup處理格式化后的字符串,soup.title 得到的是title標(biāo)簽,soup.p  得到的是文檔中的第一個(gè)p標(biāo)簽,要想得到所有標(biāo)簽,得用find_all
函數(shù)。find_all 函數(shù)返回的是一個(gè)序列,可以對(duì)它進(jìn)行循環(huán),依次得到想到的東西.
get_text() 是返回文本,這個(gè)對(duì)每一個(gè)BeautifulSoup處理后的對(duì)象得到的標(biāo)簽都是生效的。你可以試試 print soup.p.get_text()
其實(shí)是可以獲得標(biāo)簽的其他屬性的,比如我要獲得a標(biāo)簽的href屬性的值,可以使用 print soup.a['href'],類似的其他屬性,比如class也是可以這么得到的(soup.a['class'])。
特別的,一些特殊的標(biāo)簽,比如head標(biāo)簽,是可以通過(guò)soup.head 得到,其實(shí)前面也已經(jīng)說(shuō)了。
如何獲得標(biāo)簽的內(nèi)容數(shù)組?使用contents 屬性就可以 比如使用 print soup.head.contents,就獲得了head下的所有子孩子,以列表的形式返回結(jié)果,
可以使用 [num]  的形式獲得 ,獲得標(biāo)簽,使用.name 就可以。
獲取標(biāo)簽的孩子,也可以使用children,但是不能print soup.head.children 沒(méi)有返回列表,返回的是 <listiterator object at 0x108e6d150>,
不過(guò)使用list可以將其轉(zhuǎn)化為列表。當(dāng)然可以使用for 語(yǔ)句遍歷里面的孩子。
關(guān)于string屬性,如果超過(guò)一個(gè)標(biāo)簽的話,那么就會(huì)返回None,否則就返回具體的字符串print soup.title.string 就返回了 The Dormouse's story
超過(guò)一個(gè)標(biāo)簽的話,可以試用strings
向上查找可以用parent函數(shù),如果查找所有的,那么可以使用parents函數(shù)
查找下一個(gè)兄弟使用next_sibling,查找上一個(gè)兄弟節(jié)點(diǎn)使用previous_sibling,如果是查找所有的,那么在對(duì)應(yīng)的函數(shù)后面加s就可以

如何遍歷樹(shù)?

使用find_all 函數(shù)

復(fù)制代碼 代碼如下:

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

舉例說(shuō)明:

復(fù)制代碼 代碼如下:

print soup.find_all('title')
print soup.find_all('p','title')
print soup.find_all('a')
print soup.find_all(id="link2")
print soup.find_all(id=True)

返回值為:

復(fù)制代碼 代碼如下:

[<title>The Dormouse's story</title>]
[<p class="title"><b>The Dormouse's story</b></p>]
[<a class="sister" href="http://www.dbjr.com.cn" id="link1">Elsie</a>, <a class="sister" href="http://www.dbjr.com.cn" id="link2">Lacie</a>, <a class="sister" href="http://www.dbjr.com.cn" id="link3">Tillie</a>]
[<a class="sister" href="http://www.dbjr.com.cn" id="link2">Lacie</a>]
[<a class="sister" href="http://www.dbjr.com.cn" id="link1">Elsie</a>, <a class="sister" href="http://www.dbjr.com.cn" id="link2">Lacie</a>, <a class="sister" href="http://www.dbjr.com.cn" id="link3">Tillie</a>]

通過(guò)css查找,直接上例子:

復(fù)制代碼 代碼如下:

print soup.find_all("a", class_="sister")
print soup.select("p.title")

通過(guò)屬性進(jìn)行查找

復(fù)制代碼 代碼如下:

print soup.find_all("a", attrs={"class": "sister"})

通過(guò)文本進(jìn)行查找

復(fù)制代碼 代碼如下:

print soup.find_all(text="Elsie")
print soup.find_all(text=["Tillie", "Elsie", "Lacie"])

限制結(jié)果個(gè)數(shù)

復(fù)制代碼 代碼如下:

print soup.find_all("a", limit=2)

結(jié)果為:

復(fù)制代碼 代碼如下:

[<a class="sister" href="http://www.dbjr.com.cn" id="link1">Elsie</a>, <a class="sister" href="http://www.dbjr.com.cn" id="link2">Lacie</a>, <a class="sister" href="http://www.dbjr.com.cn" id="link3">Tillie</a>]
[<p class="title"><b>The Dormouse's story</b></p>]
[<a class="sister" href="http://www.dbjr.com.cn" id="link1">Elsie</a>, <a class="sister" href="http://www.dbjr.com.cn" id="link2">Lacie</a>, <a class="sister" href="http://www.dbjr.com.cn" id="link3">Tillie</a>]
[u'Elsie']
[u'Elsie', u'Lacie', u'Tillie']
[<a class="sister" href="http://www.dbjr.com.cn" id="link1">Elsie</a>, <a class="sister" href="http://www.dbjr.com.cn" id="link2">Lacie</a>]

總之,通過(guò)這些函數(shù)可以查找到想要的東西。

相關(guān)文章

  • Python的pytest測(cè)試框架使用詳解

    Python的pytest測(cè)試框架使用詳解

    這篇文章主要介紹了Python的pytest測(cè)試框架使用詳解,說(shuō)到?pytest,大家總不免要拿來(lái)和?unittest?來(lái)比一下,但是?unittest?畢竟是標(biāo)準(zhǔn)庫(kù),兼容性方面肯定沒(méi)得說(shuō),但要論簡(jiǎn)潔和方便的話,pytest?也是不落下風(fēng)的,需要的朋友可以參考下
    2023-07-07
  • python模塊離線安裝方式

    python模塊離線安裝方式

    這篇文章主要介紹了python模塊離線安裝方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 使用Atom支持基于Jupyter的Python開(kāi)教程詳解

    使用Atom支持基于Jupyter的Python開(kāi)教程詳解

    這篇文章主要介紹了使用Atom支持基于Jupyter的Python開(kāi)發(fā),Vscode雖然說(shuō)也有連接Jupyter的工具,但是交互式的開(kāi)發(fā)Hydrogen體驗(yàn)更好,需要的朋友可以參考下
    2021-08-08
  • Python基于生成器迭代實(shí)現(xiàn)的八皇后問(wèn)題示例

    Python基于生成器迭代實(shí)現(xiàn)的八皇后問(wèn)題示例

    這篇文章主要介紹了Python基于生成器迭代實(shí)現(xiàn)的八皇后問(wèn)題,簡(jiǎn)單描述了八皇后問(wèn)題,并結(jié)合實(shí)例形式分析了Python基于生成器迭代解決八皇后問(wèn)題的相關(guān)操作技巧,需要的朋友可以參考下
    2018-05-05
  • Python3中的循環(huán)語(yǔ)句示例詳解

    Python3中的循環(huán)語(yǔ)句示例詳解

    這篇文章主要介紹了Python3?循環(huán)語(yǔ)句,本文將詳細(xì)介紹Python3中的循環(huán)語(yǔ)句,給出各種循環(huán)的使用示例,以及運(yùn)行結(jié)果的解釋,需要的朋友可以參考下
    2023-04-04
  • python網(wǎng)絡(luò)編程示例(客戶端與服務(wù)端)

    python網(wǎng)絡(luò)編程示例(客戶端與服務(wù)端)

    這篇文章主要介紹了python網(wǎng)絡(luò)編程示例,提供了客戶端與服務(wù)端,需要的朋友可以參考下
    2014-04-04
  • Django Admin設(shè)置應(yīng)用程序及模型順序方法詳解

    Django Admin設(shè)置應(yīng)用程序及模型順序方法詳解

    這篇文章主要介紹了Django Admin設(shè)置應(yīng)用程序及模型順序方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • 一篇文章帶你了解python標(biāo)準(zhǔn)庫(kù)--time模塊

    一篇文章帶你了解python標(biāo)準(zhǔn)庫(kù)--time模塊

    下面小編就為大家?guī)?lái)一篇python模塊之time模塊。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2021-08-08
  • 如何基于Python代碼實(shí)現(xiàn)高精度免費(fèi)OCR工具

    如何基于Python代碼實(shí)現(xiàn)高精度免費(fèi)OCR工具

    這篇文章主要介紹了如何基于Python代碼實(shí)現(xiàn)高精度免費(fèi)OCR工具,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • python Popen 獲取輸出,等待運(yùn)行完成示例

    python Popen 獲取輸出,等待運(yùn)行完成示例

    今天小編就為大家分享一篇python Popen 獲取輸出,等待運(yùn)行完成示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12

最新評(píng)論