bs4 库 BeautifulSoup类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bs4 库 BeautifulSoup类相关的知识,希望对你有一定的参考价值。
bs4 库 BeautifulSoup类:
1. bs4 库 :
定义: bs4 库 是用来解析 遍历 维护 html (标签树) 的功能库

2 . bs4 库的引用,导入:

3 BeautifulSoup 类:
soup=BeautifulSoup(content,‘html.parser‘) # 实例化对象 | content是内容 | ‘html.parser‘ 是解析器
import requests
from bs4 import BeautifulSoup
url=‘http://www.bilibili.com/video/av9784617/index_56.html#page=22‘
try:
r=requests.get(url)
r.raise_for_status()
r.encoding=r.apparent_encoding
content=r.text[:500]
except:
print(‘Error!‘)
soup=BeautifulSoup(‘‘,‘html.parser‘) # 实例化 soup 对象 | content是内容 ‘html.parser‘ 是解析器
print(soup.head.prettify())
4 bs库的遍历 :
以上是关于bs4 库 BeautifulSoup类的主要内容,如果未能解决你的问题,请参考以下文章
pyhont---信息的爬取与提取---bs4,BeautifulSoup,re库