我有带字段的Django项目,其中包含内容(来自QuerySet):
<p><b>Name and LastName</b><br />
Work Title<br /><span class="text-spacer"></span>
</p>
<p><b>Name and LastName 1</b><br />
Work Title1 <br /><span class="text-spacer"></span>
</p>
<p><b>Name and LastName 2</b><br />
Work Title 2<br /><span class="text-spacer"></span>
</p>
但是我想使用(-)格式的文本:
Name and LastName - Work Title
Name and LastName 2 - Work Title 2
Name and LastName 3 - Work Title 3
这是我的代码,但是我只有第一个项目,但是我想拥有包含项目的数组:
text_list = self.texts.filter(code='ON')
for i in text_list:
soup = BeautifulSoup(i.text_en, "html.parser")
aa = soup.p.get_text(separator=" - ", strip=True)
return [aa]
您需要遍历p
标签。从您提供的示例中,您可以尝试如下操作:
source = """<p><b>Name and LastName</b><br />
Work Title<br /><span class="text-spacer"></span>
</p>
<p><b>Name and LastName 1</b><br />
Work Title1 <br /><span class="text-spacer"></span>
</p>
<p><b>Name and LastName 2</b><br />
Work Title 2<br /><span class="text-spacer"></span>
</p>
"""
soup = BeautifulSoup(source, 'lxml')
ary = [p.get_text(separator=' - ', strip=True) for p in soup.find_all('p')]
该ary
会是:
[u'Name and LastName - Work Title',
u'Name and LastName 1 - Work Title1',
u'Name and LastName 2 - Work Title 2']
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句