您的位置:首页 > 理论基础 > 计算机网络

python使用re, os, httplib, urllib批量下载网页上的图片

2015-05-30 09:55 246 查看
import re ,httplib, urllib, os

conn = httplib.HTTPConnection("www.njupt.edu.cn")
"""
下面这行代码可以用httplib提供的其他几个方法实现
但是要求读者对http协议有一定的了解,示例如下:
dataBody = urllib.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'})
conn.putrequest("POST", "/")
conn.putheader("HOST", "www.njupt.edu.cn")
conn.putheader("Content-Type", "application/x-www-form-urlencoded")
conn.putheader("Content-Length", str(len(databody)))
conn.putheader("Accept", "text/plain")
conn.endheaders()
conn.send(dataBody)
"""

"""
request()方法也可以携带头部字段,携带的头部字段会自动覆盖同名的头部字段
对于POST方法的Content-Length字段由方法自己计算添加,也可以由我们自己添加
例如:
params = urllib.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'})
headers = {"Content-type": "application/x-www-form-urlencoded",
"Content-Length": str(len(params))
"Accept": "text/plain"}
conn.request("POST", "/", params, headers)
"""

"""
上述两种方法没有明显差别
"""

conn.request("GET", "/")
response = conn.getresponse()
htmlPage = response.read()
conn.close()
"""
利用re模块正则表达式提取html网页中img地址(对html不熟,可能这块会有问题)
推荐书籍《精通正则表达式》

"""
reg = re.compile("<img *src=\"([^\"]+)\"")
"""
提取网页里面的图片链接,可能会有重复的地址
"""
pics = reg.findall(htmlPage)

dirname = "/home/myhome/pic"
os.chdir(dirname)
reg1 = re.compile("http://")

"""
用来提取文件名
"""
reg2 = re.compile("/([^/]+\.[a-zA-Z]{3,4})$")
"""
tuple(set(pics))用于去重
"""
for pic in tuple(set(pics)):
if reg1.match(pic):
filename = reg2.findall(pic)[0]
urllib.urlretrieve(pic, filename)
print(filename+" download completed....")
else:
pic = "http://www.njupt.edu.cn"+pic
filename = reg2.findall(pic)[0]
urllib.urlretrieve(pic, filename)
print(filename+" download completed....")




另外补充几点:

(1)在后面下载图片文件时,我们如果自己构造HTTP header,那么切记要替换图片对应的HOST头值

(2)我们可以使用httpllib.request和len = int(HTTPResponse.getheader("Content-Length"))、HTTPResponse.read(len)自己读取图片数据再写入磁盘,这样我们可以重复利用一次tcp连接,下载多个图片文件,不过在此之前我们必须对所属于同一域名的图片文件进行归类再下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: