python爬蟲beautifulsoup解析html方法
用BeautifulSoup 解析html和xml字符串

實例:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from bs4 import BeautifulSoup
import 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" class="sister" id="link1">Elsie</a>,
<a rel="external nofollow" class="sister" id="link2">Lacie</a>
and
<a rel="external nofollow" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.
</p>
<p class="story">...</p>
"""
# html字符串創(chuàng)建BeautifulSoup對象
soup = BeautifulSoup(html_doc, 'html.parser', from_encoding='utf-8')
#輸出第一個 title 標(biāo)簽
print soup.title
#輸出第一個 title 標(biāo)簽的標(biāo)簽名稱
print soup.title.name
#輸出第一個 title 標(biāo)簽的包含內(nèi)容
print soup.title.string
#輸出第一個 title 標(biāo)簽的父標(biāo)簽的標(biāo)簽名稱
print soup.title.parent.name
#輸出第一個 p 標(biāo)簽
print soup.p
#輸出第一個 p 標(biāo)簽的 class 屬性內(nèi)容
print soup.p['class']
#輸出第一個 a 標(biāo)簽的 href 屬性內(nèi)容
print soup.a['href']
'''
soup的屬性可以被添加,刪除或修改. 再說一次, soup的屬性操作方法與字典一樣
'''
#修改第一個 a 標(biāo)簽的href屬性為 http://www.baidu.com/
soup.a['href'] = 'http://www.baidu.com/'
#給第一個 a 標(biāo)簽添加 name 屬性
soup.a['name'] = u'百度'
#刪除第一個 a 標(biāo)簽的 class 屬性為
del soup.a['class']
##輸出第一個 p 標(biāo)簽的所有子節(jié)點
print soup.p.contents
#輸出第一個 a 標(biāo)簽
print soup.a
#輸出所有的 a 標(biāo)簽,以列表形式顯示
print soup.find_all('a')
#輸出第一個 id 屬性等于 link3 的 a 標(biāo)簽
print soup.find(id="link3")
#獲取所有文字內(nèi)容
print(soup.get_text())
#輸出第一個 a 標(biāo)簽的所有屬性信息
print soup.a.attrs
for link in soup.find_all('a'):
#獲取 link 的 href 屬性內(nèi)容
print(link.get('href'))
#對soup.p的子節(jié)點進行循環(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)
爬蟲設(shè)計思路:

詳細手冊:
https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/
到此這篇關(guān)于python爬蟲beautifulsoup解析html方法 的文章就介紹到這了,更多相關(guān)beautifulsoup解析html內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python?Bleach保障網(wǎng)絡(luò)安全防止網(wǎng)站受到XSS(跨站腳本)攻擊
Bleach?不僅可以清理?HTML?文檔,還能夠?qū)︽溄舆M行處理,檢查是否是合法格式,并可以使用白名單來控制哪些?HTML?標(biāo)簽、屬性是安全的,因此非常適合用于清潔用戶輸入的數(shù)據(jù),確保網(wǎng)站安全2024-01-01
基于Python實現(xiàn)智能停車場車牌識別計費系統(tǒng)
這篇文章主要為大家介紹了如何利用Python實現(xiàn)一個智能停車場車牌識別計費系統(tǒng),文中的示例代碼講解詳細,感興趣的小伙伴可以動手嘗試一下2022-04-04
Python?Flask中Cookie和Session區(qū)別詳解
Flask是一個使用?Python?編寫的輕量級?Web?應(yīng)用框架。其?WSGI?工具箱采用?Werkzeug?,模板引擎則使用?Jinja2?。Flask使用?BSD?授權(quán)。Flask也被稱為?“microframework”?,因為它使用簡單的核心,用?extension?增加其他功能,F(xiàn)lask中Cookie和Session有什么區(qū)別呢2022-07-07
python使用matplotlib:subplot繪制多個子圖的示例
這篇文章主要介紹了python使用matplotlib:subplot繪制多個子圖的示例,幫助大家更好的利用python繪制圖像,感興趣的朋友可以了解下2020-09-09
Python tkinter實現(xiàn)圖片標(biāo)注功能(完整代碼)
tkinter是Python下面向tk的圖形界面接口庫,可以方便地進行圖形界面設(shè)計和交互操作編程,本文通過實例代碼給大家介紹的Python tkinter實現(xiàn)圖片標(biāo)注功能,感興趣的朋友一起看看吧2019-12-12
機器學(xué)習(xí)經(jīng)典算法-logistic回歸代碼詳解
這篇文章主要介紹了機器學(xué)習(xí)經(jīng)典算法-logistic回歸代碼詳解,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12
python matplotlib坐標(biāo)軸設(shè)置的方法
本篇文章主要介紹了python matplotlib坐標(biāo)軸設(shè)置的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12

