您的位置:首页 > 编程语言 > Python开发

Python3抓取网页图片

2017-01-26 14:13 323 查看
# -*- coding: utf-8 -*-
# @Author: Mayuko
# @Date:   2017-01-24 15:09:23
# @Last Modified by:   Mayuko
# @Last Modified time: 2017-01-26 14:09:06
import urllib.request
import socket
import re
import sys
import os
import math
targetDir = r"D:\images"

def destFile(path):
if not os.path.isdir(targetDir):
os.mkdir(targetDir)
pos = path.rindex('/')
t = os.path.join(targetDir, path[pos + 1:])
return t

if __name__ == "__main__":
hostname = "http://vscom.mayuko.cn/"
req = urllib.request.Request(hostname)
webpage = urllib.request.urlopen(req)
contentBytes = webpage.read()
for link, t in set(re.findall(r'(http://[^\s]*?(jpg))', str(contentBytes))):
print("Cache the images of " + link)
urllib.request.urlretrieve(link, destFile(link))

print("Cache done")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python