鱼C论坛

 找回密码
 立即注册
查看: 1921|回复: 3

[已解决]soup.find_all 里怎么查 style 呢?

[复制链接]
发表于 2023-4-16 10:28:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
<span style="font-size: 15px;">他从床上爬起来,</span>



for sentence in soup.find_all():
          
        if sentence.name == 'span' and sentence.style == '"font-size: 15px;"':
        #if sentence.name == 'span' and sentence.attrs == '{"style": "font-size: 15px;"}':

怎么在if里 把 style 加上呢?   

最佳答案
2023-4-16 10:45:19
blackantt 发表于 2023-4-16 10:40
for sentence in soup.find_all():
   if sentence.name == 'span' and sentence.attrs == '{"style": " ...

当然可以,你可以在if语句中使用get方法检查style属性。这是一个示例:
from bs4 import BeautifulSoup

html = '''<span style="font-size: 15px;">他从床上爬起来,</span>
          <img src="example.jpg" alt="Example Image">'''

soup = BeautifulSoup(html, 'html.parser')

for element in soup.find_all():
    if element.name == 'span' and element.get('style') == 'font-size: 15px;':
        print(element.text)
    elif element.name == 'img':
        print(element.get('src'))

在这个示例中,我们使用element.get('style')从元素中提取style属性。如果style属性值等于'font-size: 15px;',则会打印span元素的文本内容。如果元素名称为img,则会打印src属性的值。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-16 10:30:32 | 显示全部楼层
要在BeautifulSoup中查找具有特定样式的元素,你可以使用find_all方法并在函数中使用attrs参数。在你的例子中,你可以通过以下方式在if语句中添加style属性:
from bs4 import BeautifulSoup

html = '''<span style="font-size: 15px;">他从床上爬起来,</span>'''

soup = BeautifulSoup(html, 'html.parser')

for sentence in soup.find_all('span', attrs={'style': 'font-size: 15px;'}):
    print(sentence.text)

这里我们直接在find_all方法中指定元素名称('span')和属性(attrs={'style': 'font-size: 15px;'})。这样,find_all会返回所有符合条件的span元素。然后,我们可以遍历这些元素并打印它们的文本内容。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-4-16 10:40:54 | 显示全部楼层
isdkz 发表于 2023-4-16 10:30
要在BeautifulSoup中查找具有特定样式的元素,你可以使用find_all方法并在函数中使用attrs参数。在你的例子 ...

for sentence in soup.find_all():
   if sentence.name == 'span' and sentence.attrs == '{"style": "font-size: 15px;"}':
      。。。
   elif sentence.name == 'img':

如果要放在if里,有办法吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-16 10:45:19 | 显示全部楼层    本楼为最佳答案   
blackantt 发表于 2023-4-16 10:40
for sentence in soup.find_all():
   if sentence.name == 'span' and sentence.attrs == '{"style": " ...

当然可以,你可以在if语句中使用get方法检查style属性。这是一个示例:
from bs4 import BeautifulSoup

html = '''<span style="font-size: 15px;">他从床上爬起来,</span>
          <img src="example.jpg" alt="Example Image">'''

soup = BeautifulSoup(html, 'html.parser')

for element in soup.find_all():
    if element.name == 'span' and element.get('style') == 'font-size: 15px;':
        print(element.text)
    elif element.name == 'img':
        print(element.get('src'))

在这个示例中,我们使用element.get('style')从元素中提取style属性。如果style属性值等于'font-size: 15px;',则会打印span元素的文本内容。如果元素名称为img,则会打印src属性的值。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-23 17:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表