色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

python beautiful soup庫入門安裝教程

瀏覽:3日期:2022-08-06 18:29:08
目錄beautiful soup庫的安裝beautiful soup庫的理解beautiful soup庫的引用BeautifulSoup類回顧demo.htmlTag標簽Tag的nameTag的attrs(屬性)Tag的NavigableStringHTML基本格式標簽樹的下行遍歷標簽樹的上行遍歷標簽的平行遍歷bs庫的prettify()方法bs4庫的編碼beautiful soup庫的安裝

pip install beautifulsoup4beautiful soup庫的理解

beautiful soup庫是解析、遍歷、維護“標簽樹”的功能庫

beautiful soup庫的引用

from bs4 import BeautifulSoupimport bs4BeautifulSoup類

BeautifulSoup對應一個HTML/XML文檔的全部內容

回顧demo.html

import requestsr = requests.get('http://python123.io/ws/demo.html')demo = r.textprint(demo)

<html><head><title>This is a python demo page</title></head><body><p class='title'><b>The demo python introduces several python courses.</b></p><p class='course'>Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:<a id='link1'>Basic Python</a> and <a id='link2'>Advanced Python</a>.</p></body></html>Tag標簽

基本元素 說明 Tag 標簽,最基本的信息組織單元,分別用<>和</>標明開頭和結尾

import requestsfrom bs4 import BeautifulSoupr = requests.get('http://python123.io/ws/demo.html')demo = r.textsoup = BeautifulSoup(demo,'html.parser')print(soup.title)tag = soup.aprint(tag)

<title>This is a python demo page</title><a >Basic Python</a>

任何存在于HTML語法中的標簽都可以用soup.訪問獲得。當HTML文檔中存在多個相同對應內容時,soup.返回第一個

Tag的name

基本元素 說明 Name 標簽的名字,

的名字是’p’,格式:.name

import requestsfrom bs4 import BeautifulSoupr = requests.get('http://python123.io/ws/demo.html')demo = r.textsoup = BeautifulSoup(demo,'html.parser')print(soup.a.name)print(soup.a.parent.name)print(soup.a.parent.parent.name)

ap bodyTag的attrs(屬性)

基本元素 說明 Attributes 標簽的屬性,字典形式組織,格式:.attrs

import requestsfrom bs4 import BeautifulSoupr = requests.get('http://python123.io/ws/demo.html')demo = r.textsoup = BeautifulSoup(demo,'html.parser')tag = soup.aprint(tag.attrs)print(tag.attrs[’class’])print(tag.attrs[’href’])print(type(tag.attrs))print(type(tag))

