如何使用bs4爬虫解析html

  • Beautiful Soup简介
  • Beautiful Soup安装
  • Beautiful Soup用法

Beautiful Soup简介

  • 官方网址:https://beautifulsoup.readthedocs.io/zh_CN/latest/
  • Beautiful Soup是一个可以从HTML文件中提取数据的Python库,它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式
  • 在接口测试中用于验证页面内容的正确性

Beautiful Soup安装

  • Windows 下命令行输入:pip install beautifulsoup4 即可。

Beautiful Soup用法

from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc,"html.parser")
print soup
print type(soup)
print soup.prettify()# 按照标准的缩进格式的结构输出
# 通过标签名称来获取Tag对象,如果有多个相同的标签名称,返回第一个
print soup.html
print soup.body
print soup.title
print type(soup.title)# class类,tag标签
print soup.find_all('a')# 查找所有a标签
print soup.find_all('a')[2]# 查找第二个a标签
print soup.title.string# 输出字符串格式String
print soup.b.string # 输出注释

#find只返回第一个
print soup.find("id='''")

发布了18 篇原创文章 · 获赞 13 · 访问量 2314

猜你喜欢

转载自blog.csdn.net/m0_37518413/article/details/103228958
今日推荐