from bs4 import BeautifulSoup
soup = BeautifulSoup(html#,'lxml','xml','html5lib')
soup.prettify()#补全网页格式
soup.title.string#title里的内容
就是相当于soup的属性,soup.Tag
有多个,只返回第一个
在标签后加 .name
soup.p.attrs['name']
soup.p['name']
soup.Tag.contents
子节点以列表形式返回
soup.Tag.children
迭代器,需要循环才能输出
soup.Tag.descendants
同样为迭代器
父节点
soup.Tag.parent
祖先节点
soup.Tag.parents
soup.Tag.next_siblings
soup.Tag.previous_siblings
处理方式很快,但用途有限
find_all(name, attrs, recursive, text, kwargs)**
find_all(‘...‘)
find_all(id='...')
find_all(class_='...')
text也可以用上述方法,text=‘...‘
find 返回单个元素
find_parent()
find_parents()
find_next_sibling()
find_next_siblings()
find_previous_siblings()
find_all_next()
find_next()
#返回节点后符合条件的节点
find_all_previous()
选择class时输入‘‘ .class1 .class2"
选择Tag时直接传入即可
选择id时输入 "#id"
获取内容get_text()
原文:https://www.cnblogs.com/guiguiguoguo/p/11177866.html