路飞学城web作业总结

本文介绍如何使用Python的BeautifulSoup库解析HTML文档。通过实例展示了查找特定标签、获取标签属性和文本内容的方法。

BeautifulSoup是一个模块,该模块用于接收一个HTML或XML字符串,然后将其进行格式化,之后遍可以使用他提供的方法进行快速查找指定元素,从而使得在HTML或XML中查找指定元素变得简单。

from bs4 import BeautifulSoup
 
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
asdf
    <div class="title">
        <b>The Dormouse's story总共</b>
        <h1>f</h1>
    </div>
<div class="story">Once upon a time there were three little sisters; and their names were
    <a  class="sister0" id="link1">Els<span>f</span>ie</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.</div>
ad<br/>sf
<p class="story">...</p>
</body>
</html>
"""
 
soup = BeautifulSoup(html_doc, features="lxml")
# 找到第一个a标签
tag1 = soup.find(name='a')
# 找到所有的a标签
tag2 = soup.find_all(name='a')
# 找到id=link2的标签
tag3 = soup.select('#link2')

安装

pip3 install beautifulsoup4

使用实例:

from bs4 import BeautifulSoup
 
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
    ...
</body>
</html>
"""
 
soup = BeautifulSoup(html_doc, features="lxml")

1. name,标签名称

1 tag = soup.find('a')
2 name = tag.name # 获取
3 print(name)
4 tag.name = 'span' # 设置
5 print(soup)

2. attr,标签属性

1 tag = soup.find('a')
2 attrs = tag.attrs    # 获取
3 print(attrs)
4 tag.attrs = {'ik':123} # 设置
5 tag.attrs['id'] = 'iiiii' # 设置
6 print(soup)

3. children,所有子标签

body = soup.find('body')
v = body.children

4.  children,所有子子孙孙标签

body = soup.find('body')
v = body.descendants

5. clear,将标签的所有子标签全部清空(保留标签名)

tag = soup.find('body')
tag.clear()
print(soup)

6. decode,转换为字符串(含当前标签);decode_contents(不含当前标签)

body = soup.find('body')
v = body.decode()
v = body.decode_contents()
print(v)

7. encode,转换为字节(含当前标签);encode_contents(不含当前标签)

body = soup.find('body')
v = body.encode()
v = body.encode_contents()
print(v)

10. find,获取匹配的第一个标签

# tag = soup.find('a')
# print(tag)
# tag = soup.find(name='a', attrs={'class': 'sister'}, recursive=True, text='Lacie')
# tag = soup.find(name='a', class_='sister', recursive=True, text='Lacie')
# print(tag)

11. find_all,获取匹配的所有标签

# tags = soup.find_all('a')
# print(tags)
 
# tags = soup.find_all('a',limit=1)
# print(tags)
 
# tags = soup.find_all(name='a', attrs={'class': 'sister'}, recursive=True, text='Lacie')
# # tags = soup.find(name='a', class_='sister', recursive=True, text='Lacie')
# print(tags)
 
 
# ####### 列表 #######
# v = soup.find_all(name=['a','div'])
# print(v)
 
# v = soup.find_all(class_=['sister0', 'sister'])
# print(v)
 
# v = soup.find_all(text=['Tillie'])
# print(v, type(v[0]))
 
 
# v = soup.find_all(id=['link1','link2'])
# print(v)
 
# v = soup.find_all(href=['link1','link2'])
# print(v)
 
# ####### 正则 #######
import re
# rep = re.compile('p')
# rep = re.compile('^p')
# v = soup.find_all(name=rep)
# print(v)
 
# rep = re.compile('sister.*')
# v = soup.find_all(class_=rep)
# print(v)
 
# rep = re.compile('http://www.oldboy.com/static/.*')
# v = soup.find_all(href=rep)
# print(v)
 
# ####### 方法筛选 #######
# def func(tag):
# return tag.has_attr('class') and tag.has_attr('id')
# v = soup.find_all(name=func)
# print(v)
 
 
# ## get,获取标签属性
# tag = soup.find('a')
# v = tag.get('id')
# print(v)

12. has_attr,检查标签是否具有该属性

# tag = soup.find('a')
# v = tag.has_attr('id')
# print(v)

13. get_text,获取标签内部文本内容

# tag = soup.find('a')
# v = tag.get_text('id')
# print(v)

 

转载于:https://www.cnblogs.com/wlx97e6/p/9287948.html

### 路飞学与图灵学院的课程质量及口碑对比 #### 一、课程体系完整性 图灵学院以其全面而系统的课程设计著称,尤其是在Java和Python领域提供了完整的教学路径[^2]。其课程结构清晰,适合初学者逐步掌握基础知识并深入到高级应用开发阶段。相比之下,关于路飞学的具体课程设置资料较少,但从公开评价来看,它也强调实战演练和技术栈覆盖广泛的特点。 #### 二、师资力量分析 图灵学院拥有一支由行业资深专家组成的教师队伍,这些导师不仅拥有扎实的技术功底,而且擅长通过生动的教学方式提升学生的学习体验。对于路飞学而言,虽然也有一定数量的经验丰富的讲师参与授课,但在知名度以及具体案例展示方面可能稍逊于前者。 #### 三、实践机会提供情况 两家机构都重视培养学生的动手能力。其中提到图灵学院特别注重给予学员真实的项目操作经历,帮助他们在真实环境中运用所学到的知识解决问题。至于路飞学是否有同样水平的实际工程项目支持,则需进一步核实相关信息源来确认具体情况。 #### 四、用户反馈总结 从现有评论可以看出,许多参与者对图灵学院表达了高度认可的态度,认为它是值得信赖的选择之一[^1]。而对于路飞学来说,尽管存在正面声音,但整体声量相对较低,因此难以形成确切结论。 ```python # 示例代码用于说明如何评估两个平台的数据差异 def compare_platforms(platform_a_reviews, platform_b_reviews): avg_score_a = sum([review['rating'] for review in platform_a_reviews]) / len(platform_a_reviews) avg_score_b = sum([review['rating'] for review in platform_b_reviews]) / len(platform_b_reviews) if avg_score_a > avg_score_b: return f"{platform_a} has better reviews." elif avg_score_a < avg_score_b: return f"{platform_b} has better reviews." else: return "Both platforms have similar ratings." example_data_turing = [{'rating':5}, {'rating':4}] example_data_luffy = [{'rating':3},{'rating':4}] result=compare_platforms(example_data_turing , example_data_luffy ) print(result) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值