日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

beautifulsoup4

發(fā)布時(shí)間:2025/3/20 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 beautifulsoup4 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

環(huán)境為:

  • Python3.6
  • windows
  • pycharm2017.2.4

安裝:

# 安裝beautifulsoup4   pip install beautifulsoup4# 安裝解析器   pip install lxml# 另一個(gè)可供選擇的解析器是純Python實(shí)現(xiàn)的 html5lib,html5lib的解析方式與瀏覽器相同pip install html5lib

?

?

?


基本使用

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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://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> """ #基本使用:容錯(cuò)處理,文檔的容錯(cuò)能力指的是在html代碼不完整的情況下,使用該模塊可以識(shí)別該錯(cuò)誤。<br>#使用BeautifulSoup解析上述代碼,能夠得到一個(gè) BeautifulSoup 的對(duì)象,并能按照標(biāo)準(zhǔn)的縮進(jìn)格式的結(jié)構(gòu)輸出 from?bs4?import?BeautifulSoup soup=BeautifulSoup(html_doc,'lxml')?#具有容錯(cuò)功能 res=soup.prettify()?#處理好縮進(jìn),結(jié)構(gòu)化顯示 print(res)

標(biāo)簽選擇器

1 即直接通過(guò)標(biāo)簽名字選擇,選擇速度快,如果存在多個(gè)相同的標(biāo)簽則只返回第一個(gè)<br><br>

?

from bs4 import BeautifulSouphtml_doc = """ <html><head><title>The Dormouse's story</title></head> <body><p>first tag</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<i>this i tag</i></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_doc, 'lxml')# 獲取標(biāo)簽的名稱(chēng) # print(soup.head) # <head><title>The Dormouse's story</title></head># 獲取標(biāo)簽的屬性 # print(soup.p.name) # p# 直接獲取標(biāo)簽,如果存在多個(gè)相同的標(biāo)簽則只返回第一個(gè) # print(soup.p) # <p>first tag</p># 獲取標(biāo)簽的內(nèi)容,# print(soup.p.string) # first tag # print(soup.a.string) # None # print(soup.p.text) # first tag # print(soup.a.text) # Elsiethis i tag # print(soup.a.contents) # ['Elsie', <i>this i tag</i>] """ 注意 contents獲取選中標(biāo)簽內(nèi)的所有的值,包括里面的標(biāo)簽 string 只能獲取當(dāng)前標(biāo)簽,而無(wú)法獲取子標(biāo)簽的內(nèi)容,如果存在子標(biāo)簽,則返回None text則獲取包括子標(biāo)簽在內(nèi)的所有值 """# 嵌套選擇 # print(soup.head.title.string) # The Dormouse's story # print(soup.body.a.contents) # ['Elsie', <i>this i tag</i>] # print(soup.body.a.text) # Elsiethis i tag # print(soup.body.a.string) # None # print(soup.body.p.string) # first tag# 獲取子節(jié)點(diǎn),子孫節(jié)點(diǎn) # print(soup.contents) # 返回整個(gè)HTML頁(yè)面的所有節(jié)點(diǎn) # print(soup.p.contents) # ['first tag'] # print(soup.p.children) # 得到一個(gè)迭代器,包含此標(biāo)簽內(nèi)錯(cuò)有的子節(jié)點(diǎn) # print(list(soup.a.children)) # ['Elsie', <i>this i tag</i>] # print(soup.p.descendants) # <generator object descendants at 0x00000162FFB9D570> # print(list(soup.a.descendants)) # 獲取子孫節(jié)點(diǎn),p下所有的標(biāo)簽都會(huì)選擇出來(lái) ['Elsie', <i>this i tag</i>, 'this i tag'] # for i, child in enumerate(soup.p.descendants): # print(i, child) # 0 first tag# 獲取父節(jié)點(diǎn),祖先節(jié)點(diǎn) # print(soup.a.parent) # 獲取 a 標(biāo)簽 # print(soup.a.parents) # <generator object parents at 0x0000022F8747D570> # print(list(soup.a.parents)) # a 標(biāo)簽的父,父,父節(jié)點(diǎn)都會(huì)找出來(lái),到html節(jié)點(diǎn)# 獲取兄弟節(jié)點(diǎn) # print(soup.a.next_siblings) # 生成器對(duì)象 <generator object next_siblings at 0x000002418B9BD570> # print(list(soup.a.next_siblings)) beautifulsoup4標(biāo)簽選擇器 View Code

標(biāo)準(zhǔn)選擇器

from bs4 import BeautifulSouphtml_doc = """ <html><head><title>The Dormouse's story</title></head> <body><p>first tag</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<i id="i1" class="i1">this i tag</i></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_doc, 'lxml')# 標(biāo)準(zhǔn)選擇器# 按照標(biāo)簽名查找 # print(soup.find_all('a')) # 拿到所有的標(biāo)簽 # print(soup.find_all('a', id='link2')) # [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>] # print(soup.find(id='link2')) # <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> # print(soup.find_all(attrs={"class": "sister"})) # 拿到所有的類(lèi)為sister的a標(biāo)簽 # print(soup.find_all(class_='sister')) # 拿到的結(jié)果也是所有的類(lèi)名為sister的a標(biāo)簽 # 注意:soup.find_all(class_='sister' 中的class_ 的用法,要加下劃線,因?yàn)閏lass為關(guān)鍵字,寫(xiě)在attrs里面的沒(méi)影響# 嵌套查找 # print(soup.find_all('a')[0].find('i')) # 拿到 a 標(biāo)簽的下級(jí) i 標(biāo)簽 <i>this i tag</i># 按照屬性查找 # print(soup.a.find_all(attrs={'id':'i1'})) # [<i class="i1" id="i1">this i tag</i>] # print(soup.a.find_all(attrs={"class":'i1'})) # [<i class="i1" id="i1">this i tag</i>] # print(soup.find_all(id='i1')) # [<i class="i1" id="i1">this i tag</i>]# 按照文本內(nèi)容查找,按照完全匹配來(lái)匹配內(nèi)容,不是模糊的匹配,是== 不是 in # print(soup.p.find_all(text='first tag')) # ['first tag'] beautifulsoup4標(biāo)準(zhǔn)選擇器 View Code

