欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python beautiful soup库入门安装教程

程序员文章站 2022-06-24 20:09:54
目录beautifulsoup类tag标签tag的attrs(属性)tag的navigablestringhtml基本格式beautiful soup库的安装pip install beautiful...

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 href="http://www.icourse163.org/course/bit-268001" class="py1" id="link1">basic python</a> and <a href="http://www.icourse163.org/course/bit-1001870001" 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  href="http://www.icourse163.org/course/bit-268001" >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  href="http://www.icourse163.org/course/bit-268001" >basic python</a> and <a  href="http://www.icourse163.org/course/bit-1001870001" >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  href="http://www.icourse163.org/course/bit-268001" >basic python</a> and <a  href="http://www.icourse163.org/course/bit-1001870001" >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" href="http://www.icourse163.org/course/bit-1001870001" 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" href="http://www.icourse163.org/course/bit-268001" id="link1">basic python</a> and <a class="py2" href="http://www.icourse163.org/course/bit-1001870001" 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 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" href="http://www.icourse163.org/course/bit-1001870001" 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库入门内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!