安装beautifulsoup4
使用beautifulsoup4:(两个参数分别是数据和解析格式)
import requests
from bs4 import BeautifulSoup
#获取html源代码
r = requests.get("http://python123.io/ws/demo.html")
demo = r.text
#使用BeautifulSoup类的html解析器
soup = BeautifulSoup(demo,'html.parser')
#对html添加换行符,有美化的功能
print(soup.prettify())
tag = soup.a
print("标签tag:",tag)
print("标签名:",tag.name)
print("标签属性:",tag.attrs,"对标签属性id取值:",tag.attrs['id'])
print("标签内容:",tag.string)
print("==========================遍历子节点==========================")
tag = soup.head
#子节点的列表
print("head标签子节点:",tag.contents)
tag = soup.body
print("body标签子节点:",tag.contents,"长度:",len(tag.contents))
print("对body标签子节点取值:",tag.contents[1])
print("--------------------------遍历儿子节点---------------")
for child in soup.body.children:
print(child)
print("--------------------------遍历子孙节点---------------")
for child in soup.body.children:
print(child)
print("==========================标签树的上行遍历==========================")
print(type(soup.a.parents))
for parent in soup.a.parents:
if parent is None:
print(parent)
else:
print(parent.name)