Python - 从html文件中获取所有图像

时间:2010-11-28 03:16:22

标签: python image urllib

有人可以帮我解析一个html文件,以获取python中文件中所有图像的链接吗?

最好没有第三方模块...

谢谢!

3 个答案:

答案 0 :(得分:10)

您可以使用Beautiful Soup。我知道你说没有第三方模块。但是,这是解析HTML的理想工具。

import urllib2
from BeautifulSoup import BeautifulSoup
page = BeautifulSoup(urllib2.urlopen("http://www.url.com"))
page.findAll('img')

答案 1 :(得分:10)

仅使用PSL

from html.parser import HTMLParser
class MyParse(HTMLParser):
    def handle_starttag(self, tag, attrs):
        if tag=="img":
            print(dict(attrs)["src"])

h=MyParse()
page=open("index.html").read()
h.feed(page)

答案 2 :(得分:2)

普遍认为lxml比Beautiful Soup (ref)快。它的教程可以在这里找到:(link) 您也可以查看this old stackoverflow post