首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Beautifulsoup从HTML标签中获取单个属性的值?

如何使用Beautifulsoup从HTML标签中获取单个属性的值?
EN

Stack Overflow用户
提问于 2018-07-17 13:58:46
回答 2查看 442关注 0票数 -1

我使用这段代码来遍历一个p标记列表,这个列表比我的示例中包含1个或多个span标记的列表要长得多。我知道列表中的span标签也有font-style属性。我一直在尝试弄清楚我正在查看的font-style属性是否有一个斜体值。有没有办法获取font-style属性的值,或者如果font-style是斜体,则返回一个布尔值?

代码语言:javascript
复制
content = "<p dir="ltr">
               <span style="color: rgb(0, 0, 0); font-style: normal; background-color: transparent; font-weight: 400; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap">a</span>
           </p>,
           <p dir="ltr">
               <span style="color: rgb(0, 0, 0); font-style: italic; background-color: transparent; font-weight: 400; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap">b</span>
               <span style="color: rgb(0, 0, 0); font-style: normal; background-color: transparent; font-weight: 400; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap">c</span>
           </p>,
           <p dir="ltr">
               <span style="color: rgb(0, 0, 0); font-style: normal; background-color: transparent; font-weight: 400; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap">d</span>
               <span style="color: rgb(0, 0, 0); font-style: italic; background-color: transparent; font-weight: 400; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap">e</span>
           </p>"
soup = BeautifulSoup(test, 'html.parser')

page = {}
ital = []
i = 1
p = 1
for par in soup:
    page[i] = {}
    for x in par.find_all('span'):
        if x['font-style'] == 'italic':  #stuck here trying to figure out if font-style value is italic or not
            ital.append(p)
        par = 'par_{}'.format(p)
        page[i].update({par:x.next})
        p += 1
    page[i].update({'ital':ital})
    ital = []
    i += 1
    p = 1 

更新:

我的目标是在page上按顺序排列span标记之间的所有内容,并知道内容的哪一部分是斜体的。

运行此页面后,应如下所示

代码语言:javascript
复制
print(page)

{
    1: {'ital': [],
        'par_1':'a'},
    2: {'ital': [1],
        'par_1':'b',
        'par_2':'c'},
    3: {'ital': [2],
        'par_1':'d',
        'par_2':'e'}
}

当前打印此代码

代码语言:javascript
复制
print(page)

{
    1: {'ital': [],
        'par_1':'a'},
    2: {'ital': [],
        'par_1':'b',
        'par_2':'c'},
    3: {'ital': [],
        'par_1':'d',
        'par_2':'e'}
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-18 04:20:37

span标记中的所有内容都是一个属性,该属性的名称是style。其他的一切都是style中的字符串。获取字体样式属性的值是不可能的,因为它不是一个属性。if 'font-style: italic' in x['style']:是检查字体样式是否为斜体的方法。x['style']以字符串的形式返回样式属性的值。然后只检查x['style']返回的字符串中是否存在'font-style: italic'

票数 0
EN

Stack Overflow用户

发布于 2018-07-17 14:09:47

可以,您可以在BeautifulSoup的find_all()函数中使用lambda。此示例将查找样式属性中具有'italize‘的所有span标记:

代码语言:javascript
复制
from bs4 import BeautifulSoup

content = '''"<p dir="ltr">
               <span style="color: rgb(0, 0, 0); font-style: normal; background-color: transparent; font-weight: 400; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap">a</span>
           </p>,
           <p dir="ltr">
               <span style="color: rgb(0, 0, 0); font-style: italize; background-color: transparent; font-weight: 400; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap">d</span>
               <span style="color: rgb(0, 0, 0); font-style: normal; background-color: transparent; font-weight: 400; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap">b</span>
           </p>,
           <p dir="ltr">
               <span style="color: rgb(0, 0, 0); font-style: normal; background-color: transparent; font-weight: 400; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap">c</span>
               <span style="color: rgb(0, 0, 0); font-style: italize; background-color: transparent; font-weight: 400; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap">d</span>
           </p>"'''

soup = BeautifulSoup(content, 'lxml')

for span in soup.find_all('span', style=lambda s: 'italize' in s):
    print(span)

打印:

代码语言:javascript
复制
<span style="color: rgb(0, 0, 0); font-style: italize; background-color: transparent; font-weight: 400; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap">d</span>
<span style="color: rgb(0, 0, 0); font-style: italize; background-color: transparent; font-weight: 400; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap">d</span>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51374189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档