ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Beautiful Soup 库

2022-02-06 21:04:49  阅读:193  来源: 互联网

标签:Beautiful soup Python demo python Soup tag courses


理解

基本元素

from bs4 import BeautifulSoup
soup = BeautifulSoup(demo,'html.parser')
r = requests.get("http://python123.io/ws/demo.html")
r.text
'<html><head><title>This is a python demo page</title></head>\r\n<body>\r\n<p class="title"><b>The demo python introduces several python courses.</b></p>\r\n<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n<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>\r\n</body></html>'
demo = r.text
#bs4的prettify()方法可以使HTML内容更加友好的显示
soup.prettify()  
'<html>\n <head>\n  <title>\n   This is a python demo page\n  </title>\n </head>\n <body>\n  <p class="title">\n   <b>\n    The demo python introduces several python courses.\n   </b>\n  </p>\n  <p class="course">\n   Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\n   <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">\n    Basic Python\n   </a>\n   and\n   <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">\n    Advanced Python\n   </a>\n   .\n  </p>\n </body>\n</html>'
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:
   <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>
 </body>
</html>
>>> soup.title
<title>This is a python demo page</title>
#.prettify()为HTML文本<>及其内容增加 '\n'
#.prettify()可用于标签,方法 : <tag>.prettify()
print(soup.a.prettify())
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">
 Basic Python
</a>

 

>>> soup.title
<title>This is a python demo page</title>
# 任何存在于HTML语法中的标签都可用soup.<tag>访问获得
# 当文档中存在多个相同<tag>对应内容时,soup.<tag>返回第一个
>>> tag = soup.a
>>> tag
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
>>> tag = soup.p
>>> tag
<p class="title"><b>The demo python introduces several python courses.</b></p>
>>> tag = soup.a

>>> tag.attrs
{'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
#每个<tag>都有自己的名字,通过<tag>.name获取,得到的名字为字符串类型
>>> soup.a.name
'a'
>>> soup.p.name
'p'
>>> soup.a.parent.parent.name
'body'
#一个<tag>可有0个或多个属性,得到的属性表示方式为字典类型
>>> tag.attrs
{'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
>>> tag.attrs['class']
['py1']
>>> tag.attrs['href']
'http://www.icourse163.org/course/BIT-268001'
>>> type(tag.attrs)
<class 'dict'>
>>> type(tag)
<class 'bs4.element.Tag'>

#Tag的NavigableString

#Tag的Comment

 

 基于bs4库的HTML内容遍历方法

 >>>标签树的下行遍历    BeautifulSoup类型是标签树的根节点

>>> soup = BeautifulSoup(demo,"html.parser")
>>> soup.head
<head><title>This is a python demo page</title></head>
>>> soup.head.contents
[<title>This is a python demo page</title>]
>>> soup.body
<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 track
ing 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.icour
se163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p> </body> ####### >>> soup.body.contents [[0]'\n',[1] <p class="title"><b>The demo python introduces several python courses.</b></p>, [2]'\n',[3] <p class="course">Python is a won
derful 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.icour
se163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>,[4] '\n'] >>> len(soup.body.contents) 5 >>> soup.body.contents[2] '\n' ########################################################### for child in soup.body.childern: print(child) #遍历儿子节点 for child in soup.body.descendants: print(child) #遍历子孙节点

 

>>>标签树的上行遍历

>>> 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]
>>> 

 

>>>标签树的平行遍历

                            

>>> soup
<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" 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>
</body></html>
>>> soup.a.next_sibling
' and '
>>> soup.a.next_sibling.next_sibling
<a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>

#标签树的平行遍历
for sibling in soup.a.next_sibling:
    print(sibling)            #遍历后续节点
for sibling in soup.a.previous_sibling:
    print(sibling)           #遍历后续节点

 

标签:Beautiful,soup,Python,demo,python,Soup,tag,courses
来源: https://www.cnblogs.com/tyuni-1607/p/15865768.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有