bs4 模块

爬虫之bs4模块

我们在编写一些业务时需要从html页面上获取到用户输入的内容。比如说文章内容。

其实底层是在编写html代码,用户输入的时候看着是字其实是标签里包含输入的文本

我们如果直接保存到表里会把标签一起存起来了。这时候就需要一些工具帮助我们筛选或匹配。

from bs4 import BeautifulSoup
soup = BeautifulSoup('文本','解释器')
soup.find('标签')  # 拿到标签
soup.find_all('标签') # 拿到所有指定标签
soup.find('标签').text  # 拿到指定标签的文本内容
soup.find('a').get('href') # 拿到a标签内的href

排除掉标签提取所有的字符

from bs4 import BeautifulSoup

Soup = BeautifulSoup(content,'lxml')
tags=soup.find_all()
for tag in tags:
    if tag.name =='script':
        tag.decompose() # 删除标签
 

image

posted @ 2023-01-05 17:33  李阿鸡  阅读(28)  评论(0编辑  收藏  举报
Title