在beatifulsoup解析器python中提取src

时间:2015-01-16 19:41:42

标签: python parsing beautifulsoup

需要在标签'div.participant-logo'的python中使用beautifulsoup提取标签中的src。

import requests
import bs4

root_url = 'here url to parse'

response = requests.get(root_url)
soup = bs4.BeautifulSoup(response.text)

logo_cuadro1 = soup.select('div.participant-logo')

print (logo_cuadro1)

代码HTML

<td class="participant-logo"><a href="/futbol/espana/equipo-cordoba-cf-8004992.html"><img src="http://medias/logos/icons/teams-80/7869.png?v=2"></a></td>

2 个答案:

答案 0 :(得分:0)

获取src:

soup = bs4.BeautifulSoup("""<td class="participant-logo"><a href="/futbol/espana/equipo-cordoba-cf-8004992.html"><img src="http://medias/logos/icons/teams-80/7869.png?v=2"></a></td>""")

logo_cuadro1 = soup.find("img")["src"]

print (logo_cuadro1)

http://medias/logos/icons/teams-80/7869.png?v=2

在你的情况下:

sel  = soup.select('div.participant-logo')
link = sel[0].find("img")["src"]

答案 1 :(得分:0)

您可以将CSS选择扩展为具有src属性的图像:

for img in soup.select('div.participant-logo img[src]'):
    print img['src']