簡體   English   中英

從HTML提取元素

[英]Extract element from HTML

我有html:

<div class="img-holder">
 <h1>Sample Image</h1>
 <img src="http://sample.com/img.jpg"/>
</div>

帶有:

s = soup.find('div', {'class' : 'img-holder'}).h1
s = s.get_text()

顯示“樣本圖像”。

如何使用相同格式獲取圖像src?

使用img.attrs["src"]

例如:

from bs4 import BeautifulSoup
s = """<div class="img-holder">
 <h1>Sample Image</h1>
 <img src="http://sample.com/img.jpg"/>
</div>"""

soup = BeautifulSoup(s, "html.parser")
s = soup.find('div', {'class' : 'img-holder'})
print( s.img.attrs["src"] )

像這樣?

soup.find('img')['src']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM