python:从html获取图像链接

San*_*yBr 1 python regex xml

来自这样的html/rss片段

[...]<div class="..." style="..."></div><p><a href="..."
<img alt="" heightt="" src="http://link.to/image"
width="" /></a><span style="">[...]
Run Code Online (Sandbox Code Playgroud)

我想获取图像src链接"http://link.to/image.jpg".我怎么能在python中这样做?谢谢.

Aco*_*orn 5

lxml 是工作的工具.

从网页上抓取所有图像就像这样简单:

import lxml.html

tree = lxml.html.parse("http://example.com")
images = tree.xpath("//img/@src")

print images
Run Code Online (Sandbox Code Playgroud)

赠送:

['/_img/iana-logo-pageheader.png', '/_img/icann-logo-micro.png']

如果它是RSS提要,您需要解析它lxml.etree.