从基于链接的网站拉图像

时间:2013-04-13 01:58:07

标签: python html python-2.7 beautifulsoup

我目前有代码使用BeautifulSoup库来拉取html文档中的所有图像标记。我想知道在Python中是否有一种方法可以通过图像标记中链接元素指向的图像以编程方式将图像本地下载到您的计算机。以下是我在网页中获取所有图片代码的代码。

import urllib2
from bs4 import BeautifulSoup

#Retrieve object containing website information
page = urllib2.urlopen("http://google.com")
soup = BeautifulSoup(page)
#Pull image tags
print soup.findAll('img')

1 个答案:

答案 0 :(得分:1)

您需要做的就是解析img标记以找到网址并使用urllib.urlretrieve之类的内容下载。