婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > python beautiful soup庫入門安裝教程

python beautiful soup庫入門安裝教程

熱門標簽:企業微信地圖標注 B52系統電梯外呼顯示E7 鶴壁手機自動外呼系統違法嗎 地圖標注多個 沈陽防封電銷電話卡 怎么辦理400客服電話 銀川電話機器人電話 高德地圖標注收入咋樣 萊蕪電信外呼系統

beautiful soup庫的安裝

pip install beautifulsoup4

beautiful soup庫的理解

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

beautiful soup庫的引用

from bs4 import BeautifulSoup
import bs4

BeautifulSoup類

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

回顧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 標簽,最基本的信息組織單元,分別用>和/>標明開頭和結尾

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文檔中存在多個相同對應內容時,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 標簽內非屬性字符串,>…/>中字符串,格式:.string

Tag的Comment

基本元素 說明
Comment 標簽內字符串的注釋部分,一種特殊的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 子節點的列表,將所有兒子結點存入列表
.children 子節點的迭代類型,與.contents類似,用于循環遍歷兒子結點
.descendents 子孫節點的迭代類型,包含所有子孫節點,用于循環遍歷

BeautifulSoup類型是標簽樹的根節點

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

標簽樹的上行遍歷

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

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文本順序的下一個平行節點標簽
.previous.sibling 返回按照HTML文本順序的上一個平行節點標簽
.next_siblings 迭代類型,返回按照HTML文本順序的后續所有平行節點標簽
.previous.siblings 迭代類型,返回按照HTML文本順序的前續所有平行節點標簽
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)  #遍歷后續節點
for sibling in soup.a.previous_sibling:
	print(sibling)  #遍歷前續節點

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'
.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> 

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

您可能感興趣的文章:
  • Python BeautifulSoup基本用法詳解(通過標簽及class定位元素)
  • python爬蟲學習筆記--BeautifulSoup4庫的使用詳解
  • Python爬蟲進階之Beautiful Soup庫詳解
  • python爬蟲beautifulsoup庫使用操作教程全解(python爬蟲基礎入門)
  • python網絡爬蟲精解之Beautiful Soup的使用說明

標簽:銀川 葫蘆島 湘西 呼倫貝爾 安慶 呼倫貝爾 烏魯木齊 三亞

