Win平臺:「以管理員身份運行」 cmdhtml
執行 pip install beautifulsoup4python
文件名稱:demo.htmlui
網頁源代碼:HTML 5.0 格式代碼編碼
>>> import requests >>> 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 >>> from bs4 import BeautifulSoup >>> 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: <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> >>>
Beautiful Soup庫的理解:spa
Beautiful Soup庫是解析、遍歷、維護「標籤樹」的功能庫。3d
<p>..</p> : 標籤Tagcode
from bs4 import BeautifulSouphtm
import bs4blog
Beautiful Soup庫解析器:ip
soup = BeautifulSoup ('<html>data</html>','html.parser')
BeautifulSoup類的基本元素:
< p class = "title" > ... </p>
Tag標籤:
>>> from bs4 import BeautifulSoup >>> soup = BeautifulSoup(demo,'html.parser') >>> soup.title <title>This is a python demo page</title> >>> tag = soup.a >>> tag <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
任何存在於HTML語法中的標籤均可以用soup.<tag>訪問得到當HTML文檔中存在多個相同<tag>對應內容時,soup.<tag>返回第一個
Tag的name:
>>> from bs4 import BeautifulSoup >>> soup = BeautifulSoup(demo,'html.parser') >>> soup.a.name 'a' >>> soup.a.parent.name 'p' >>> soup.a.parent.parent.name 'body' >>>
每一個<tag>都有本身的名字,經過<tag>.name獲取,字符串類型
Tag的attrs(屬性):
>>> tag = soup.a >>> 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>能夠有0或多個屬性,字典類型
Tag的NavigableString:
>>> soup.a <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> >>> soup.a.string 'Basic Python' >>> soup.p <p class="title"><b>The demo python introduces several python courses.</b></p> >>> soup.p.string 'The demo python introduces several python courses.' >>> type(soup.p.string) <class 'bs4.element.NavigableString'>
NavigableString能夠跨越多個層次
Tag的Comment:
>>> newsoup = BeautifulSoup("<b><!--This is a comment--></b><p>This is not a comment</p>","html.parser") >>> newsoup.b.string 'This is a comment' >>> type(newsoup.b.string) <class 'bs4.element.Comment'> >>> newsoup.p.string 'This is not a comment' >>> type(newsoup.p.string) <class 'bs4.element.NavigableString'>
Comment是一種特殊類型
標籤<tag>
<>...</>構成了所屬關係,造成了標籤的樹形結構
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.contents ['\n', <p class="title"><b>The demo python introduces several python courses.</b></p>, '\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: <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>, '\n'] >>> len(soup.body.contents) 5 >>> soup.body.contents[1] <p class="title"><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)
soup = BeautifulSoup(demo,'html.parser') for parent in soup.a.parents: #標籤樹的上行遍歷 if parent is None: print(parent) else: print(parent.name)
遍歷全部先輩節點,包括soup自己,因此要區別判斷
運行結果:
平行遍歷發生在同一個父節點下的各節點間
讓HTML內容更加「友好」的顯示:
>>> import requests >>> r = requests.get("http://python123.io/ws//demo.html") >>> demo = r.text >>> demo '<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>' >>> soup = BeautifulSoup(demo,'html.parser') >>> 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>
.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>
bs4庫將任何HTML輸入都變爲utf-8編碼,Python 3.x默認支持編碼是utf-8,解析無障礙。
>>> soup = BeautifulSoup("<p>中文</p>",'html.parser') >>> soup.p.string '中文' >>> print(soup.p.prettify()) <p> 中文 </p>