亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

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

python beautiful soup庫(kù)入門安裝教程

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

pip install beautifulsoup4beautiful soup庫(kù)的理解

beautiful soup庫(kù)是解析、遍歷、維護(hù)“標(biāo)簽樹”的功能庫(kù)

beautiful soup庫(kù)的引用

from bs4 import BeautifulSoupimport bs4BeautifulSoup類

BeautifulSoup對(duì)應(yīng)一個(gè)HTML/XML文檔的全部?jī)?nèi)容

回顧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標(biāo)簽

基本元素 說(shuō)明 Tag 標(biāo)簽,最基本的信息組織單元,分別用<>和</>標(biāo)明開頭和結(jié)尾

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語(yǔ)法中的標(biāo)簽都可以用soup.訪問(wèn)獲得。當(dāng)HTML文檔中存在多個(gè)相同對(duì)應(yīng)內(nèi)容時(shí),soup.返回第一個(gè)

Tag的name

基本元素 說(shuō)明 Name 標(biāo)簽的名字,

的名字是’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(屬性)

基本元素 說(shuō)明 Attributes 標(biāo)簽的屬性,字典形式組織,格式:.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

基本元素 說(shuō)明 NavigableString 標(biāo)簽內(nèi)非屬性字符串,<>…</>中字符串,格式:.string

Tag的Comment

基本元素 說(shuō)明 Comment 標(biāo)簽內(nèi)字符串的注釋部分,一種特殊的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基本格式標(biāo)簽樹的下行遍歷

屬性 說(shuō)明 .contents 子節(jié)點(diǎn)的列表,將所有兒子結(jié)點(diǎn)存入列表 .children 子節(jié)點(diǎn)的迭代類型,與.contents類似,用于循環(huán)遍歷兒子結(jié)點(diǎn) .descendents 子孫節(jié)點(diǎn)的迭代類型,包含所有子孫節(jié)點(diǎn),用于循環(huán)遍歷

BeautifulSoup類型是標(biāo)簽樹的根節(jié)點(diǎn)

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) #遍歷兒子結(jié)點(diǎn)for child in soup.body.descendants:print(child) #遍歷子孫節(jié)點(diǎn)標(biāo)簽樹的上行遍歷

屬性 說(shuō)明 .parent 節(jié)點(diǎn)的父親標(biāo)簽 .parents 節(jié)點(diǎn)先輩標(biāo)簽的迭代類型,用于循環(huán)遍歷先輩節(jié)點(diǎn)

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]標(biāo)簽的平行遍歷屬性 說(shuō)明 .next_sibling 返回按照HTML文本順序的下一個(gè)平行節(jié)點(diǎn)標(biāo)簽 .previous.sibling 返回按照HTML文本順序的上一個(gè)平行節(jié)點(diǎn)標(biāo)簽 .next_siblings 迭代類型,返回按照HTML文本順序的后續(xù)所有平行節(jié)點(diǎn)標(biāo)簽 .previous.siblings 迭代類型,返回按照HTML文本順序的前續(xù)所有平行節(jié)點(diǎn)標(biāo)簽

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) #遍歷后續(xù)節(jié)點(diǎn)for sibling in soup.a.previous_sibling:print(sibling) #遍歷前續(xù)節(jié)點(diǎn)

python beautiful soup庫(kù)入門安裝教程

bs庫(kù)的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èi)容增加更加’n’.prettify()可用于標(biāo)簽,方法:.prettify()

bs4庫(kù)的編碼

bs4庫(kù)將任何HTML輸入都變成utf-8編碼python 3.x默認(rèn)支持編碼是utf-8,解析無(wú)障礙

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

中文<p> 中文</p>

到此這篇關(guān)于python beautiful soup庫(kù)入門安裝教程的文章就介紹到這了,更多相關(guān)python beautiful soup庫(kù)入門內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 国产精品欧美激情在线播放 | 在线观看中文字幕国产 | 精品手机在线视频 | 日本高清不卡在线观看 | 中文字幕 亚洲 一区二区三区 | 俄罗斯极品美女毛片免费播放 | 日韩在线精品 | 国产精品夫妇久久 | 韩日三级视频 | 999久久久精品视频在线观看 | 欧美亚洲日本视频 | 国产精品夫妇久久 | 久久亚洲国产精品一区二区 | 99精品福利 | 特黄大片aaaaa毛片 | 久久精品国产99久久6动漫欧 | 亚洲一区区 | 在线观看久草视频 | 欧美日韩一区二区综合在线视频 | 国产视频a区| 国产毛片久久久久久国产毛片 | 亚洲一区二区三区四区 | 在线播放成人高清免费视频 | 五月久久亚洲七七综合中文网 | 日本 欧美 国产 | 国产免费一区二区三区免费视频 | 欧美成人影院 在线播放 | 久久91精品国产91久久 | 俺来也欧美亚洲a∨在线 | 久久久一级| 欧美黄色网络 | 日韩美女强理论片 | 国产精品久久九九 | 看v片| 久久99精品久久久久久青青91 | 国产成人综合在线视频 | 亚洲精品69 | 国产香蕉尹人综合在线观 | 美女黄色在线网站大全 | 日韩亚洲欧美一区二区三区 | 亚洲国产一 |