巨人網絡通訊聲明:本文標題《python beautiful soup庫入門安裝教程》,本文關鍵詞  python,beautiful,soup,庫,入門,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《python beautiful soup庫入門安裝教程》相關的同類信息!
  • 本頁收集關于python beautiful soup庫入門安裝教程的相關信息資訊供網民參考!
  • 推薦文章
    婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av
    亚洲午夜免费电影| 亚州成人在线电影| 中文字幕亚洲在| 亚洲国产sm捆绑调教视频| 精品国产乱码久久久久久夜甘婷婷 | 337p亚洲精品色噜噜噜| 精品第一国产综合精品aⅴ| 亚洲国产精品成人久久综合一区| 一区二区三区国产精品| 久久99最新地址| 99久久免费精品| 亚洲精品一区二区三区精华液 | 日韩午夜av一区| 国产精品毛片a∨一区二区三区 | 亚洲欧洲国产日本综合| 免费人成在线不卡| 97久久人人超碰| 欧美精选午夜久久久乱码6080| 日韩欧美一区二区在线视频| 国产精品美女久久久久高潮| 亚洲国产精品一区二区尤物区| 国产一区二区在线免费观看| 91视频一区二区三区| 日韩午夜在线观看视频| 亚洲欧美国产高清| 国产乱人伦偷精品视频不卡 | 51久久夜色精品国产麻豆| 亚洲国产高清在线| 视频在线观看一区| 一本久道久久综合中文字幕| 久久久精品黄色| 日本最新不卡在线| 国产精品综合网| 91成人网在线| 17c精品麻豆一区二区免费| 国模套图日韩精品一区二区| 欧美老年两性高潮| 亚洲综合免费观看高清完整版| 国产精品亚洲一区二区三区妖精 | 在线播放中文一区| 亚洲精品成人精品456| 国产在线精品不卡| 国产精品久久久久影院老司 | 欧美成人官网二区| 日韩和欧美一区二区| 色欧美片视频在线观看| 国产精品家庭影院| 韩国精品在线观看| 日韩一区二区在线看| 亚洲国产日产av| 欧美亚洲综合色| 亚洲精品网站在线观看| 成人h动漫精品一区二区| 久久伊人中文字幕| 免费观看30秒视频久久| 51精品久久久久久久蜜臀| 午夜视黄欧洲亚洲| 欧美午夜寂寞影院| 天堂在线亚洲视频| 欧美日韩一区精品| 一区二区三区四区不卡视频| 一本久道中文字幕精品亚洲嫩| 亚洲伦理在线精品| 色8久久人人97超碰香蕉987| 亚洲一区二区三区视频在线播放 | 国产露脸91国语对白| 91精品国产综合久久精品| 五月天丁香久久| 精品国产乱码久久久久久影片| 黄色资源网久久资源365| 国产午夜精品美女毛片视频| 国产一区二区三区四区五区美女 | av中文字幕不卡| 亚洲人精品一区| 日本精品免费观看高清观看| 日本女优在线视频一区二区 | 亚洲精品国久久99热| 在线观看成人小视频| 蜜桃精品视频在线| 精品久久久久久综合日本欧美| 国产精品77777| 亚洲综合一区在线| 精品剧情v国产在线观看在线| 成人毛片在线观看| 午夜精品一区二区三区电影天堂| 欧美成人精品福利| 另类小说欧美激情| 国产丝袜在线精品| 91久久国产综合久久| 久久国产精品免费| 亚洲日本韩国一区| 91精品久久久久久久99蜜桃 | 欧美日韩三级一区| 国产在线看一区| 玉米视频成人免费看| 精品乱码亚洲一区二区不卡| 91麻豆高清视频| 狠狠久久亚洲欧美| 午夜欧美一区二区三区在线播放| 精品99一区二区| 在线一区二区三区四区五区 | 精品国产91乱码一区二区三区 | 国产视频一区在线播放| 欧美视频自拍偷拍| 国产91清纯白嫩初高中在线观看| 视频一区中文字幕国产| 国产精品超碰97尤物18| 精品国产一区二区亚洲人成毛片| 91论坛在线播放| 国产精品原创巨作av| 亚洲激情图片一区| 欧美一级久久久| 日本久久一区二区| 成人av免费观看| 国产乱色国产精品免费视频| 日韩精品视频网| 欧美韩日一区二区三区四区| 欧美大度的电影原声| 欧美撒尿777hd撒尿| 97se亚洲国产综合自在线| 黑人巨大精品欧美黑白配亚洲| 中文字幕一区二区三区四区不卡| 欧美一级日韩一级| 欧美亚洲综合网| 在线观看网站黄不卡| 91麻豆视频网站| 国产在线精品免费| 久久成人免费电影| 日韩av一级电影| 日韩av网站在线观看| 日产欧产美韩系列久久99| 国产一区二区不卡在线| 粉嫩一区二区三区性色av| eeuss鲁片一区二区三区在线观看| 99视频有精品| 6080国产精品一区二区| 久久午夜色播影院免费高清 | 日本韩国一区二区三区视频| 欧美日韩在线电影| 欧美白人最猛性xxxxx69交| 国产精品色呦呦| 亚洲成人在线免费| 国产精品456露脸| 在线精品视频一区二区| 日韩欧美在线观看一区二区三区| 国产人妖乱国产精品人妖| 一区二区三区四区激情| 久久激情五月婷婷| 99久久精品免费观看| 91精品国产高清一区二区三区| 国产婷婷一区二区| 亚洲成人1区2区| 风间由美性色一区二区三区| 欧美日韩在线播| 国产精品久久久久久久久免费相片 | 久久精品国产77777蜜臀| www.久久精品| 久久综合久久99| 香蕉成人啪国产精品视频综合网| 国产精品正在播放| 欧美久久久久免费| 中文字幕中文在线不卡住| 久久av老司机精品网站导航| 91美女片黄在线观看| 久久久国产精品午夜一区ai换脸| 午夜精品影院在线观看| 91啪九色porn原创视频在线观看| 欧美日本一区二区三区四区| 国产精品久久久久7777按摩| 久久精品国产精品亚洲精品| 欧美性欧美巨大黑白大战| 中文字幕av不卡| 国产福利一区二区三区| 欧美一级欧美三级在线观看| 亚洲国产欧美在线| 91最新地址在线播放| 国产欧美日韩在线| 精品午夜一区二区三区在线观看| 91蜜桃免费观看视频| 国产精品久久午夜夜伦鲁鲁| 国内成+人亚洲+欧美+综合在线| 在线播放一区二区三区| 亚洲国产一区二区三区| 91蝌蚪porny九色| 成人免费在线播放视频| 成人动漫精品一区二区| 久久精品一区二区三区不卡牛牛| 日本系列欧美系列| 欧美日韩色综合| 日精品一区二区三区| 欧美日韩国产高清一区二区| 午夜精品在线看| 91精选在线观看| 日本欧美一区二区| 欧美一区二区三区电影| 视频一区在线播放| 日韩一区二区三区在线| 久久精品国产澳门| 亚洲精品一区二区三区四区高清| 国产一区不卡视频|