亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術文章
文章詳情頁

面向新手解析python Beautiful Soup基本用法

瀏覽:11日期:2022-07-17 18:38:20

Beautiful Soup就是Python的一個HTML或XML的解析庫,可以用它來方便地從網頁中提取數據。它有如下三個特點:

Beautiful Soup提供一些簡單的、Python式的函數來處理導航、搜索、修改分析樹等功能。它是一個工具箱,通過解析文檔為用戶提供需要抓取的數據,因為簡單,所以不需要多少代碼就可以寫出一個完整的應用程序。 Beautiful Soup自動將輸入文檔轉換為Unicode編碼,輸出文檔轉換為UTF-8編碼。你不需要考慮編碼方式,除非文檔沒有指定一個編碼方式,這時你僅僅需要說明一下原始編碼方式就可以了。 Beautiful Soup已成為和lxml、html6lib一樣出色的Python解釋器,為用戶靈活地提供不同的解析策略或強勁的速度。

首先,我們要安裝它:pip install bs4,然后安裝 pip install beautifulsoup4.

Beautiful Soup支持的解析器

面向新手解析python Beautiful Soup基本用法

下面我們以lxml解析器為例:

from bs4 import BeautifulSoupsoup = BeautifulSoup(’<p>Hello</p>’, ’lxml’)print(soup.p.string)

結果:

Hello

beautiful soup美化的效果實例:

html = '''<html><head><title>The Dormouse’s story</title></head><body><p name='dromouse'><b>The Dormouse’s story</b></p><p class='story'>Once upon a time there were three little sisters; and their names were<a rel='external nofollow' rel='external nofollow' rel='external nofollow' id='link1'><!-- Elsie --></a>,<a rel='external nofollow' rel='external nofollow' rel='external nofollow' id='link2'>Lacie</a> and<a rel='external nofollow' rel='external nofollow' rel='external nofollow' id='link3'>Tillie</a>;and they lived at the bottom of a well.</p><p class='story'>...</p>'''from bs4 import BeautifulSoupsoup = BeautifulSoup(html, ’lxml’)#調用prettify()方法。這個方法可以把要解析的字符串以標準的縮進格式輸出print(soup.prettify())print(soup.title.string)

結果:

<html> <head> <title> The Dormouse’s story </title> </head> <body> <p name='dromouse'> <b> The Dormouse’s story </b> </p> <p class='story'> Once upon a time there were three little sisters; and their names were <a rel='external nofollow' rel='external nofollow' rel='external nofollow' id='link1'> <!-- Elsie --> </a> , <a rel='external nofollow' rel='external nofollow' rel='external nofollow' id='link2'> Lacie </a> and <a rel='external nofollow' rel='external nofollow' rel='external nofollow' id='link3'> Tillie </a> ;and they lived at the bottom of a well. </p> <p class='story'> ... </p> </body></html>The Dormouse’s story

下面舉例說明選擇元素、屬性、名稱的方法

html = '''<html><head><title>The Dormouse’s story</title></head><body><p name='dromouse'><b>The Dormouse’s story</b></p><p class='story'>Once upon a time there were three little sisters; and their names were<a rel='external nofollow' rel='external nofollow' rel='external nofollow' id='link1'><!-- Elsie --></a>,<a rel='external nofollow' rel='external nofollow' rel='external nofollow' id='link2'>Lacie</a> and<a rel='external nofollow' rel='external nofollow' rel='external nofollow' id='link3'>Tillie</a>;and they lived at the bottom of a well.</p><p class='story'>...</p>'''from bs4 import BeautifulSoupsoup = BeautifulSoup(html, ’lxml’)print(’輸出結果為title節點加里面的文字內容:n’,soup.title)print(’輸出它的類型:n’,type(soup.title))print(’輸出節點的文本內容:n’,soup.title.string)print(’結果是節點加其內部的所有內容:n’,soup.head)print(’結果是第一個p節點的內容:n’,soup.p)print(’利用name屬性獲取節點的名稱:n’,soup.title.name)#這里需要注意的是,有的返回結果是字符串,有的返回結果是字符串組成的列表。# 比如,name屬性的值是唯一的,返回的結果就是單個字符串。# 而對于class,一個節點元素可能有多個class,所以返回的是列表。print(’每個節點可能有多個屬性,比如id和class等:n’,soup.p.attrs)print(’選擇這個節點元素后,可以調用attrs獲取所有屬性:n’,soup.p.attrs[’name’])print(’獲取p標簽的name屬性值:n’,soup.p[’name’])print(’獲取p標簽的class屬性值:n’,soup.p[’class’])print(’獲取第一個p節點的文本:n’,soup.p.string)

結果:

