我有这样的跨度 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/