我使用这段代码来遍历一个p标记列表,这个列表比我的示例中包含1个或多个span标记的列表要长得多。我知道列表中的span标签也有font-style属性。我一直在尝试弄清楚我正在查看的font-style属性是否有一个斜体值。有没有办法获取font-style属性的值,或者如果font-style是斜体,则返回一个布尔值?
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标记之间的所有内容,并知道内容的哪一部分是斜体的。
运行此页面后,应如下所示
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'}
}
当前打印此代码
print(page)
{
1: {'ital': [],
'par_1':'a'},
2: {'ital': [],
'par_1':'b',
'par_2':'c'},
3: {'ital': [],
'par_1':'d',
'par_2':'e'}
}
发布于 2018-07-18 04:20:37
span标记中的所有内容都是一个属性,该属性的名称是style。其他的一切都是style中的字符串。获取字体样式属性的值是不可能的,因为它不是一个属性。if 'font-style: italic' in x['style']:
是检查字体样式是否为斜体的方法。x['style']
以字符串的形式返回样式属性的值。然后只检查x['style']
返回的字符串中是否存在'font-style: italic'
。
发布于 2018-07-17 14:09:47
可以,您可以在BeautifulSoup的find_all()
函数中使用lambda。此示例将查找样式属性中具有'italize‘的所有span
标记:
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)
打印:
<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>
https://stackoverflow.com/questions/51374189
复制相似问题