理解
基本元素
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) #遍历后续节点