gpt4 book ai didi

python - 如何使用Python正则表达式获取Image src?

转载 作者:行者123 更新时间:2023-11-28 04:07:41 33 4
gpt4 key购买 nike

如何使用正则表达式使用 Python 从以下 html 字符串中获取图像的 src

<td width="80" align="center" valign="top"><font style="font-size:85%;font-family:arial,sans-serif"><a href="http://news.google.com/news/url?sa=t&amp;fd=R&amp;usg=AFQjCNFqz8ZCIf6NjgPPiTd2LIrByKYLWA&amp;url=http://www.news.com.au/business/spain-victory-faces-market-test/story-fn7mjon9-1226390697278"><img src="//nt3.ggpht.com/news/tbn/380jt5xHH6l_FM/6.jpg" alt="" border="1" width="80" height="80" /><br /><font size="-2">NEWS.com.au</font></a></font></td>

我试过

matches = re.search('@src="([^"]+)"',text)
print(matches[0])

却一无所获

最佳答案

您可以考虑使用 BeautifulSoup 而不是正则表达式:

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(junk)
>>> soup.findAll('img')
[<img src="//nt3.ggpht.com/news/tbn/380jt5xHH6l_FM/6.jpg" alt="" border="1" width="80" height="80" />]
>>> soup.findAll('img')[0]['src']
u'//nt3.ggpht.com/news/tbn/380jt5xHH6l_FM/6.jpg'

关于python - 如何使用Python正则表达式获取Image src?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10972086/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com