BeautifulSoup是一个模块,该模块用于接收一个HTML或XML字符串,然后将其进行格式化,之后便可以使用他提供的方法进行快速查找指定元素,从而使得在HTML或XML中查找指定元素变得简单。
from bs4 import BeautifulSoup html_doc = """ <html><head><title>The Dormouse's story</title></head> <body> asdf <div class="title"> <b>The Dormouse's story总共</b> <h1>f</h1> </div> <div class="story">Once upon a time there were three little sisters; and their names were <a class="sister0" >Els<span>f</span>ie</a>, <a href="http://example.com/lacie" class="sister" >Lacie</a> and <a href="http://example.com/tillie" class="sister" >Tillie</a>; and they lived at the bottom of a well.</div> ad<br/>sf <p class="story">...</p> </body> </html> """ soup = BeautifulSoup(html_doc, features="lxml") # 找到第一个a标签 tag1 = soup.find(name='a') # 找到所有的a标签 tag2 = soup.find_all(name='a') # 找到id=link2的标签 tag3 = soup.select('#link2') 简单示例
1. name,标签名称
1 # tag = soup.find('a') 2 # name = tag.name # 获取 3 # print(name) 4 # tag.name = 'span' # 设置 5 # print(soup)
2. attr,标签属性
1 # tag = soup.find('a') 2 # attrs = tag.attrs # 获取 3 # print(attrs) 4 # tag.attrs = {'ik':123} # 设置 5 # tag.attrs['id'] = 'iiiii' # 设置 6 # print(soup)
3. children,所有子标签
1 # body = soup.find('body') 2 # v = body.children
4. descendants,所有子子孙孙标签
1 # body = soup.find('body') 2 # v = body.descendants
5. clear,将标签的所有子标签全部清空(保留标签名)
1 # tag = soup.find('body') 2 # tag.clear() 3 # print(soup)
6. decompose,递归的删除所有的标签
1 # body = soup.find('body') 2 # body.decompose() 3 # print(soup)
7. extract,递归的删除所有的标签,并获取删除的标签
1 # body = soup.find('body') 2 # v = body.extract() 3 # print(soup)
8. decode,转换为字符串(含当前标签);decode_contents(不含当前标签)
1 # body = soup.find('body') 2 # v = body.decode() 3 # v = body.decode_contents() 4 # print(v)
9. encode,转换为字节(含当前标签);encode_contents(不含当前标签)
1 # body = soup.find('body') 2 # v = body.encode() 3 # v = body.encode_contents() 4 # print(v)
10. find,获取匹配的第一个标签
1 # tag = soup.find('a') 2 # print(tag) 3 # tag = soup.find(name='a', attrs={'class': 'sister'}, recursive=True, text='Lacie') 4 # tag = soup.find(name='a', class_='sister', recursive=True, text='Lacie') 5 # print(tag)
11. find_all,获取匹配的所有标签
1 # tags = soup.find_all('a') 2 # print(tags) 3 4 # tags = soup.find_all('a',limit=1) 5 # print(tags) 6 7 # tags = soup.find_all(name='a', attrs={'class': 'sister'}, recursive=True, text='Lacie') 8 # # tags = soup.find(name='a', class_='sister', recursive=True, text='Lacie') 9 # print(tags) 10 11 12 # ####### 列表 ####### 13 # v = soup.find_all(name=['a','div']) 14 # print(v) 15 16 # v = soup.find_all(class_=['sister0', 'sister']) 17 # print(v) 18 19 # v = soup.find_all(text=['Tillie']) 20 # print(v, type(v[0])) 21 22 23 # v = soup.find_all(id=['link1','link2']) 24 # print(v) 25 26 # v = soup.find_all(href=['link1','link2']) 27 # print(v) 28 29 # ####### 正则 ####### 30 import re 31 # rep = re.compile('p') 32 # rep = re.compile('^p') 33 # v = soup.find_all(name=rep) 34 # print(v) 35 36 # rep = re.compile('sister.*') 37 # v = soup.find_all(class_=rep) 38 # print(v) 39 40 # rep = re.compile('http://www.oldboy.com/static/.*') 41 # v = soup.find_all(href=rep) 42 # print(v) 43 44 # ####### 方法筛选 ####### 45 # def func(tag): 46 # return tag.has_attr('class') and tag.has_attr('id') 47 # v = soup.find_all(name=func) 48 # print(v) 49 50 51 # ## get,获取标签属性 52 # tag = soup.find('a') 53 # v = tag.get('id') 54 # print(v)
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:爬虫之BeautifulSoup - Python技术站