最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python beautiful soup庫入門安裝教程

 更新時間:2021年08月30日 14:31:21   作者:Cachel wood  
Beautiful Soup是python的一個庫,最主要的功能是從網(wǎng)頁抓取數(shù)據(jù)。今天通過本文給大家分享python beautiful soup庫入門教程,需要的朋友參考下吧

beautiful soup庫的安裝

pip install beautifulsoup4

beautiful soup庫的理解

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

beautiful soup庫的引用

from bs4 import BeautifulSoup
import bs4

BeautifulSoup類

BeautifulSoup對應一個HTML/XML文檔的全部內(nèi)容

回顧demo.html

import requests

r = requests.get("http://python123.io/ws/demo.html")
demo = r.text
print(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  class="py1" id="link1">Basic Python</a> and <a  class="py2" id="link2">Advanced Python</a>.</p>
</body></html>

Tag標簽

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

import requests
from bs4 import BeautifulSoup
r = requests.get("http://python123.io/ws/demo.html")
demo = r.text
soup = BeautifulSoup(demo,"html.parser")
print(soup.title)
tag = soup.a
print(tag)
<title>This is a python demo page</title>
<a   >Basic Python</a>

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

Tag的name

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

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

import requests
from bs4 import BeautifulSoup
r = requests.get("http://python123.io/ws/demo.html")
demo = r.text
soup = BeautifulSoup(demo,"html.parser")
print(soup.a.name)
print(soup.a.parent.name)
print(soup.a.parent.parent.name)
a
p   
body

Tag的attrs(屬性)

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

import requests
from bs4 import BeautifulSoup
r = requests.get("http://python123.io/ws/demo.html")
demo = r.text
soup = BeautifulSoup(demo,"html.parser")
tag = soup.a
print(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 標簽內(nèi)非屬性字符串,<>…</>中字符串,格式:.string

Tag的Comment

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

import requests
from bs4 import BeautifulSoup
newsoup = 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 子節(jié)點的列表,將所有兒子結(jié)點存入列表
.children 子節(jié)點的迭代類型,與.contents類似,用于循環(huán)遍歷兒子結(jié)點
.descendents 子孫節(jié)點的迭代類型,包含所有子孫節(jié)點,用于循環(huán)遍歷

BeautifulSoup類型是標簽樹的根節(jié)點

import requests
from bs4 import BeautifulSoup
r = requests.get("http://python123.io/ws/demo.html")
demo = r.text

soup = 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é)點
for child in soup.body.descendants:
	print(child) #遍歷子孫節(jié)點

標簽樹的上行遍歷

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

import requests
from bs4 import BeautifulSoup
r = requests.get("http://python123.io/ws/demo.html")
demo = r.text

soup = 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 requests
from bs4 import BeautifulSoup
r = requests.get("http://python123.io/ws/demo.html")
demo = r.text

soup = BeautifulSoup(demo,"html.parser")
for parent in soup.a.parents:
    if parent is None:
        print(parent)
    else:
        print(parent.name)
p
body      
html      
[document]

標簽的平行遍歷

屬性 說明
.next_sibling 返回按照HTML文本順序的下一個平行節(jié)點標簽
.previous.sibling 返回按照HTML文本順序的上一個平行節(jié)點標簽
.next_siblings 迭代類型,返回按照HTML文本順序的后續(xù)所有平行節(jié)點標簽
.previous.siblings 迭代類型,返回按照HTML文本順序的前續(xù)所有平行節(jié)點標簽
import requests
from bs4 import BeautifulSoup
r = requests.get("http://python123.io/ws/demo.html")
demo = r.text

soup = 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 class="py2"  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 class="py1"  id="link1">Basic Python</a> and <a class="py2"  id="link2">Advanced Python</a>.</p>
for sibling in soup.a.next_sibling:
	print(sibling)  #遍歷后續(xù)節(jié)點
for sibling in soup.a.previous_sibling:
	print(sibling)  #遍歷前續(xù)節(jié)點

在這里插入圖片描述

bs庫的prettify()方法

import requests
from bs4 import BeautifulSoup
r = requests.get("http://python123.io/ws/demo.html")
demo = r.text

soup = 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 class="py2"  id="link2">
    Advanced Python
   </a>
   .
  </p>
 </body>
</html>

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

bs4庫的編碼

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

import requests
from bs4 import BeautifulSoup

soup = BeautifulSoup("<p>中文</p>","html.parser")
print(soup.p.string)

print(soup.p.prettify())
中文

<p>  
 中文
</p> 

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

相關(guān)文章

  • 基于python 爬蟲爬到含空格的url的處理方法

    基于python 爬蟲爬到含空格的url的處理方法

    今天小編就為大家分享一篇基于python 爬蟲爬到含空格的url的處理方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • 關(guān)于pandas的read_csv方法使用解讀

    關(guān)于pandas的read_csv方法使用解讀

    這篇文章主要介紹了關(guān)于pandas的read_csv方法使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • 進一步探究Python中的正則表達式

    進一步探究Python中的正則表達式

    這篇文章主要介紹了Python中的正則表達式的一些用法,正則表達式的使用是Python學習進階中的重要知識,需要的朋友可以參考下
    2015-04-04
  • 如何實現(xiàn)python爬蟲爬取視頻時實現(xiàn)實時進度條顯示

    如何實現(xiàn)python爬蟲爬取視頻時實現(xiàn)實時進度條顯示

    這篇文章主要介紹了如何實現(xiàn)python爬蟲爬取視頻時實現(xiàn)實時進度條顯示,在爬取并下載網(wǎng)頁上的視頻的時候,我們需要實時進度條,這可以幫助我們更直觀的看到視頻的下載進度。文章圍繞主題展開更多內(nèi)容,需要的小伙伴可以參考一下
    2022-06-06
  • python庫umap有效地揭示高維數(shù)據(jù)的結(jié)構(gòu)和模式初探

    python庫umap有效地揭示高維數(shù)據(jù)的結(jié)構(gòu)和模式初探

    這篇文章主要介紹了python庫umap有效地揭示高維數(shù)據(jù)的結(jié)構(gòu)和模式初探,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • Python中bisect的用法

    Python中bisect的用法

    這篇文章主要介紹了Python中bisect的用法,主要講述了針對數(shù)組的插入及排序操作,非常具有實用價值,需要的朋友可以參考下
    2014-09-09
  • Python爬蟲使用實例wallpaper問題記錄

    Python爬蟲使用實例wallpaper問題記錄

    本文介紹解決中文亂碼的方法,以及Python爬蟲處理數(shù)據(jù)、圖片URL的技巧,包括使用正則表達式處理字符串、URL替換等,還涉及單線程與多線程的應用場景,如電腦壁紙和手機壁紙爬取,適合進行Web數(shù)據(jù)抓取和處理的開發(fā)者參考
    2024-09-09
  • 對python3中, print橫向輸出的方法詳解

    對python3中, print橫向輸出的方法詳解

    今天小編就為大家分享一篇對python3中, print橫向輸出的方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • python?pandas處理excel表格數(shù)據(jù)的常用方法總結(jié)

    python?pandas處理excel表格數(shù)據(jù)的常用方法總結(jié)

    在計算機編程中,pandas是Python編程語言的用于數(shù)據(jù)操縱和分析的軟件庫,下面這篇文章主要給大家介紹了關(guān)于python?pandas處理excel表格數(shù)據(jù)的常用方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-07-07
  • Python+SimpleRNN實現(xiàn)股票預測詳解

    Python+SimpleRNN實現(xiàn)股票預測詳解

    這篇文章主要為大家詳細介紹了如何利用Python和SimpleRNN實現(xiàn)股票預測效果,文中的示例代碼講解詳細,對我們學習有一定幫助,需要的可以參考一下
    2022-05-05

最新評論

福海县| 江源县| 抚松县| 洞头县| 赫章县| 东至县| 常州市| 婺源县| 勃利县| 新建县| 都昌县| 浠水县| 商都县| 历史| 平远县| 平远县| 澄城县| 琼海市| 襄城县| 阳原县| 广汉市| 上犹县| 安丘市| 康乐县| 渑池县| 当阳市| 湟中县| 德令哈市| 横山县| 华亭县| 霍州市| 岚皋县| 宝丰县| 望城县| 东乌珠穆沁旗| 柏乡县| 临夏县| 南皮县| 塔城市| 方城县| 崇明县|