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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python爬虫beautifulsoup_python爬虫beautifulsoup解析html方法

發布時間:2025/3/15 python 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python爬虫beautifulsoup_python爬虫beautifulsoup解析html方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

用BeautifulSoup 解析html和xml字符串

實例:

#!/usr/bin/python

# -*- coding: UTF-8 -*-

from bs4 import BeautifulSoup

import re

#待分析字符串

html_doc = """

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.

...

"""

# html字符串創建BeautifulSoup對象

soup = BeautifulSoup(html_doc, 'html.parser', from_encoding='utf-8')

#輸出第一個 title 標簽

print soup.title

#輸出第一個 title 標簽的標簽名稱

print soup.title.name

#輸出第一個 title 標簽的包含內容

print soup.title.string

#輸出第一個 title 標簽的父標簽的標簽名稱

print soup.title.parent.name

#輸出第一個 p 標簽

print soup.p

#輸出第一個 p 標簽的 class 屬性內容

print soup.p['class']

#輸出第一個 a 標簽的 href 屬性內容

print soup.a['href']

'''

soup的屬性可以被添加,刪除或修改. 再說一次, soup的屬性操作方法與字典一樣

'''

#修改第一個 a 標簽的href屬性為 http://www.baidu.com/

soup.a['href'] = 'http://www.baidu.com/'

#給第一個 a 標簽添加 name 屬性

soup.a['name'] = u'百度'

#刪除第一個 a 標簽的 class 屬性為

del soup.a['class']

##輸出第一個 p 標簽的所有子節點

print soup.p.contents

#輸出第一個 a 標簽

print soup.a

#輸出所有的 a 標簽,以列表形式顯示

print soup.find_all('a')

#輸出第一個 id 屬性等于 link3 的 a 標簽

print soup.find(id="link3")

#獲取所有文字內容

print(soup.get_text())

#輸出第一個 a 標簽的所有屬性信息

print soup.a.attrs

for link in soup.find_all('a'):

#獲取 link 的 href 屬性內容

print(link.get('href'))

#對soup.p的子節點進行循環輸出

for child in soup.p.children:

print(child)

#正則匹配,名字中帶有b的標簽

for tag in soup.find_all(re.compile("b")):

print(tag.name)

爬蟲設計思路:

詳細手冊:

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

到此這篇關于python爬蟲beautifulsoup解析html方法 的文章就介紹到這了,更多相關beautifulsoup解析html內容請搜索以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持!

總結

以上是生活随笔為你收集整理的python爬虫beautifulsoup_python爬虫beautifulsoup解析html方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。