Python3---BeautifulSoup---节点选择器
代码部分:
1 from bs4 import BeautifulSoup 2 3 #下面代码示例都是用此文档测试 4 html_doc = """ 5 <html><head><title>The Dormouse's story</title></head> 6 <body> 7 <p class="title"><b>The Dormouse's story</b></p> 8 9 <p class="story">Once upon a time there were three little sisters; and their names were 10 <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>, 11 <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and 12 <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; 13 and they lived at the bottom of a well.</p> 14 15 <p class="story">...</p> 16 """ 17 soup = BeautifulSoup(html_doc,'lxml') 18 print("1;获取head标签") 19 print(soup.head) 20 print("2;#获取p节点下的b节点") 21 print(soup.p.b) 22 #name属性获取节点名称: 23 print("4;name属性获取节点名称") 24 print(soup.body.name) 25 #attrs属性获取节点属性,也可以字典的形式直接获取,返回的结果可能是列表或字符串类型,取决于节点类型 26 print("5;获取p节点所有属性") 27 print(soup.p.attrs) 28 print("6;获取p节点class属性") 29 print(soup.p.attrs['class']) 30 print("7;直接获取p节点class属性") 31 print(soup.p['class']) 32 #string属性获取节点元素包含的文本内容: 33 print("8;获取a标签下的文本,只获取第一个") 34 print(soup.p.string) 35 #contents属性获取节点的直接子节点,以列表的形式返回内容 36 print("9;contents属性获取节点的直接子节点,以列表的形式返回内容") 37 print(soup.body.contents) 38 #children属性获取的也是节点的直接子节点,只是以生成器的类型返回 39 print("10;children属性获取的也是节点的直接子节点,只是以生成器的类型返回") 40 print(soup.body.children) 41 #descendants属性获取子孙节点,返回生成器 42 print("11;descendants属性获取子孙节点,返回生成器") 43 print(soup.body.descendants) 44 #parent属性获取父节点,parents获取祖先节点,返回生成器 45 print("12;parent属性获取父节点,parents获取祖先节点,返回生成器") 46 print(soup.b.parent) 47 print(soup.b.parents) 48 #next_sibling属性返回下一个兄弟节点 49 print("13;next_sibling属性返回下一个兄弟节点") 50 print(soup.a.next_sibling) 51 #previous_sibling返回上一个兄弟节点,注意换行符也是一个节点 52 print("14;previous_sibling返回上一个兄弟节点,注意换行符也是一个节点") 53 print(soup.a.previous_sibling) 54 #next_siblings属性返回下所有兄弟节点 55 print("15;next_sibling属性返回下一个兄弟节点") 56 print(soup.a.next_siblings) 57 #previous_siblings返回上所有兄弟节点,注意换行符也是一个节点 58 print("16;previous_sibling返回上一个兄弟节点,注意换行符也是一个节点") 59 print(soup.a.previous_siblings) 60 #next_element和previous_element属性获取下一个被解析的对象,或者上一个 61 print("17;next_element和previous_element属性获取下一个被解析的对象,或者上一个") 62 print(soup.a.next_element) 63 print(soup.a.previous_element) 64 #next_elements和previous_elements迭代器向前或者后访问文档解析内容 65 print("18;next_elements和previous_elements迭代器向前或者后访问文档解析内容") 66 print(soup.a.next_elements) 67 print(soup.a.previous_elements)
运行结果:
/home/aaron/桌面/Python3-Test/venv/bin/python /home/aaron/桌面/Python3-Test/bs4-study.py 1;获取head标签 <head><title>The Dormouse's story</title></head> 2;#获取p节点下的b节点 <b>The Dormouse's story</b> 4;name属性获取节点名称 body 5;获取p节点所有属性 {'class': ['title']} 6;获取p节点class属性 ['title'] 7;直接获取p节点class属性 ['title'] 8;获取a标签下的文本,只获取第一个 The Dormouse's story 9;contents属性获取节点的直接子节点,以列表的形式返回内容 ['\n', <p class="title"><b>The Dormouse's story</b></p>, '\n', <p class="story">Once upon a time there were three little sisters; and their names were <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>; and they lived at the bottom of a well.</p>, '\n', <p class="story">...</p>, '\n'] 10;children属性获取的也是节点的直接子节点,只是以生成器的类型返回 <list_iterator object at 0x7f0b1bd17750> 11;descendants属性获取子孙节点,返回生成器 <generator object Tag.descendants at 0x7f0b19e17d50> 12;parent属性获取父节点,parents获取祖先节点,返回生成器 <p class="title"><b>The Dormouse's story</b></p> <generator object PageElement.parents at 0x7f0b19e17d50> 13;next_sibling属性返回下一个兄弟节点 , 14;previous_sibling返回上一个兄弟节点,注意换行符也是一个节点 Once upon a time there were three little sisters; and their names were 15;next_sibling属性返回下一个兄弟节点 <generator object PageElement.next_siblings at 0x7f0b19e17d50> 16;previous_sibling返回上一个兄弟节点,注意换行符也是一个节点 <generator object PageElement.previous_siblings at 0x7f0b19e17d50> 17;next_element和previous_element属性获取下一个被解析的对象,或者上一个 Elsie Once upon a time there were three little sisters; and their names were 18;next_elements和previous_elements迭代器向前或者后访问文档解析内容 <generator object PageElement.next_elements at 0x7f0b19e17d50> <generator object PageElement.previous_elements at 0x7f0b19e17d50> Process finished with exit code 0