av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python爬蟲(chóng)beautifulsoup解析html方法

瀏覽:14日期:2022-07-03 08:08:36

用BeautifulSoup 解析html和xml字符串

python爬蟲(chóng)beautifulsoup解析html方法

實(shí)例:

#!/usr/bin/python# -*- coding: UTF-8 -*-from bs4 import BeautifulSoupimport re#待分析字符串html_doc = '''<html><head> <title>The Dormouse’s story</title></head><body><p class='title aq'> <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' id='link1'>Elsie</a>, <a rel='external nofollow' id='link2'>Lacie</a> and <a rel='external nofollow' id='link3'>Tillie</a>; and they lived at the bottom of a well.</p><p class='story'>...</p>'''# html字符串創(chuàng)建BeautifulSoup對(duì)象soup = BeautifulSoup(html_doc, ’html.parser’, from_encoding=’utf-8’)#輸出第一個(gè) title 標(biāo)簽print soup.title#輸出第一個(gè) title 標(biāo)簽的標(biāo)簽名稱(chēng)print soup.title.name#輸出第一個(gè) title 標(biāo)簽的包含內(nèi)容print soup.title.string#輸出第一個(gè) title 標(biāo)簽的父標(biāo)簽的標(biāo)簽名稱(chēng)print soup.title.parent.name#輸出第一個(gè) p 標(biāo)簽print soup.p#輸出第一個(gè) p 標(biāo)簽的 class 屬性?xún)?nèi)容print soup.p[’class’]#輸出第一個(gè) a 標(biāo)簽的 href 屬性?xún)?nèi)容print soup.a[’href’]’’’soup的屬性可以被添加,刪除或修改. 再說(shuō)一次, soup的屬性操作方法與字典一樣’’’#修改第一個(gè) a 標(biāo)簽的href屬性為 http://www.baidu.com/soup.a[’href’] = ’http://www.baidu.com/’#給第一個(gè) a 標(biāo)簽添加 name 屬性soup.a[’name’] = u’百度’#刪除第一個(gè) a 標(biāo)簽的 class 屬性為del soup.a[’class’]##輸出第一個(gè) p 標(biāo)簽的所有子節(jié)點(diǎn)print soup.p.contents#輸出第一個(gè) a 標(biāo)簽print soup.a#輸出所有的 a 標(biāo)簽,以列表形式顯示print soup.find_all(’a’)#輸出第一個(gè) id 屬性等于 link3 的 a 標(biāo)簽print soup.find(id='link3')#獲取所有文字內(nèi)容print(soup.get_text())#輸出第一個(gè) a 標(biāo)簽的所有屬性信息print soup.a.attrsfor link in soup.find_all(’a’): #獲取 link 的 href 屬性?xún)?nèi)容 print(link.get(’href’))#對(duì)soup.p的子節(jié)點(diǎn)進(jìn)行循環(huán)輸出 for child in soup.p.children: print(child)#正則匹配,名字中帶有b的標(biāo)簽for tag in soup.find_all(re.compile('b')): print(tag.name)

爬蟲(chóng)設(shè)計(jì)思路:

python爬蟲(chóng)beautifulsoup解析html方法

詳細(xì)手冊(cè):

https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/

到此這篇關(guān)于python爬蟲(chóng)beautifulsoup解析html方法 的文章就介紹到這了,更多相關(guān)beautifulsoup解析html內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 中国一级特黄真人毛片免费观看 | 二区三区视频 | 久久久久久国产精品免费免费狐狸 | 操夜夜| 精品欧美一区二区在线观看欧美熟 | 一区二区精品视频 | 一区二区三区在线播放 | 亚洲一区中文字幕在线观看 | 韩国av电影网 | 国产99久久久国产精品 | 成人免费在线电影 | 久草在线在线精品观看 | 亚洲经典一区 | 丝袜久久| 成人午夜网站 | 亚洲激情在线 | 欧洲精品在线观看 | 日本黄色大片免费 | 成人国产精品免费观看 | 二区在线视频 | 欧美精品v | av手机在线免费观看 | av片网站| 日韩精品久久 | 少妇精品亚洲一区二区成人 | 久久精品国产99国产精品 | 欧美精品久久久久 | 亚洲三级视频 | 久久综合av | 国产99久久久久 | 欧美涩 | 国产精品毛片一区二区在线看 | 色婷婷综合网 | 夜夜摸夜夜操 | 久久久久久免费观看 | 无吗视频 | 久久国产精品网站 | 天天射天天干 | 91观看| 久久一区二区三区四区 | 成人欧美一区二区三区色青冈 |