【发布时间】:2017-04-24 03:42:17
【问题描述】:
我刚刚发现了 Beautiful Soup,它看起来非常强大。我想知道是否有一种简单的方法可以用文本提取“alt”字段。 一个简单的例子是
from bs4 import BeautifulSoup
html_doc ="""
<body>
<p>Among the different sections of the orchestra you will find:</p>
<p>A <img src="07fg03-violin.jpg" alt="violin" /> in the strings</p>
<p>A <img src="07fg03-trumpet.jpg" alt="trumpet" /> in the brass</p>
<p>A <img src="07fg03-woodwinds.jpg" alt="clarinet and saxophone"/> in the woodwinds</p>
</body>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
print(soup.get_text())
这会导致
在管弦乐队的不同部分中,您会发现:
字符串中的A
铜管家
木管乐器
但我希望在文本提取中包含 alt 字段,这样可以提供
在管弦乐队的不同部分中,您会发现:
弦乐中的小提琴
铜管中的小号
木管乐器中的单簧管和萨克斯管
谢谢
【问题讨论】:
-
看看:stackoverflow.com/questions/2612548/…(这个问题可能重复)
标签: python beautifulsoup alt