#coding:utf8 from bs4 import BeautifulSoup #指定要搜索的文档: html_doc = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>, <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ from bs4 import BeautifulSoup soup = BeautifulSoup(html_doc, 'html.parser') # 传入文档 解析器 指定编码:UTF-8 soup=BeautifulSoup(html_doc,'html.parser',from_encoding='utf-8') links=soup.find_all('a') print '获取 P 段落的文字' link_node=soup.find('p' ,class_="title") #不能用class,因为跟python关键字冲突 print link_node.name,link_node.get_text() #输出: ''' 获取 P 段落的文字 p The Dormouse's story '''
python之find all
最新推荐文章于 2025-02-15 06:45:00 发布
本文介绍如何利用Python中的BeautifulSoup库解析HTML文档并提取所需信息。通过实例演示了查找特定标签及其内容的方法,例如获取带有特定类名的P段落文字。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
Python3.9
Conda
Python
Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本
1673

被折叠的 条评论
为什么被折叠?



