'''
爬取页面图片2.0
'''
import re
import threading
from urllib import request
import requests
basepath = "D:/PyDownload/"
def downImg(imgUrl, filename):
request.urlretrieve(imgUrl, filename)
print(filename, "下载完成")
if __name__ == "__main__":
resp = requests.get("http://www.163.com")
html = resp.text
pattern = re.compile("<img.* src=\"(https?://.*?)\".*>")
reslist = pattern.findall(html)
tlist = []
for i in range(len(reslist)):
imgUrl = reslist[i]
print(imgUrl)
try:
filename = basepath + str(i) + ".jpg"
t = threading.Thread(target=downImg, args=(imgUrl, filename))
tlist.append(t)
t.start()
except Exception as e:
print(e)
pass
for t in tlist:
t.join()
print("main over")