CSS選擇器

##該模塊提供了select方法來(lái)支持css html_doc = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title"><b>The Dormouse's story</b>Once upon a time there were three little sisters; and their names were<a href="http://example.com/elsie" class="sister" id="link1"><span>Elsie</span></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>;<div class='panel-1'><ul class='list' id='list-1'><li class='element'>Foo</li><li class='element'>Bar</li><li class='element'>Jay</li></ul><ul class='list list-small' id='list-2'><li class='element'><h1 class='yyyy'>Foo</h1></li><li class='element xxx'>Bar</li><li class='element'>Jay</li></ul></div>and they lived at the bottom of a well. </p> <p class="story">...</p> """ from bs4 import BeautifulSoup soup=BeautifulSoup(html_doc,'lxml')#1、CSS選擇器 print(soup.p.select('.sister')) print(soup.select('.sister span'))print(soup.select('#link1')) print(soup.select('#link1 span'))print(soup.select('#list-2 .element.xxx'))print(soup.select('#list-2')[0].select('.element')) #可以一直select,但其實(shí)沒(méi)必要,一條select就可以了# 2、獲取屬性 print(soup.select('#list-2 h1')[0].attrs)# 3、獲取內(nèi)容 print(soup.select('#list-2 h1')[0].get_text())CSS選擇器 View Code

beautifulsoup4中文文檔

轉(zhuǎn)載于:https://www.cnblogs.com/q240756200/p/10671952.html

總結(jié)

以上是生活随笔為你收集整理的beautifulsoup4的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 99热亚洲 | 亚洲www在线 | 国精产品一区二区三区 | 无码人妻精品一区二区三区99v | 久久成人乱码欧美精品一区二区 | 久久无毛 | 成人深夜视频 | 91亚洲视频在线 | 亚洲精品欧美日韩 | аⅴ资源新版在线天堂 | 欧美精品午夜 | 日韩一区二区三区在线观看视频 | 夜夜爽夜夜 | 亚洲av无一区二区三区怡春院 | 嫩草视频在线免费观看 | 久久久久久久国产精品美女 | 国产热 | 欧美性生活网 | 成人特级毛片 | 人妻精品久久久久中文字幕69 | 五月激情六月丁香 | 91操人视频| 日本美女在线 | 网站免费在线观看 | 天天摸日日摸狠狠添 | 欧美一级做a爰片免费视频 成人激情在线观看 | 天天摸天天操天天爽 | 黄色小视频国产 | 免费成人在线观看视频 | 成年人看的视频网站 | 福利在线一区二区 | 午夜成人影片 | 日韩一级网站 | 成人精品一区二区 | 最新黄色网址在线观看 | 俄罗斯色片 | 免费黄色网址观看 | 久久国产影院 | 日本一二区视频 | 波多野结衣一二三区 | 国产欧美日韩综合精品一区 | 女人私密又肥又大 | 泽村玲子av | 少妇精品亚洲一区二区成人 | 国产91亚洲精品 | 91丝袜一区二区三区 | 国产三级在线观看完整版 | 国产精品成人国产乱一区 | 中文在线一区 | 四虎免费久久 | 91久久人澡人人添人人爽欧美 | 国产精品一区久久 | 久久久久影视 | аⅴ资源新版在线天堂 | 成人午夜一区 | 视频黄页在线观看 | 国产原创在线 | 懂色av色吟av夜夜嗨 | 色翁荡息又大又硬又粗又爽 | 不卡av片| 日本少妇xxxx软件 | 一级全黄裸体免费视频 | 国产精品视频第一页 | 欧美日韩高清在线播放 | 国产精品欧美日韩 | 亚洲精品乱码久久久久久蜜桃麻豆 | 麻豆影视在线观看 | 日本在线中文字幕专区 | 华丽的外出在线观看 | 裸体裸乳免费看 | 久久久久在线视频 | 中文字幕人成 | 毛片毛片毛片毛片毛片毛片毛片毛片 | 日本在线一级片 | 少妇熟女视频一区二区三区 | 亚洲瘦老头同性xxxxx | 在线草 | 日韩欧美一区二 | 黄色在线播放视频 | 肉色超薄丝袜脚交69xx | 亚日韩av| 久久55| 久久五月网 | 日本韩国欧美一区二区 | 女人洗澡一级特黄毛片 | 亚洲精品欧美日韩 | av资源首页 | 国产小视频在线观看免费 | 天天操夜夜添 | 毛片网站免费在线观看 | 古典武侠av | 最新超碰 | 日韩亚洲欧美精品 | 美日韩一二三区 | 香蕉视频在线网址 | 国产三级精品在线 | 亚洲一级在线观看 | 台湾av在线播放 | 免费黄色小网站 |