Python3 抓取网页中的图片
来源:互联网 发布:短发发型设计软件 编辑:程序博客网 时间:2024/05/01 02:43
import urllib.requestimport socketimport reimport sysimport ostargetDir = r"C:\Users\elqstux\Desktop\pic"def destFile(path): if not os.path.isdir(targetDir): os.mkdir(targetDir) pos = path.rindex('/') t = os.path.join(targetDir, path[pos+1:]) return tif __name__ == "__main__": hostname = "http://www.douban.com" req = urllib.request.Request(hostname) webpage = urllib.request.urlopen(req) contentBytes = webpage.read() for link, t in set(re.findall(r'(http:[^\s]*?(jpg|png|gif))', str(contentBytes))): print(link) urllib.request.urlretrieve(link, destFile(link))