python爬虫笔记(4-1)bs4模块

 
说明:
Beautiful Soup 4.4.0 文档:
 
它的作用是能够简单快速地提取网页中指定的内容,用requests模块获取网页源代码,用bs4的接口将网页源代码生成一个对象,通过这个对象的方法来提取数据
 
bs4解析的逻辑:
<标签 属性=“值”>被标记的内容</标签>
通过某个标签,获取标签特征(属性),通过特征定位到要提取的内容
 
1、安装bs4模块:
pip3 install Beautifulsoup4
 
2、导入
from bs4 import BeautifulSoup
 
3、从bs对象中查找数据
3.1、find方法:查一个,取第一个的值
find(‘标签’,属性=‘值’)
soup.find('a')
soup.find('a', class_='xxx')
soup.find('a', title='xxx')
soup.find('a', id='xxx')
soup.find('a', id=re.compile(r'xxx'))
 
3.2、find_all方法:查所有,返回一个列表
find_all(‘标签’,属性=‘值’)
soup.find_all('a')
soup.find_all('a', class_='wang')
soup.find_all('a', id=re.compile(r'xxx'))
soup.find_all('a', limit=2) 提取出前两个符合要求的a
 

posted on 2021-08-14 11:06  坐看浮云飘飘飘  阅读(118)  评论(0编辑  收藏  举报

导航