輸出結果為title節點加里面的文字內容:<title>The Dormouse’s story</title>輸出它的類型:<class ’bs4.element.Tag’>輸出節點的文本內容:The Dormouse’s story結果是節點加其內部的所有內容:<head><title>The Dormouse’s story</title></head>結果是第一個p節點的內容:<p name='dromouse'><b>The Dormouse’s story</b></p>利用name屬性獲取節點的名稱:title每個節點可能有多個屬性,比如id和class等:{’class’: [’title’], ’name’: ’dromouse’}選擇這個節點元素后,可以調用attrs獲取所有屬性:dromouse獲取p標簽的name屬性值:dromouse獲取p標簽的class屬性值:[’title’]獲取第一個p節點的文本:The Dormouse’s story

在上面的例子中,我們知道每一個返回結果都是bs4.element.Tag類型,它同樣可以繼續調用節點進行下一步的選擇。

html = '''<html><head><title>The Dormouse’s story</title></head><body>'''from bs4 import BeautifulSoupsoup = BeautifulSoup(html, ’lxml’)print(’獲取了head節點元素,繼續調用head來選取其內部的head節點元素:n’,soup.head.title)print(’繼續調用輸出類型:n’,type(soup.head.title))print(’繼續調用輸出內容:n’,soup.head.title.string)

結果:

獲取了head節點元素,繼續調用head來選取其內部的head節點元素: <title>The Dormouse’s story</title>繼續調用輸出類型: <class ’bs4.element.Tag’>繼續調用輸出內容: The Dormouse’s story

(1)find_all()

find_all,顧名思義,就是查詢所有符合條件的元素。給它傳入一些屬性或文本,就可以得到符合條件的元素,它的功能十分強大。

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

他的用法:

html=’’’<div class='panel'> <div class='panel-heading'> <h4>Hello</h4> </div> <div class='panel-body'> <ul id='list-1'> <li class='element'>Foo</li> <li class='element'>Bar</li> <li class='element'>Jay</li> </ul> <ul id='list-2'> <li class='element'>Foo</li> <li class='element'>Bar</li> </ul> </div></div>’’’from bs4 import BeautifulSoupsoup = BeautifulSoup(html, ’lxml’)print(’查詢所有ul節點,返回結果是列表類型,長度為2:n’,soup.find_all(name=’ul’))print(’每個元素依然都是bs4.element.Tag類型:n’,type(soup.find_all(name=’ul’)[0]))#將以上步驟換一種方式,遍歷出來for ul in soup.find_all(name=’ul’): print(’輸出每個u1:’,ul.find_all(name=’li’))#遍歷兩層for ul in soup.find_all(name=’ul’): print(’輸出每個u1:’,ul.find_all(name=’li’)) for li in ul.find_all(name=’li’): print(’輸出每個元素:’,li.string)

結果:

查詢所有ul節點,返回結果是列表類型,長度為2: [<ul id='list-1'><li class='element'>Foo</li><li class='element'>Bar</li><li class='element'>Jay</li></ul>, <ul id='list-2'><li class='element'>Foo</li><li class='element'>Bar</li></ul>]每個元素依然都是bs4.element.Tag類型: <class ’bs4.element.Tag’>輸出每個u1: [<li class='element'>Foo</li>, <li class='element'>Bar</li>, <li class='element'>Jay</li>]輸出每個u1: [<li class='element'>Foo</li>, <li class='element'>Bar</li>]輸出每個u1: [<li class='element'>Foo</li>, <li class='element'>Bar</li>, <li class='element'>Jay</li>]輸出每個元素: Foo輸出每個元素: Bar輸出每個元素: Jay輸出每個u1: [<li class='element'>Foo</li>, <li class='element'>Bar</li>]輸出每個元素: Foo輸出每個元素: Bar

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 欧美视频一级 | 亚洲一级二级三级 | 大学生一级一片第一次免费 | 欧美精品一区二区三区免费播放 | 亚洲欧美日韩中文字幕在线一 | 尹人成人| 玖玖99视频 | 久久er国产精品免费观看1 | 国产成人免费午夜在线观看 | 国产三a级日本三级日产三级 | 国产精品自在欧美一区 | 亚洲精品高清国产一久久 | 一本久 | 一级特黄一欧美俄罗斯毛片 | 亚洲欧美日韩国产综合高清 | 久久不射网 | 亚洲羞羞裸色私人影院 | 一级做a爰性色毛片 | 亚洲午夜在线播放 | 99精品免费久久久久久久久日本 | 午夜日韩精品 | 久久精品99| 色秀视频在线观看88品善网 | 国产精品日韩欧美一区二区 | 一本色道久久88加勒比—综合 | 国产精品高清在线观看地址 | 成年网站在线 | 日本三级香港三级人妇99视 | 黄色一级片a| 免费观看情趣v视频网站 | 三级黄色片在线免费观看 | 女人张开腿让男人捅的视频 | 特级毛片www欧美 | 九九在线观看视频 | 视频在线一区 | 色拍拍噜噜噜aⅴ在线观看 色青青草原桃花久久综合 色婷婷91 | 久草在线视频网站 | 国产精品99久久久久久宅男 | 日韩精品一区二区三区不卡 | 日本道综合一本久久久88 | 亚洲一区欧美一区 |