{’href’: ’http://www.icourse163.org/course/BIT-268001’, ’class’: [’py1’], ’id’: ’link1’}[’py1’]http://www.icourse163.org/course/BIT-268001<class ’dict’><class ’bs4.element.Tag’>Tag的NavigableString

Tag的NavigableString

基本元素 說明 NavigableString 標簽內非屬性字符串,<>…</>中字符串,格式:.string

Tag的Comment

基本元素 說明 Comment 標簽內字符串的注釋部分,一種特殊的Comment類型

import requestsfrom bs4 import BeautifulSoupnewsoup = BeautifulSoup('<b><!--This is a comment--></b><p>This is not a comment</p>','html.parser')print(newsoup.b.string)print(type(newsoup.b.string))print(newsoup.p.string)print(type(newsoup.p.string))

This is a comment<class ’bs4.element.Comment’>This is not a comment<class ’bs4.element.NavigableString’>HTML基本格式標簽樹的下行遍歷

屬性 說明 .contents 子節點的列表,將所有兒子結點存入列表 .children 子節點的迭代類型,與.contents類似,用于循環遍歷兒子結點 .descendents 子孫節點的迭代類型,包含所有子孫節點,用于循環遍歷

BeautifulSoup類型是標簽樹的根節點

import requestsfrom bs4 import BeautifulSoupr = requests.get('http://python123.io/ws/demo.html')demo = r.textsoup = BeautifulSoup(demo,'html.parser')print(soup.head)print(soup.head.contents)print(soup.body.contents)print(len(soup.body.contents))print(soup.body.contents[1])

<head><title>This is a python demo page</title></head>[<title>This is a python demo page</title>][’n’, <p ><b>The demo python introduces several python courses.</b></p>, ’n’, <p >Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:<a >Basic Python</a> and <a >Advanced Python</a>.</p>, ’n’]5<p ><b>The demo python introduces several python courses.</b></p>

for child in soup.body.children:print(child) #遍歷兒子結點for child in soup.body.descendants:print(child) #遍歷子孫節點標簽樹的上行遍歷

屬性 說明 .parent 節點的父親標簽 .parents 節點先輩標簽的迭代類型,用于循環遍歷先輩節點

import requestsfrom bs4 import BeautifulSoupr = requests.get('http://python123.io/ws/demo.html')demo = r.textsoup = BeautifulSoup(demo,'html.parser')print(soup.title.parent)print(soup.html.parent)

<head><title>This is a python demo page</title></head><html><head><title>This is a python demo page</title></head><body><p ><b>The demo python introduces several python courses.</b></p><p >Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:<a >Basic Python</a> and <a >Advanced Python</a>.</p></body></html>

import requestsfrom bs4 import BeautifulSoupr = requests.get('http://python123.io/ws/demo.html')demo = r.textsoup = BeautifulSoup(demo,'html.parser')for parent in soup.a.parents: if parent is None:print(parent) else:print(parent.name)

pbody html [document]標簽的平行遍歷屬性 說明 .next_sibling 返回按照HTML文本順序的下一個平行節點標簽 .previous.sibling 返回按照HTML文本順序的上一個平行節點標簽 .next_siblings 迭代類型,返回按照HTML文本順序的后續所有平行節點標簽 .previous.siblings 迭代類型,返回按照HTML文本順序的前續所有平行節點標簽

import requestsfrom bs4 import BeautifulSoupr = requests.get('http://python123.io/ws/demo.html')demo = r.textsoup = BeautifulSoup(demo,'html.parser')print(soup.a.next_sibling)print(soup.a.next_sibling.next_sibling)print(soup.a.previous_sibling)print(soup.a.previous_sibling.previous_sibling)print(soup.a.parent)

and <a id='link2'>Advanced Python</a>Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:None<p class='course'>Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:<a id='link1'>Basic Python</a> and <a id='link2'>Advanced Python</a>.</p>

for sibling in soup.a.next_sibling:print(sibling) #遍歷后續節點for sibling in soup.a.previous_sibling:print(sibling) #遍歷前續節點

python beautiful soup庫入門安裝教程

bs庫的prettify()方法

import requestsfrom bs4 import BeautifulSoupr = requests.get('http://python123.io/ws/demo.html')demo = r.textsoup = BeautifulSoup(demo,'html.parser')print(soup.prettify())

<html> <head> <title> This is a python demo page </title> </head> <body> <p class='title'> <b> The demo python introduces several python courses. </b> </p> <p class='course'> Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses: Basic Python </a> and <a id='link2'> Advanced Python </a> . </p> </body></html>

.prettify()為HTML文本<>及其內容增加更加’n’.prettify()可用于標簽,方法:.prettify()

bs4庫的編碼

bs4庫將任何HTML輸入都變成utf-8編碼python 3.x默認支持編碼是utf-8,解析無障礙

import requestsfrom bs4 import BeautifulSoupsoup = BeautifulSoup('<p>中文</p>','html.parser')print(soup.p.string)print(soup.p.prettify())

中文<p> 中文</p>

到此這篇關于python beautiful soup庫入門安裝教程的文章就介紹到這了,更多相關python beautiful soup庫入門內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 一区在线免费观看 | h网站国产| 香港三级日本三级人妇三级四 | 欧美色偷偷 | 97久久精品午夜一区二区 | 国产欧美日韩亚洲精品区2345 | 中文字幕在线播 | 91欧美亚洲| 视频精品一区 | 成人在线亚洲 | 一道精品视频一区二区三区图片 | 国产一区二区日韩欧美在线 | 夜色www国产精品资源站 | 国产91一区二区在线播放不卡 | a级日韩乱理伦片在线观看 a级特黄毛片免费观看 | 99久久国产免费福利 | 女人张开腿让男人捅视频 | 国产美女一级特黄毛片 | 欧美综合自拍亚洲综合百度 | 国产美女91视频 | 超级碰碰碰在线观看 | 成人综合在线观看 | 草草视频在线播放 | 久草在线免费色站 | 国产一区二区三区四区在线 | 亚洲第一在线播放 | 久久国产精品最新一区 | 国产精品第五页 | 夜夜操影院 | 国产农村一二三区 | 欧美专区在线视频 | 亚州综合| 国产三级黄色片 | 国内精品一区二区三区最新 | 日韩一级免费视频 | 国产精品青草久久 | 国产成人精品视频午夜 | 国产精品黑丝 | 国产精品亚洲成在人线 | 欧美黄色一级在线 | 亚洲福利视频一区二区三区 |