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

Don*_* Li 1 html python regex html-parsing

如何使用正则表达式从使用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])
Run Code Online (Sandbox Code Playgroud)

但什么也没得到

fra*_*xel 7

您可以考虑使用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'
Run Code Online (Sandbox Code Playgroud)


buc*_*ley 6

只需在正则表达式中丢失@,它就会起作用