[python]BeautifulSoup中的find和find_all函数
上一篇 /
下一篇 2020-09-29 09:26:48
/ 个人分类:Python
BeautifulSoup是
Python第三方库,应用于Python爬虫时用于html文件解析
#BeautifulSoup在Python3下的安装命令:
pip3 install bs4
#在代码中导入BeautifulSoup命令
from bs4 import BeautifulSoup
#BeautifulSoup中的find()与find_all()函数
find(name,attrs,recursive,text,**wargs)
-name:查找的标签名称
-attrs:返回标签的属性
-recursive: 递归?
-text:返回标签的内容
举栗子:
myhtml = <span class="title">肖申克的救赎</span>
BeautifulSoup.find('span') =>
BeautifulSoup.find(myhtml.attrs) =>
BeautifulSoup.find(myhtml.text) =>
#find_all()返回所有匹配到的结果,区别于find(仅返回查找到的第一个结果)
#find_all(name,attrs,recursive,text,limit,**kwargs)
栗子就不举了,可以使用上面find的查询,将find替换为find_all
注意在使用这两个函数之前,都要使用lxml或者html.parser对html文件进行解析,如以下句式:
response = requests.get(myurl.'lxml') 或
response = requests.get(myurl.'html.parser')
收藏
举报
TAG:
Python语法