python - 我如何将 img 元素和文本放入 span-block 中?

标签 python html python-3.x beautifulsoup

我有这样的跨度 block :

<span class="selectable-text invisible-space copyable-text" dir="ltr">
     some text
     <img alt="" class="b61 emoji wa selectable-text invisible-space copyable-text" data-plain-text="" src="URL" style="background-position: -20px -20px;"/>
     more some text
     <img alt="" class="b62 emoji wa selectable-text invisible-space copyable-text" data-plain-text="" src="URL" style="background-position: -40px -40px;"/>
     blah-blah-blah
     <img alt="" class="b76 emoji wa selectable-text invisible-space copyable-text" data-plain-text="" src="URL" style="background-position: 0px -20px;"/>
</span>
soup.find('span', {'class': 'selectable-text invisible-space copyable-text'}).get_text()

这段代码只给我文本。

我想到的一切

span = soup.select('span', {'class': 'selectable-text invisible-space copyable-text'})
for item in span:
    if re.match('.*emoji', str(item)):
        ...

现在我有这样的字符串:

<span class="selectable-text invisible-space copyable-text" dir="ltr">some text <img alt="😂" class="b61 emoji wa selectable-text invisible-space copyable-text" data-plain-text="😂" src="URL" style="background-position: -20px -20px;"/>more some text<img alt="😡" class="b62 emoji wa selectable-text invisible-space copyable-text" data-plain-text="😡" src="URL" style="background-position: -40px -40px;"/> blah-blah-blah  <img alt="🤯" class="b76 emoji wa selectable-text invisible-space copyable-text" data-plain-text="🤯" src="URL" style="background-position: 0px -20px;"/></span>

在我看来,下一步是使用正则表达式来获取我需要的元素。

有没有其他方法可以获取如下字符串:

some text <emoji> more some text <emoji> blah-blah-blah <emoji>

最佳答案

Span 标签内查找子元素,然后使用 previous_element (文本值)。

from bs4 import BeautifulSoup
data='''<span class="selectable-text invisible-space copyable-text" dir="ltr">
     some text
     <img alt="" class="b61 emoji wa selectable-text invisible-space copyable-text" data-plain-text="" src="URL" style="background-position: -20px -20px;"/>
     more some text
     <img alt="" class="b62 emoji wa selectable-text invisible-space copyable-text" data-plain-text="" src="URL" style="background-position: -40px -40px;"/>
     blah-blah-blah
     <img alt="" class="b76 emoji wa selectable-text invisible-space copyable-text" data-plain-text="" src="URL" style="background-position: 0px -20px;"/>
</span>'''

soup=BeautifulSoup(data,'html.parser')
itemtag=soup.find('span', class_='selectable-text invisible-space copyable-text')
children = itemtag.findChildren()
items=[]
for child in children:
  items.append(child.previous_element.replace('\n','').strip())
  items.append(child)

print(items)

输出:

['some text', <img alt="" class="b61 emoji wa selectable-text invisible-space copyable-text" data-plain-text="" src="URL" style="background-position: -20px -20px;"/>, 'more some text', <img alt="" class="b62 emoji wa selectable-text invisible-space copyable-text" data-plain-text="" src="URL" style="background-position: -40px -40px;"/>, 'blah-blah-blah', <img alt="" class="b76 emoji wa selectable-text invisible-space copyable-text" data-plain-text="" src="URL" style="background-position: 0px -20px;"/>]

关于python - 我如何将 img 元素和文本放入 span-block 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56423472/

相关文章:

python - 超越关键字依赖的文本分类并推断实际含义

javascript - 按钮上的数据加载消息不适用于 ajax 调用

javascript - 使用 Ajax 仅检索数据库中的数据之间如何更好地从 html 结构中的数据库检索数据?

python - 如何在 Docker 上运行的 Apache Superset 中为电子邮件报告配置 Celery Worker 和 Beat?

python - 属性错误 : 'Settings' object has no attribute 'OSCAR_REQUIRED_ADDRESS_FIELDS'

python - 三引号转义歧义

python - Pandas Dataframe 删除具有特定值的行,直到该值更改

javascript - 使用 javascript 获取选定行的值

android - 您如何在 kivy 应用程序中使用语言选项

python - 列表python的产品