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&fd=R&usg=AFQjCNFqz8ZCIf6NjgPPiTd2LIrByKYLWA&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)
但什么也没得到
您可以考虑使用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)
| 归档时间: |
|
| 查看次数: |
14619 次 |
| 最近记录: |