用于解析、遍歷、維護“標(biāo)簽樹”的庫
用于解析html
from bs4 import BeautifulSoup
import request
r=requests.get("http://www.baidu.com")
demo=r.text #返回的是一個html的文件
soup=BeautifulSoup(demo,'html.parser') #解析html信息
另外一種方式 soup=BeautifulSoup(open('D://demo.html'),'html.parser'))
tag=soup.a #找到a標(biāo)簽
print(tag.attrs['class']) #查看a標(biāo)簽的屬性信息
print(tag.string)
用這個網(wǎng)址測試:https://python123.io/ws/demo.html
soup.title
soup.a.parent.name
soup.p.parent.name

image.png

image.png